不期望OpenCV分水岭结果

时间:2016-02-19 10:59:37

标签: opencv computer-vision watershed

我正在使用一些样本测试分水岭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() 

Input image

  

应用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()

Result 结果(蓝线)与我的期望(红线)相比有点偏移

0 个答案:

没有答案