python – 填充OpenCV轮廓的外部

前端之家收集整理的这篇文章主要介绍了python – 填充OpenCV轮廓的外部前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用openCV和 python语言在轮廓的外部区域用黑色着色.
这是我的代码
contours,hierarchy = cv2.findContours(copy.deepcopy(img_copy),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]
# how to fill of black the outside of the contours cnt please? `

解决方法

以下是如何在一组轮廓之外填充黑色图像:
import cv2
import numpy
img = cv2.imread("zebra.jpg")
stencil = numpy.zeros(img.shape).astype(img.dtype)
contours = [numpy.array([[100,180],[200,280],180]]),numpy.array([[280,70],[12,20],[80,150]])]
color = [255,255,255]
cv2.fillPoly(stencil,contours,color)
result = cv2.bitwise_and(img,stencil)
cv2.imwrite("result.jpg",result)


原文链接:https://www.f2er.com/python/185977.html

猜你在找的Python相关文章