Opencv floodFill:如何从图像转换为蒙版坐标?

Opencv floodFill: How do I transform from image to mask coordinates?

我在 android 应用程序中使用 floodFill。 Opencv floodfill 函数更改蒙版输入图像。

public static int floodFill(Mat image,
        Mat mask,
        Point seedPoint,
        Scalar newVal,
        Rect rect,
        Scalar loDiff,
        Scalar upDiff,
        int flags)

我从 here 那里读到了这个函数的用法。文档说

Operation mask that should be a single-channel 8-bit image, 2 pixels wider and 2 pixels taller than image.

Note: Since the mask is larger than the filled image, a pixel (x, y) in image corresponds to the pixel (x+1, y+1) in the mask.

但我认为,如果我想访问 (x,y),我会使用 (x+2,y+2)。我错了吗?

您应该相信文档! (好吧,大多数时候......:-)

Mask 比输入图像宽和高 2 个像素,但是图像的 top/left 上使用了一个像素条纹,图像的 bottom/right 上使用了另一个像素条纹。

正如您所说的那样,在填充过程中,所有 inputImage 坐标都通过求和 (1,1) 转换为蒙版坐标。

例如,当到达位于坐标 x = 0, y = y0 的图像左边缘的任意点时,算法将在掩码中检查以坐标 x = 1, y = y0 + 1 为中心的 8 个点,其中包括x = 0, y = y0 + 1 点(紧靠左侧)。如果你不加1,你就会退出图像边界。

当到达位于坐标 x = inputImage.cols -1, y = y0 的图像的右边缘时,算法将在掩码中检查以坐标 x = inputImage.cols, y = y0 + 1 为中心的 8 个点,其中包括点 x = inputImage.cols + 1, y = y0 + 1(紧靠右边)。如果您的蒙版不比原始图像宽 2 个像素,您将退出图像边界。

这就是为什么您需要一个比输入图像宽 2 个像素的蒙版图像,同时对 (1,1) 求和以从图像坐标转换为蒙版坐标。

所有这些考虑对于 y 坐标当然是有效的,以解释为什么掩码必须比输入图像高 2 个像素。

注意:如果您使用 4 个邻域而不是 8 个邻域,注意事项是相同的。