簡體   English   中英

如何在輪廓opencv內繪制輪廓?

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

我有以下圖像,我想在紅色輪廓(線)內繪制輪廓。 因此,另一種顏色輪廓將從內部附加到紅色輪廓。 在此處輸入圖像描述

在此處輸入圖像描述 紅色輪廓是你的,綠色輪廓是里面的輪廓。

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