Python中除了使用OpenCV之外还有其他方法来填充轮廓的内容吗?
cv2.drawContours(mask, [a_contour], -1, 255, -1)
我发现很难相信这是唯一的方法。我已经在 PIL、skimage 等中但没有找到任何东西。
使用 scikit-image 查看这些过滤器:
我知道已经很晚了,但由于我正在寻找相同的答案,也许其他人可能会发现它有用。
cv2.drawContour实际上是用一些值填充轮廓,是坐标的轮廓列表不一定与像素粒度匹配,也就是说坐标可以是浮点数。
在 skimage 中,find_contour 算法的输出也可以用作多边形,您可以使用 skimage.draw.polygon 函数“绘制”(即填充多边形)以获得与 cv2.drawContour 相同的结果。
这是一个简单的示例代码:
# contour is one contour of the image as generated by the find_contour function
# img is the original image
# the img.shape is limiting the size of the polygon to be inside the image.
rr, cc = skimage.draw.polygon(contour[:,0], contour[:,1], img.shape)
# in this way you set the value of each pixel inside the polygon to 1
# or to whatever you want.
img[rr,cc] = 1