[英]How to create an outline (with controllable thickness) from a mask segmentation image in Python?
如果我理解正確,您想找到所有斑點的輪廓,然后將此輪廓繪制到另一個具有可控輪廓厚度的圖像上。 您可以使用cv2.drawContours()
執行此操作,並使用thickness
參數控制輪廓厚度。 設置負值,例如。 -1
,將填充輪廓,同時增加參數會給你一個更厚的輪廓。
在此示例中,我們使用 cv2.findContours() 找到每個 blob 的輪廓,然后使用cv2.drawContours()
cv2.findContours()
將輪廓繪制到蒙版上。 在您的情況下,您可以將其繪制到所需的圖像上,而不是將其繪制到蒙版上。 thickness=2
:
thickness=5
:
import cv2
import numpy as np
image = cv2.imread('1.png')
mask = np.ones(image.shape, dtype=np.uint8) * 255
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(mask, [c], -1, (36, 255, 12), thickness=5)
cv2.imshow('mask', mask)
cv2.waitKey()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.