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

时间:2021-06-23 18:00:38

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

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

1 个答案:

答案 0 :(得分:1)

enter image description here 红色轮廓是你的,绿色轮廓是里面的轮廓。

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)