尝试使用
cv2.fillPoly
将简单的多边形转化为光栅图时,我得到了奇怪的结果——非零像素的总数是10201,但应该是100*100。pts = np.array([[0, 0], [100, 0], [100, 100], [0, 100], [0, 0]])
img = np.zeros((256, 256))
vertices = np.array([pts], dtype=np.int32)
mask = cv2.fillPoly(img, vertices, color=255)
print('np.count_nonzero(mask)', np.count_nonzero(mask))
出了什么问题?
101 * 101
(它确实是),因为您在第100列和第100行有一个边缘。想象一下小规模([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]
),那是4条线,覆盖了4个像素(没有填充)。 - Dan Mašek