1 | initial version |
I am not sure if this would work for you but you can make use of contourArea method to get contour area and filter out some small contours on a basis of that. Here is the link.
2 | No.2 Revision |
I am not sure if this would work for you but you can make use of contourArea method to get contour area and filter out some small contours on a basis of that. Here is the link.
Another elegant solution would be get a bounding rects and get width and height from rect's 4 coordinates. But I'm not sure if you're using it to detect regular shaped contours and not.
3 | No.3 Revision |
I am not sure if this would work for you but you can make use of contourArea method to get contour area and filter out some small contours on a basis of that. Here is the link.
Another elegant solution would be get a bounding rects and get width and height from rect's 4 coordinates. coordinates (by calculating distance on your own). But I'm not sure if you're using it to detect regular shaped contours and not.
4 | No.4 Revision |
I am not sure if this would work for you but you can make use of contourArea method to get contour area and filter out some small contours on a basis of that. Here is the link.
Another elegant solution would be get a bounding rects rect and get width and height from rect's 4 coordinates (by calculating distance on your own). But I'm not sure if you're using it to detect regular shaped contours and not.
5 | No.5 Revision |
I am not sure if this would work for you but you can make use of contourArea method to get contour area and filter out some small contours on a basis of that. Here is the link.
threshold_area = 10000 #threshold area
contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area > threshold_area:
#Put your code in here
Another elegant solution would be get a bounding rect and get width and height from rect's 4 coordinates (by calculating distance on your own). But I'm not sure if you're using it to detect regular shaped contours and not.
And as far as I understand, you code should be rearranged as follow for it to work:
contours, hierarchy = cv2.findContours(im,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours
rect = cv2.minAreaRect(cnt) #I have used min Area rect for better result
width = rect[1][0]
height = rect[1][1]
if (width<widthmax) and (height <heightmax) and (width >= widthMin) and (height > heightMin):
#put your code here if desired contour is detected
Hope it helps.
6 | No.6 Revision |
I am not sure if this would work for you but you can make use of contourArea method to get contour area and filter out some small contours on a basis of that. Here is the link.. Example code as below:
threshold_area = 10000 #threshold area
contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area > threshold_area:
#Put your code in here
Another elegant solution would be get a bounding minimum area rect and get width and height from rect's 4 coordinates (by calculating distance on your own). But I'm not sure if you're using it to detect regular shaped contours and not.
And as far as I understand, you code should be rearranged as follow for it to work:
contours, hierarchy = cv2.findContours(im,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours
rect = cv2.minAreaRect(cnt) #I have used min Area rect for better result
width = rect[1][0]
height = rect[1][1]
if (width<widthmax) and (height <heightmax) and (width >= widthMin) and (height > heightMin):
#put your code here if desired contour is detected
Hope it helps.