I'm trying to find the green object in the video stream using the following code - unfortunately the HSV code doesn't always work well, doesn't always select the greeen but rather some odd background part.
So I'm trying to find the biggest contour out of the image - which is likely the green object (HSV checked - How can I define a threshold value to detect only green colour objects in an image with Python OpenCV?), then mark a rectangle around it. The rectangle doesn't seem to show up though?
import numpy as np import cv2 as cv major_number = cv.__version__[0] cap = cv.VideoCapture(0) if not cap.isOpened(): print("Cannot open camera") exit() while True: # Capture frame-by-frame ret, frame = cap.read() # if frame is read correctly ret is True if not ret: print("Can't receive frame (stream end?). Exiting ...") break # Our operations on the frame come here hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) mask = cv.inRange(hsv, (40, 0, 0), (70, 255,255)) #Get contour of the object, dimensions: #ret,thresh = cv.threshold(mask,127,255,0) if major_number == '4': contours, hierarchy = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) elif major_number == '3': img, contours, hierarchy = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) area = 0 cntlarge = [] for cnt in contours: x,y,w,h = cv.boundingRect(cnt) if( w*h > area ): area = w*h cntlarge = cnt x,y,w,h = cv.boundingRect(cntlarge) print(x,y,w,h) rect = cv.minAreaRect(cntlarge) box = cv.boxPoints(rect) box = np.int0(box) cv.drawContours(mask,[box],0,(0,0,255),2) #cv.rectangle(mask,(x,y),(x+w,y+h),(0,255,0),2) #cv.drawContours(mask,[box],0,(0,0,255),2) # Display the resulting frame cv.imshow('frame', mask) if cv.waitKey(1) == ord('q'): break # When everything done, release the capture cap.release() cv.destroyAllWindows() See image shown:

UPDATE
This is an example in which you can see it doesn't find all the green on an old shirt - and doesn't mark the area it found on the final image-show:

import numpy as np import cv2 as cv major_number = cv.__version__[0] #cap = cv.VideoCapture(0) #if not cap.isOpened(): # print("Cannot open camera") # exit() while True: # Capture frame-by-frame frame = cv.imread('greenshirt.jpg') # if frame is read correctly ret is True #if not ret: # print("Can't receive frame (stream end?). Exiting ...") # break # Our operations on the frame come here hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) mask = cv.inRange(hsv, (40, 0, 0), (70, 255,255)) #Get contour of the object, dimensions: #ret,thresh = cv.threshold(mask,127,255,0) if major_number == '4': contours, hierarchy = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) elif major_number == '3': img, contours, hierarchy = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) area = 0 cntlarge = [] for cnt in contours: x,y,w,h = cv.boundingRect(cnt) if( w*h > area ): area = w*h cntlarge = cnt x,y,w,h = cv.boundingRect(cntlarge) print(x,y,w,h) rect = cv.minAreaRect(cntlarge) box = cv.boxPoints(rect) box = np.int0(box) cv.drawContours(mask,[box],0,(0,0,255),2) #cv.rectangle(mask,(x,y),(x+w,y+h),(0,255,0),2) #cv.drawContours(mask,[box],0,(0,0,255),2) # Display the resulting frame cv.imshow('frame', mask) if cv.waitKey(1) == ord('q'): break # When everything done, release the capture cap.release() cv.destroyAllWindows()
Источник: https://stackoverflow.com/questions/780 ... ith-opencv