【问题标题】:Contours from a image appear very sloppy with cv2 findContours. How to improve?使用 cv2 findContours 时,图像的轮廓显得非常草率。怎么提高?
【发布时间】:2019-04-09 12:45:59
【问题描述】:

我正在尝试使用 cv2 查找图像的轮廓。有很多相关的问题,但答案似乎总是非常具体,不适用于我的情况。

我有一张黑白图像,我会变成彩色。

thresh = cv2.cvtColor(thresh, cv2.COLOR_RGB2GRAY) 
plt.imshow(thresh)

接下来,我尝试找到轮廓。

image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

然后我通过在黑色背景上绘制它来可视化它。

blank_image = np.zeros((thresh.shape[0],thresh.shape[1],3), np.uint8) 
img = cv2.drawContours(blank_image, contours, 0, (255,255,255), 3)
plt.imshow(img)

轮廓遵循实际轮廓,即围绕整个事物。我怎样才能得到这样非常糟糕的油漆印象:

【问题讨论】:

  • 我认为您正在寻找的工件是“边缘”,而不是“轮廓”。
  • 提供重现问题的minimal reproducible example 以及原始输入图像。使用的 OpenCV 的状态版本。 |为什么第二个图中的坐标远远超过 4000?这是否意味着您的输入图像有那么大?
  • 我使用的是 3.4.1 版本。是的,输入图像就是这么大。

标签: python opencv cv2


【解决方案1】:

您可以使用 Canny 边缘检测来执行此操作:

import cv2
frame = cv2.imread("iCyrOT3.png")                       # read a frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)          # turn it gray
edges = cv2.Canny(gray, 100, 200)                       # get canny edges
cv2.imshow('Test', edges)                               # display the result
cv2.waitKey(0)

【讨论】:

  • 这行得通。奇怪的是,它不适用于大约 4000x4000 的原始图像。显然,尺寸需要足够小才能工作。
  • @MitchellvanZuylen,这很有趣。我会用它做实验。我也有使用轮廓的解决方案,但我必须在发布之前对其进行清理。
猜你喜欢
  • 1970-01-01
  • 2017-11-19
  • 2016-04-23
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多