【问题标题】:How to create polygons for watershed segmentation如何为流域分割创建多边形
【发布时间】:2014-12-24 18:10:33
【问题描述】:

我有一个图像,并希望使用标记控制的分水岭创建该图像的多边形段。我编写了以下代码,但我无法将相互连接的对象分开并创建对象的多边形。

如何解决这些问题?非常感谢您的帮助。

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')

【问题讨论】:

    标签: python opencv watershed


    【解决方案1】:

    This question 似乎非常接近您的需求,因为该示例使用与您完全相同的图像。

    要将生成的“水坝”转换为多边形,我建议在结果图像上使用cv2.findContourscv2.approxPolyDP

    【讨论】:

    • 按照您的建议,我将分割图像转换为二值图像,然后运行轮廓:_, contours1, _= cv2.findContours(thresh3,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)。然后运行函数: def approxing(cnts): back = [] closed = True for cnt in cnts: epsilon = 10 cnt2 = cv2.approxPolyDP(cnt, epsilon, closed) back.append(cnt2) return back c = approxing(contours1 )。并保存结果,image_out=cv2.imwrite('D:/contour2.jpg',c)。但是它有错误图像不是 numpy 或标量。如何解决这个问题?谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多