如何为两个相连的圈子分水岭

问题描述

我正在处理两个图像:

enter image description here

enter image description here

(这里的图像大小不一样,但是在我的程序中它们是一样的)

在拍摄了上面两张图片的skimage.metrics.structural_similarity()之后,我的阈值如下:

enter image description here

如您所见,它由2个形状组成,这些形状几乎是圆形,但不完全是(右下角的额外部分是圆形的阴影)

我想对这个脱粒进行分水岭,以便获得两个圆,但是我当前的代码给了我这个:

enter image description here

相反,我想要的是蓝色的东西:

enter image description here

# import the necessary packages
from skimage.feature import peak_local_max
from skimage.segmentation import watershed
from scipy import ndimage
import numpy as np
import cv2
from skimage.metrics import structural_similarity

imageA = cv2.imread("frames/thing150.png") #the left image
imageB = cv2.imread("frames/thing180.png") #the right image
grayA = cv2.cvtColor(imageA,cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB,cv2.COLOR_BGR2GRAY)

(score,diff) = structural_similarity(grayA,grayB,full=True)
diff = (diff * 255).astype("uint8")

thresh = cv2.threshold(diff,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# cv2.namedWindow("Thresh",cv2.WINDOW_NORMAL)
# cv2.imshow("Thresh",thresh)

# compute the exact Euclidean distance from every binary
# pixel to the nearest zero pixel,then find peaks in this
# distance map
D = ndimage.distance_transform_edt(thresh)
localMax = peak_local_max(D,indices=False,min_distance=100,labels=thresh)
# perform a connected component analysis on the local peaks,# using 8-connectivity,then appy the Watershed algorithm
markers = ndimage.label(localMax,structure=np.ones((3,3)))[0]
labels = watershed(-D,markers,mask=thresh)
print(f"[INFO] {len(np.unique(labels)) - 1} unique segments found")

# loop over the unique labels returned by the Watershed
# algorithm
for label in np.unique(labels):
    # if the label is zero,we are examining the 'background'
    # so simply ignore it
    if label == 0:
        continue
    # otherwise,allocate memory for the label region and draw
    # it on the mask
    mask = np.zeros(grayB.shape,dtype="uint8")
    mask[labels == label] = 255
    # detect contours in the mask and grab the largest one
    (cnts,_) = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    c = max(cnts,key=cv2.contourArea)
    # draw a circle enclosing the object
    ((x,y),r) = cv2.minEnclosingCircle(c)
    cv2.circle(imageB,(int(x),int(y)),int(r),(0,0),2)
    cv2.putText(imageB,"#{}".format(label),(int(x) - 10,cv2.FONT_HERSHEY_SIMPLEX,0.6,255),2)
    print(len(cnts))
print("----")
print(np.unique(labels))
# show the output imageB
cv2.namedWindow("Output",cv2.WINDOW_NORMAL)
cv2.imshow("Output",imageB)
cv2.waitKey(0)

我对分水岭并不熟悉,因此我从this网站复制了代码。我尝试更改min_distancelocalMax = peak_local_max(D,labels=thresh)的参数,但这并不能解决我的问题。

我也尝试过使用OpenCV的分水岭算法,但是由于某种原因,它不起作用。如果那要比skimage好,那我会尝试一下。

任何建议将不胜感激。

PS thresh = cv2.threshold(diff,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

thresh = cv2.threshold(diff,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

我看到各种来源都使用这两种方式,并且两者都对我有用,有什么区别吗?

解决方法

此解决方案将从您上传的脱粒图像开始:

  1. 填充轮廓::由于脱粒图像没有真正连接(白色空间之间有很多黑色空间),我们必须找到一种方法来填充这些“孔”。我们可以使用一些开放的内核,但是大小在图像之间会有所不同。我认为最好找到外部轮廓并通过填充它们来创建某种遮罩。
contours,_ = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# get largest contour (snowman figurine)
idx = np.argmax([cv2.contourArea(cnt) for cnt in contours])
cnt = contours[idx]
# create mask
thresh = cv2.fillPoly(thresh,np.int32([cnt]),(255,255,255)) #Bug with fillPoly,needs explict cast to 32bit

Binarized mask

  1. 分水岭算法::使用watershed algorithm实现来分离两个连接的对象。我做了一些小的修改,因为该教程的更新不如人们希望的那样。
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel,iterations=2)
background = cv2.dilate(opening,iterations=3)

# extract foreground
dst = cv2.distanceTransform(opening,cv2.DIST_L2,5,dstType=cv2.CV_32F)
_,foreground = cv2.threshold(dst,0.7 * dst.max(),cv2.THRESH_BINARY)
foreground = np.uint8(foreground)
unknown = cv2.subtract(background,foreground)

# get markers
_,markers = cv2.connectedComponents(foreground)
markers += 1
markers[unknown == 255] = 0

# Since the original image is in fact two different images,use
# instead the thresh image,which is the combination of both.
thresh = cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR)
markers = cv2.watershed(thresh,markers)
# normalize markers so they can be visualized
markers = cv2.normalize(
  markers,None,alpha=0,beta=255,norm_type=cv2.NORM_MINMAX,dtype=cv2.CV_8U
)

Separated contours

  1. 阈值图像(再次)+轮廓线:现在,我们终于分离出轮廓线,现在可以找到封闭的圆了。为此,我建议对标记设置阈值,这样我们就不会再有更多的圆圈了。

注意::我使用了逆二进制,以便在两个轮廓之间获得一条漂亮的中间线,否则我们将回到平方一。

_,thresh = cv2.threshold(markers,150,cv2.THRESH_BINARY_INV)
contours,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

thresholdd image

  1. 近似圆:现在,我们可以找到最小的封闭圆。

注意::我使用了一个半径过滤器,因为获取的轮廓数量可能超过两个;您可能还需要使用此过滤器,以确保始终获得要查找的圆,而不是较大(或更小)的圆。

circles = []
for cnt in contours:
  (cx,cy),radius = cv2.minEnclosingCircle(cnt)
  if radius < 100:
    circles.append((int(cx),int(cy),int(radius)))

Minimum enclosing circles

这个过程可能可以被优化/减少,因为我即兴创作了一点,并没有花太多时间。但是无论如何,我希望这可以有所帮助:)

PS::使用“ +”还是“ |”都没关系在opencv中,它们的含义相同。尽管我建议您使用“ +”,因为它更具可读性,但完全取决于您。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...