简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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