对于非常大的图像,在 opencv 中使用函数 fillPoly 的错误结果

Wrong result using function fillPoly in opencv for very large images

我很难解决蒙版问题 creation.My 图片很大, 40959px X 24575px,我正在尝试为其创建一个遮罩。 我注意到对于特定大小的图像我没有问题(我测试了大约 33000px X 22000px),但是对于大于这个尺寸的图像我在我的面具内得到一个错误(错误是它在多边形的中间变黑并且白色区域向左延伸 edge.Result 应该在多边形内部没有黑色区域并且没有白色区域延伸到图像的左边缘)。

所以我的代码是这样的:

    pixel_points_list = latLonToPixel(dataSet, lat_lon_pairs)
    print pixel_points_list
    # This is the list im getting
    #[[213, 6259], [22301, 23608], [25363, 22223], [27477, 23608], [35058, 18433], [12168, 282], [213, 6259]]
    image = cv2.imread(in_tmpImgFilePath,-1)
    print image.shape
    #Value of image.shape: (24575, 40959, 4)
    mask = np.zeros(image.shape, dtype=np.uint8)
    roi_corners = np.array([pixel_points_list], dtype=np.int32)
    print roi_corners
    #contents of roi_corners_array:
    """
    [[[  213  6259]
    [22301 23608]
    [25363 22223]
    [27477 23608]
    [35058 18433]
    [12168   282]
    [  213  6259]]]
    """
    channel_count = image.shape[2] 
    ignore_mask_color = (255,)*channel_count
    cv2.fillPoly(mask, roi_corners, ignore_mask_color)
    cv2.imwrite("mask.tif",mask)

这是我用这些坐标得到的蒙版(缩小版蒙版):

你看到在掩码的中间,掩码是 mirrored.I 从 pixel_points_list 中获取这些点并将它们绘制在坐标系上,我得到了有效的多边形,但是当使用 fillPoly 时我出错了结果。

这是一个更简单的例子,我只有 4(5) 分:

 roi_corners = array([[  213  6259]
                      [22301 23608]
                      [35058 18433]
                      [12168   282]
                      [  213  6259]])

我明白了

有人知道为什么会这样吗? 谢谢!

问题出在由 fillPoly(以及 drawContoursfillConvexPoly 等...)调用的函数 CollectPolyEdges 中。

在内部,假设点坐标(整数类型int32)仅在最低 16 位中具有有意义的值。在实践中,只有当你的点的坐标达到 32768(这恰好是你可以在图像中绘制的最大 x 坐标。)

时,你才能正确绘制。

这不能被视为错误,因为您的图片非常大。


作为解决方法,您可以尝试按给定系数缩放蒙版和点,在较小的蒙版上填充多边形,然后将蒙版重新缩放回原始大小


@DanMašek pointed out in the comments, this is in fact a bug 相同,尚未修复。

在错误讨论中,提到了另一种解决方法。它包括使用大小小于 32768 的多个 ROI 进行绘制,使用 fillPoly.

中的 offset 参数校正每个 ROI 的坐标