【问题标题】:Python OpenCV draw contour only on the outside borderPython OpenCV仅在外边框上绘制轮廓
【发布时间】:2015-11-19 09:58:32
【问题描述】:

当使用 OpenCV 的drawContours 绘制轮廓时,边框以轮廓为中心绘制,我只想在轮廓外侧绘制边框。

这张图片(取自 SketchUp 文档)解释得最好:

drawContours 像第一个圆一样绘制轮廓(轮廓在绘制边框的中间)。我只需要在轮廓外侧设置边框,就像在最后一个圆圈中一样。

有人知道我该如何实现这种行为吗?

谢谢。

【问题讨论】:

  • OpenCV 没有内置函数来执行此操作。如果你不太关心性能,你可以:1)在mask1上绘制填充的白色轮廓,2)在mask2上放大1,3)mask3 = mask1 XOR mask2。 4)使用mask3将图像中的像素设置为您想要的颜色。
  • 你会一直有颜色均匀的内圈吗,在绘制轮廓的时候你有关于内圈颜色的信息吗?

标签: python opencv opencv-drawcontour


【解决方案1】:

将代码用作

  _ret, contours, hierarchy = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
  cv2.drawContours(img,contours , -1, (255,0,0), 1)

这里 cv2.RETR_EXTERNAL 只给出检测到的外部轮廓。

【讨论】:

  • 感谢您的回答 AdityaIntwala 但我说的是绘制轮廓而不是检测轮廓。
  • 您需要为此找到外轮廓。一旦找到外轮廓,您就可以正常使用drawContours函数绘制找到的外轮廓。
【解决方案2】:

假设内芯的颜色始终是同质的,并且您事先知道芯颜色的值,我们可以简单地这样做:

#First you draw the contour on both the sides of the border.
contour_id = 0
border_thickness = 10
border_color = (185, 115, 72)
cv2.drawContours(img, contours, contour_id, border_color, border_thickness)

#Now you again draw contour but with thickness = -1 and color = Core color
border_thickness = -1
core_color = (225, 141, 98)
cv2.drawContours(img, contours, contour_id, core_color, border_thickness)

【讨论】:

    猜你喜欢
    • 2012-09-22
    • 2019-01-25
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2018-02-13
    • 1970-01-01
    • 2015-03-02
    相关资源
    最近更新 更多