cv2.drawContours()
实现此目的,并使用thickness
参数来控制轮廓线条的粗细。设置负值,例如-1
,可填充轮廓,而增加参数值会使轮廓线条更加粗细。
在本例中,我们使用cv2.findContours()
找到每个斑点的轮廓,然后使用cv2.drawContours()
将轮廓线条绘制到蒙版上。对于您的情况,您可以将其绘制到所需的图像上。使用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()