Reputation: 323
I am trying to remove elongated contours from my binary image but I am still getting most of them. I have tried to remove them using but considering compactness and eccentricity factors but that didn't work in my case.
im=cv2.imread('thresh.png')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.imshow('thres',gray)
gray2 = gray.copy()
mask = np.zeros(gray.shape,np.uint8)
contours, hier = cv2.findContours(gray2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area=cv2.contourArea(cnt)
if area>=5:
ellipse = cv2.fitEllipse(cnt)
# center, axis_length and orientation of ellipse
(center,axes,orientation) = ellipse
# length of MAJOR and minor axis
majoraxis_length = max(axes)
minoraxis_length = min(axes)
eccentricity = np.sqrt(1-(minoraxis_length/majoraxis_length)**2)
#############compactness################
area=cv2.contourArea(cnt)
equi_diameter = np.sqrt(4*area/np.pi)
compactness=equi_diameter/majoraxis_length
########################################
if(eccentricity<=0.6 or eccentricity >1) or (compactness <0.8):
cv2.drawContours(gray2,[cnt],0,(0,0,0),1)
cv2.drawContours(mask,[cnt],0,255,-1)
cv2.imshow('final',mask)
Can anyone suggest me some method for removing these elongated contours.
Upvotes: 0
Views: 751
Reputation: 281
One option i can think of, is to calculate each object area and max length, than set a threshold to area/length.
Upvotes: 1