Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Python, how can I reduce a list of contours to those of a specified size?

I've extracted contours from an image and now want to discard those that don't match a specified size requirement. But I'm new to Python and can't figure out how to do this efficiently. I can get a list of boolean that identify which contours should be retained, but how can I generate the new list of contours in a simple, single line? E.g., what's the right way to code the final line below? Thanks!

contours, hierarchy = cv2.findContours(im,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) brect = np.array([list(cv2.boundingRect(cnt)) for cnt in contours]) widths = brect[:,2] heights = brect[:,3] bUse = (widths<widthmax) &amp;="" (heights<heightmax)="" &amp;="" (widths="">widthMin) & (heights>heightMin) contours = contours[bUse] # DOESN'T WORK! WHY?

In Python, how can I reduce a list of contours to those of a specified size?

I've extracted contours from an image and now want to discard those that don't match a specified size requirement. But I'm new to Python and can't figure out how to do this efficiently. I can get a list of boolean booleans that identify which contours should be retained, but how can I generate the new list of contours in a simple, single line? E.g., what's the right way to code the final line below? Thanks!

contours, hierarchy = cv2.findContours(im,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) brect = np.array([list(cv2.boundingRect(cnt)) for cnt in contours]) widths = brect[:,2] heights = brect[:,3] bUse = (widths<widthmax) &amp;="" (heights<heightmax)="" &amp;="" (widths="">widthMin) & (heights>heightMin) contours = contours[bUse] # DOESN'T WORK! WHY?

In Python, how can I reduce a list of contours to those of a specified size?

I've extracted contours from an image and now want to discard those that don't match a specified size requirement. But I'm new to Python and can't figure out how to do this efficiently. I can get a list of booleans that identify which contours should be retained, but how can I generate the new list of contours in a simple, single line? E.g., what's the right way to code the final line below? Thanks!

contours, hierarchy = cv2.findContours(im,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cv2.findContours(im,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

brect = np.array([list(cv2.boundingRect(cnt)) for cnt in contours]) contours])

widths = brect[:,2] brect[:,2]

heights = brect[:,3] brect[:,3]

bUse = (widths<widthmax) &amp;="" (heights<heightmax)="" &amp;="" (widths="">widthMin) & (heights>heightMin) (heights>heightMin)

contours = contours[bUse] # DOESN'T WORK! WHY?