def get_filtered_contours(self,img):
# convert image to color space hsv
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# threshold the image to remove all colors that are not yellpw
thresh = cv2.inRange(hsv_img, self.COLOR[0], self.COLOR[1])
# create the list of filtered contours to return
filtered_contours = []
# find all contours in the thresholded image
img_mod, contours, hierarchy = cv2.findContours(\
thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
# sort all contours by area, largest to smallest
contour_area = [ (cv2.contourArea(c), (c) ) for c in contours]
contour_area = sorted(contour_area,reverse=True, key=lambda x: x[0])
for j, (area,(cnt)) in enumerate(contour_area):
# only report MAX_DETECTIONS number of controus
if j >=self.MAX_DETECTIONS: break
# create a bounding box around the contour
x,y,w,h = cv2.boundingRect(cnt)
box = (x,y,w,h)
# add this contour to the list
filtered_contours.append( (cnt, box) )
if DEMO:
cv2.imshow('hsv_image', hsv_img)
cv2.imshow('in_range', thresh)
mask = cv2.bitwise_and(img, img, mask= thresh)
cv2.imshow('mask', mask)
cnts = img.copy()
cv2.drawContours(cnts, contours, -1, (0,255,0), 3)
cv2.imshow('contours', cnts)
cv2.waitKey(1)
return filtered_contours
评论列表
文章目录