为什么积分图像包含额外的零行和列? [英] why the integral-image contains extra row and column of zeros?

查看:151
本文介绍了为什么积分图像包含额外的零行和列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用opencv和Java API来使用积分图像,我创建了一个测试,在使用积分图像之前和使用之后显示灰度图像。灰度图像为10 x 10,当我将其转换为积分图像时,
i发现它是11 x 11,额外的零行和额外的零列,如下图所示。

I am learning how to use the integral-images using opencv with Java API, and i created a test that displays the grayscale image before using the integral-image and after using it. the grayscale image is 10 x 10, and when i converted it to the integral-image i found it 11 x 11 with extra rows of zeros and extra column of zeros as shown below in the output.

请告诉我为什么积分图片包含额外的零行和零列?

please let me know why the integral-image contains extra row and column of zeros?

代码

    public static void main(String[] args) {
    MatFactory matFactory = new MatFactory();
    FilePathUtils.addInputPath(path_Obj);
    Mat bgrMat = matFactory.newMat(FilePathUtils.getInputFileFullPathList().get(0));
    Mat gsImg = SysUtils.rgbToGrayScaleMat(bgrMat);

    Log.D(TAG, "MainClas", "gsImg.dump(): " + gsImg.dump());
    Mat integralMat = new Mat();
    Imgproc.integral(gsImg, integralMat, CvType.CV_32F);

    Log.D(TAG, "MainClas", "sumMat.dump(): " + integralMat.dump());
}

OutPut

    1: Debug: MainClass -> MainClas: gsImg.dump(): [2, 1, 7, 5, 1, 11, 2, 7, 9, 11;
        1, 2, 0, 0, 3, 20, 17, 5, 7, 8;
        4, 8, 0, 2, 6, 30, 31, 5, 2, 2;
        39, 43, 47, 44, 38, 62, 60, 37, 37, 39;
        27, 29, 52, 52, 47, 75, 67, 59, 58, 63;
        25, 21, 49, 51, 51, 78, 64, 66, 76, 80;
        40, 36, 50, 46, 41, 56, 42, 45, 47, 49;
        13, 17, 20, 15, 9, 20, 15, 19, 12, 11;
        17, 13, 8, 5, 4, 7, 13, 20, 17, 17;
        2, 4, 7, 9, 8, 6, 6, 7, 7, 8]
    2: Debug: MainClass -> MainClas: sumMat.dump(): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
        0, 2, 3, 10, 15, 16, 27, 29, 36, 45, 56;
        0, 3, 6, 13, 18, 22, 53, 72, 84, 100, 119;
        0, 7, 18, 25, 32, 42, 103, 153, 170, 188, 209;
        0, 46, 100, 154, 205, 253, 376, 486, 540, 595, 655;
        0, 73, 156, 262, 365, 460, 658, 835, 948, 1061, 1184;
        0, 98, 202, 357, 511, 657, 933, 1174, 1353, 1542, 1745;
        0, 138, 278, 483, 683, 870, 1202, 1485, 1709, 1945, 2197;
        0, 151, 308, 533, 748, 944, 1296, 1594, 1837, 2085, 2348;
        0, 168, 338, 571, 791, 991, 1350, 1661, 1924, 2189, 2469;
        0, 170, 344, 584, 813, 1021, 1386, 1703, 1973, 2245, 2533]


推荐答案

有两个原因。

第一个是纯数学的。假设您有一行3个数字(像素)。它产生多少可能的累积总和?答案是4.你可以得到0个第一个像素,1个像素,2个像素或所有3个像素的总和。不同总和的数量是4:(0,1,2,3)。 4恰好是3 + 1。等效的宽度为10的图像将为每行产生11个总和,10x10的大小将产生11x11的总和。

First one is purely mathematical. Say you have a row of 3 numbers (pixels). How many possible cumulative sums it generates? the answer is 4. You can take the sum of 0 first pixels, 1 pixel, 2 pixels or all the 3 pixels. The amount of different sums is 4: (0,1,2,3). 4 is exactly 3+1. Equivalently image of width 10 will yield 11 sums for each row and size 10x10 will yield 11x11 sums.

