使用OpenCV导出分割的图像

时间:2019-02-05 07:26:40

标签: python-3.x opencv

我需要导出包含原始图像的图像(以png,jpg,pdf,tiff等常见格式),以显示分割后对象的轮廓。

因此,我可以使用openCV分割一组图像,并且能够绘制检索到的对象的轮廓(以绿色显示),并最终将图像与轮廓一起打印:

[...]

h, w = thresholded.shape[:2]
_, contours0, hierarchy = cv.findContours(thresholded.copy(), cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
contours = [cv.approxPolyDP(cnt, 3, True) for cnt in contours0]

[...]

cv.drawContours(origImage, [cnt], -1, (128, 255, 0), 2)
cv.circle(origImage, (cX, cY), 1, (255,0,0), -1)

[...]

#here it shows the image together with the found contours
#print a window with "f = filename" as main title
cv.namedWindow(f)
cv.imshow(f, origImage)
cv.waitKey()
cv.destroyAllWindows()

现在,我想将图像(origImage)及其以绿线分隔的对象一起导出:

cv.imwrite(saveIn + dirn.split(directory)[1] + "/" + name + '.jpg', origImage)

但是我得到的是我提供的与输入相同的图像。在导出的图像中基本上没有轮廓。

我缺少一些参数吗?

编辑:

该脚本需要3个输入,即:

  • 指向图像的绝对路径
  • 产生结果的绝对路径(包含具有一些 功能和分割的图像)
  • 阈值

1 个答案:

答案 0 :(得分:0)

好的,我不好! 该代码有效

问题是,我使用的阈值太低,无法检测到任何轮廓(尽管它检测到的东西太小而无法显示),所以基本上没有任何显示(这有点棘手,但是这就是重点。