【问题标题】:How to draw the contour inside the contour opencv?如何在轮廓opencv内绘制轮廓?
【发布时间】:2021-09-07 07:32:54
【问题描述】:

我有以下图像,我想在红色轮廓(线)内绘制轮廓。因此,另一种颜色轮廓将从内部附加到红色轮廓。

【问题讨论】:

  • 在黑色背景上绘制一个白色填充轮廓,与您的图像大小相同。然后使用形态学稍微侵蚀轮廓。然后从侵蚀的图像中计算出一个新的轮廓。在原始图像上绘制这个新轮廓,它将位于现有轮廓内。
  • 欢迎来到 Stack Overflow。请阅读帮助中心 (stackoverflow.com/help) 中的信息指南,特别是“如何提出一个好问题”(stackoverflow.com/help/how-to-ask) 和“如何创建最小的、可重现的示例”( stackoverflow.com/help/minimal-reproducible-example).
  • @fmw42 成功了。谢谢。

标签: python image opencv image-processing opencv-contour


【解决方案1】:

红色轮廓是你的,绿色轮廓是里面的轮廓。

import cv2
import numpy as np
img = cv2.imread('input.png')
gray = cv2.imread('input.png',0)
img=np.uint8(img)

blank=np.zeros([768,1024,3],np.uint8)
cnts = cv2.findContours(255-gray, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(blank, [c], -1, (255, 255, 255), -1)
kernel = np.ones((3, 3), np.uint8)
erosion_image = cv2.erode(blank, kernel, iterations=2)

blank = np.uint8(erosion_image[:,:,0])
cnts = cv2.findContours(blank, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(img, [c], -1, (0, 255, 0), 1)

cv2.imwrite('output.png', img)

【讨论】:

    猜你喜欢
    • 2019-01-25
    • 2021-01-17
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 2014-05-25
    • 2019-02-12
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多