3

我正在用一些样本测试分水岭 OpenCV 3.0 的实现,我得到了奇怪的结果,或者至少是我没想到的。

我正在应用以下内容:

读取输入图像

import numpy as np
import cv2
from matplotlib import pyplot as plt
import numpy

#Reading original image
img = cv2.imread('c:\\tmp\\image.png')
img = 255-img

cv2.imshow('Input',img)
cv2.waitKey(0)
cv2.destroyAllWindows() 

输入图像

应用 Otsu 二值化来区分前景和背景

#Binarizing by applying threshold 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

opening = ((opening - opening.min()) / (opening.max() - opening.min()) * 255).astype(numpy.uint8)

cv2.imshow('Threshold', opening)
cv2.waitKey(0)
cv2.destroyAllWindows()

获取要用作标记的连接点

# Marker labelling
ret, markers = cv2.connectedComponents(opening)

运行分水岭

# Apply watershed
markers = cv2.watershed(img,markers)

#Show result
img[markers == -1] = [255,0,0]
cv2.imshow('Watershed', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果 结果(蓝线)与我的预期(红线)略有不同

4

0 回答 0