第二个原因是编程简单。积分图像用于计算图像中任何可能的矩形的总和,仅有4个动作(2个角的总和减去其他2个角)。角之间的距离恰好等于您想要求和的矩形的大小。例如,如果矩形的宽度为5像素,则在索引 im [i] [j] im [i]中访问积分图像J + 5] 。但是,如果您的矩形覆盖整个图像的宽度或高度,这可能会产生一个落在数组之外的索引1.这就是整数图像的存储大小比图像大1x1的原因

The second reason is for programming simplicity. Integral image is used to calculate sum of any possible rectangle in the image with just 4 actions (sum of 2 corners minus the 2 other corners). The distance between the corners is exactly equals to the size of the rectangle you want to sum. For example if your rectangle has width of 5 pixels than you access the integral image at indices im[i][j] and im[i][j+5]. However if your rectangle covers the entire image width or height this may produce an index that falls out of array by 1. That is why the integral image is stored in a size that is by 1x1 larger than the image

注意:可以将整数图像存储在与图像大小相同的数组中。但是对数组的访问速度要慢得多,因为需要测试索引是否超出范围。必须检测索引[-1]处的积分图像并产生0的总和,并且在索引>宽度处访问将自动返回整个宽度的总和。

Note: it is possible to store the integral image in an array of the same size as the image. But then the access to the array will be much slower because one will need to test the indices for out of bound. Accessing the integral image at index [-1] must be detected and produce sum of 0, and accessing at index > width will automatically return sum of the entire width.

OpenCV 主要由于速度原因实现了较大的积分图像。矩形之和的计算仅需要4 +或 - 操作和4指针参考。只要请求的矩形在图像内部具有合法坐标,就不需要测试指针落在图像内部。

OpenCV implemented the larger integral images mainly due to speed reasons. The calculation of the sum of rectangle requires only 4 + or - operation and 4 pointers deference. No need to test that pointers fall inside the image as long as the requested rectangle has legal coordinates inside the image

有些架构允许访问数组越界(非法)指数)。例如GPU着色器。在这些架构上,积分图像可以以不同的方式实现(NxN的大小,而不是N + 1xN + 1,甚至是金字塔的总和)

There are architectures that allow accessing array out of bounds (at illegal indices). For example GPU shaders. On those architectures integral image can be implemented in different fashion (size of NxN instead N+1xN+1 or even as pyramid of sums)

你能手动删除吗? openCV中积分图像中的额外列?

我强烈建议不要这样做! openCV具有内置代码,以特定方式访问整合图像。如果删除第一列,可能会导致不可预测的计算。

I strongly do not recommend doing so! openCV has a built in code to access the integral image in a specific way. If you remove the first column you will probably cause unpredictable calculations.

此外 - 正如我所解释的那样,这个额外的行和列将运行时间增加了x10倍(因为加速和减法由CPU执行得更快比if()条件)

Moreover - as I explained, this additional row and column increase the running time by a factor of up to x10 faster (since addition and subtraction are performed by the CPU much faster than if() conditions)

请注意,积分图像是原始图像的完全不同的表示。它不仅具有不同的尺寸(N + 1)x(N + 1),而且具有不同的深度。您的原始图像可以是灰度(每像素存储一个字节),而积分图像通常需要每像素4个字节(因为许多像素的总和需要更大的数字)。因此,无论如何,积分图像将比原始图像多大约4倍的内存。由于不同的BPP(每像素位数),您无法在原始图像中显示积分图像,因此为什么会被不同的宽度和高度所困扰

Please note that integral image is a completely different representation of the original image. Not only it has a different size (N+1)x(N+1) but also a different depth. Your original image can be grayscale (store one byte per pixel) while integral image will need typically 4 bytes per pixel (because summation of many pixels require much larger numbers). So anyway integral image will take ~4 times more memory than the original image. You cannot fit integral image in the original image due to different BPP (bits per pixels) so why be bothered by different width and height

这篇关于为什么积分图像包含额外的零行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