Python 中使用 cv2/skimage 进行分水岭分割(灰度图像)

Watershed Segmentation in Python with cv2/skimage (grayscale image)

提问人:BrLeon 提问时间:11/15/2023 更新时间:11/15/2023 访问量:46

问:

如何让分水岭分割正常工作?

我想将分水岭分割用于灰度图像,但是我没有得到我想要/期望的结果。我的图像中显然有一些分割无法识别的斑点......

通过分水岭分割,我希望隔离图像中的每个点,这是原始版本:要处理的原始图像我已经尝试使用 skimage 和 opencv,以文档和其他教程为导向。这是我得到的最好的结果: 最好的结果,用 opencv 完成 这是我的代码:

#image is already grayscale
image = tif_stack[0]

ret1, thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
kernel = np.ones((3,3), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations = 0)

from skimage.segmentation import clear_border
opening = clear_border(opening)

sure_bg = cv2.dilate(opening, kernel, iterations=0)
opening = np.uint8(opening)
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 3)
ret2, sure_fg = cv2.threshold(dist_transform, 0.5*dist_transform.max(), 255, 0)
sure_fg = np.uint8(sure_fg)

unknown = np.zeros(image.shape, np.uint8)
sure_bg = np.uint8(sure_bg)
unknown = cv2.subtract(sure_bg,sure_fg, unknown)
ret3, markers = cv2.connectedComponents(sure_fg)

markers = markers + 10
markers[unknown==255] = 0

#create RGB version of image for watershed func
image3 = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
image3 = np.uint8(image3)
markers_w = cv2.watershed(image3, markers)

#just noticed that i used the wrong markers (markers instead of markers_w)
#to edit the original image
#however when using the correct one, the result gets worse (see following image)
image[markers==-1]= 255

#display image
plt.figure()
plt.imshow(image, cmap = "gist_gray", interpolation= "nearest")
plt.show()

以下是使用 image[markers_w==-1]= 255 时的结果图像: 使用 markers_w 时的结果

python opencv scikit-图像 分水岭

评论


答: 暂无答案