【问题标题】:unable to get contour properly无法正确获得轮廓
【发布时间】:2018-06-22 12:41:45
【问题描述】:

我正在玩 openCV 并执行以下简单的任务。

1) 读取图片

2) 阈值化

3) 寻找轮廓。

4) 在空白图像中绘制所有轮廓。

5) 绘制单个轮廓。

在虚拟图像上绘制所有轮廓看起来不错,而绘制单个轮廓会产生分散的轮廓,如下图所示。

原文:

所有轮廓:

单轮廓:

请在下面找到代码。

import  cv2
import numpy as np

#Reading Image.
srcImg = cv2.imread("./bottle.jpeg")

#Color Conversion.

grayedImg = cv2.cvtColor(srcImg,cv2.COLOR_RGB2GRAY)
__, thresholdedImg = cv2.threshold(grayedImg, 240, 255, cv2.THRESH_BINARY)

#Noice Removal
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
erodeImage = cv2.erode(thresholdedImg,kernel, iterations=1)
dilatedImg = cv2.dilate(erodeImage,kernel, iterations=1)
_, contours, _ = cv2.findContours(dilatedImg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

#draw All Contours.

dummyImg = np.zeros(grayedImg.shape, dtype=grayedImg.dtype)
cv2.drawContours(dummyImg, contours, -1, 255, 1)
cv2.imshow("All Contours", dummyImg)
cv2.imwrite("allContours.jpeg",dummyImg)

#draw Individual Contours.

mask =  np.zeros(dummyImg.shape[:2], dtype= dummyImg.dtype)
isolatedImg = cv2.drawContours(mask, contours[9], -1, 255, 1)

cv2.imshow("Indivial Contours.", isolatedImg)
cv2.imwrite("single.jpeg",isolatedImg)

cv2.waitKey(0)

【问题讨论】:

  • 答案对您有帮助吗?你在寻找更多的东西吗?
  • 您的回答对我有帮助。非常感谢。 :)

标签: python opencv opencv3.0 opencv-contour


【解决方案1】:

你必须用另一组方括号括起来:

isolatedImg = cv2.drawContours(mask, [contours[9]], -1, 255, 1)

预期结果:

如果深入挖掘,cv2.findContours() 会返回数组的list。现在每个array 都包含有关构成轮廓的点数的详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多