0

画像があり、マーカー制御の流域を使用してこの画像のセグメントのポリゴンを作成したいと考えています。以下のコードを書いたのですが、くっついたオブジェクトを切り離して、オブジェクトのポリゴンを作成することができません。

それらの問題をどのように解決できますか?どうもありがとうございました。

import cv2
import numpy as np
import scipy.misc
import scipy.ndimage as snd
# image is read and is converted to a numpy array
img = cv2.imread('D:/exam_watershed/Example_2_medicine/Medicine_create_poly/medicine.jpg')
# image is convereted to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# binary thresholding is done using the threshold
# from Otsu's method
ret1,thresh1 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# foreground pixels are determined by
# performing erosion
fore_ground = cv2.erode(thresh1,None,iterations = 3)
bgt = cv2.dilate(thresh1,None,iterations = 3)
ret,back_ground = cv2.threshold(bgt,1,100,1)
# marker is determined by adding foreground and background pixels
marker = cv2.add(fore_ground,back_ground)
# converting marker to 32 int
marker32 = np.int32(marker)
cv2.watershed(img,marker32)
res = scipy.misc.toimage(marker32)
res.save('D:/exam_watershed/Example_2_medicine/Medicine_create_poly/res_output.png')

元の画像

セグメント後の画像

4

1 に答える 1

1

例ではあなたとまったく同じ画像を使用しているため、この質問はあなたのニーズにかなり近いようです。

結果の「ダム」をポリゴンに変換するには、結果の画像でcv2.findContoursをcv2.approxPolyDPと一緒に使用することをお勧めします。

于 2014-12-24T10:10:33.787 に答える