2014-08-31 180 views
0

enter image description here
我想从我的二进制图像中删除拉长的轮廓,但我仍然获得大部分。我试图删除它们,但考虑到紧凑性和偏心因素,但这在我的情况下不起作用。如何从二进制图像中删除细长结构(轮廓)

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) 

任何人都可以建议我一些方法来消除这些拉长的轮廓。

+0

您能否展示一些示例图片来帮助您查看“这些拉长的轮廓”的含义? – AldurDisciple 2014-08-31 07:31:28

+0

我编辑了我的帖子。我想要从二进制图像中删除像(结构化)的静脉(拉长)@AldurDisciple – user3778289 2014-08-31 09:11:02

+0

您是否尝试过使用morpho-maths?例如。 'erode' +'dilate'或'morphologyEx'? – AldurDisciple 2014-08-31 10:07:38

回答

1

我能想到的一个选择是计算每个对象区域和最大长度,而不是将阈值设置为区域/长度。