简体   繁体   English

Python OpenCV仅在外部边框上绘制轮廓

[英]Python OpenCV draw contour only on the outside border

When drawing a contour using OpenCV's drawContours the borders is drawn centered to the contour, I want to draw the border only on the outside of the contour. 使用OpenCV的drawContours绘制轮廓时,边框以轮廓为中心绘制,我只想在轮廓的外部绘制边框。

This image (taken from the SketchUp documentation) explains it best: 这张图片(取自SketchUp文档)说明得最好: 在此处输入图片说明

drawContours draws the contour like in the first circle (the contour is in the middle of the drawn border). drawContours像在第一个圆中一样绘制轮廓(轮廓位于绘制边框的中间)。 I need to have the border only on the outside of the contour, like in the last circle. 我只需要在轮廓的外部具有边框,就像最后一个圆一样。

Anyone have an idea as to how can I achieve this kind of behaviour? 有人对我如何实现这种行为有想法吗?

Thanks. 谢谢。

use the code as 使用代码作为

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

here cv2.RETR_EXTERNAL gives only the external detected contour. 这里cv2.RETR_EXTERNAL仅给出检测到的外部轮廓。

Assuming that the color of the inside core is always homogeneous, and you know the value of the core color beforehand, we can simply do it as: 假设内部核心的颜色始终是均匀的,并且您已经预先知道核心颜色的值,我们可以简单地执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM