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)
brect = np.array([list(cv2.boundingRect(cnt)) for cnt in contours])
widths = brect[:,2]
heights = brect[:,3]
bUse = (widths<widthmax) &="" (heights<heightmax)="" &="" (widths="">widthMin) & (heights>heightMin)
contours = contours[bUse] # DOESN'T WORK! WHY?