流域 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()
大津二値化を適用して前景と背景を区別する
#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()
結果 結果 (青い線) は、私が期待したもの (赤い線) から少しずれています。