简体   繁体   English

如何在轮廓opencv内绘制轮廓?

[英]How to draw the contour inside the contour opencv?

I have the following image and I want to draw the contour inside the red contour (line).我有以下图像,我想在红色轮廓(线)内绘制轮廓。 So another colour contour will be attached to the red one from inside.因此,另一种颜色轮廓将从内部附加到红色轮廓。 在此处输入图像描述

在此处输入图像描述 red contour is Yours and green contour is the contour inside.红色轮廓是你的,绿色轮廓是里面的轮廓。

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)

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

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