def binaryContoursNestingFilterHeuristic(img, cnts, *args, **kwargs):
'''
Concept : Use the found contours, with binary drawn contours to extract hierarchy and hence filter on nesting.
Critique : WIP
'''
# Set the image to black (0):
img[:,:] = (0,0,0)
# Draw all of the contours on the image in white
contours = [c.contour for c in cnts]
cv2.drawContours( img, contours, -1, (255, 255, 255), 1 )
iv = ImageViewer(img)
iv.windowShow()
# Now extract any channel
gray = cv2.split(img)[0]
iv = ImageViewer(gray)
iv.windowShow()
retval, bin = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
iv = ImageViewer(bin)
iv.windowShow()
# Now find the contours again, but this time we care about hierarchy (hence _TREE) - we get back next, previous, first_child, parent
bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
iv = ImageViewer(bin)
iv.windowShow()
# Alternative flags : only take the external contours
bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
iv = ImageViewer(bin)
iv.windowShow()
return cnts
评论列表
文章目录