【问题标题】:Get absolute difference from a picture and template. [Template matching]从图片和模板中获得绝对差异。 【模板匹配】
【发布时间】:2014-11-24 11:41:40
【问题描述】:

我有两种方法应该将模板的绝对差的像素值和原始图片中相同大小的补丁写入补丁和模板坐标均为 0,0 的像素中。

这是我的两种方法。

private void normalizeAndDraw(double biggest, double[] temporaryPixels, int[] dstPixels  ){
         double normalize = 255 / biggest;
            for (int c = 0; c < temporaryPixels.length; c++) {
            int value = (int) (temporaryPixels[c] * normalize);
            dstPixels[c] = 0xFF000000 | (value << 16) | (value << 8) | value;
        }
    }

    private void getAbsolutePicture(int srcPixels[], int srcWidth, int srcHeight, int dstPixels[], int dstWidth, int dstHeight, int templatePixels[], int tmpHeight, int tmpWidth) {
        double temporaryPixels[] = new double[dstHeight * dstWidth];
        double biggest = 0;

        double sumR = 0;
        for (int j = 0; j < tmpHeight; j++) {
            for (int i = 0; i < tmpWidth; i++) {

                int posTmp = j * tmpWidth + i;
                sumR += templatePixels[posTmp] & 0xFF;
            }
        }

        for (int y = 0; y < dstHeight; y++) {
            for (int x = 0; x < dstWidth; x++) {
                double sumI = 0;
                for (int j = 0; j < tmpHeight; j++) {
                    for (int i = 0; i < tmpWidth; i++) {
                        int pos = (y + j) * dstWidth + (x + i);
                        sumI += srcPixels[pos] & 0xFF;
                    }
                }
                double absDifference = Math.abs(sumI - sumR);

                biggest = Math.max(absDifference, biggest);

                temporaryPixels[y * dstWidth + x] = absDifference;
            }
        }

         normalizeAndDraw(biggest, temporaryPixels, dstPixels);

    }

他们被这样称呼。

getAbsolutePicture(srcPixels, srcWidth, srcHeight, dstPixels, dstWidth, dstHeight, templatePixels, templateWidth, templateHeight);

如果将值写入 dstPixels 数组,它们将自动显示。

不幸的是,而不是看起来像这样的正确解决方案

http://i.stack.imgur.com/cqlD3.png

我得到一个看起来像这样的结果

http://i.stack.imgur.com/2Cjhz.png

我很确定我的错误在于 sumR 和 sumI 的计算,但我就是想不通?

我的代码到底出了什么问题?

【问题讨论】:

    标签: java image-processing template-matching


    【解决方案1】:

    我的代码实际上没问题。最大的问题是当我调用 getAbsolutePicture() 时,我混淆了 tmpWdith 和 tmpHeight。

    【讨论】:

      猜你喜欢
      • 2014-02-05
      • 2014-04-05
      • 2017-10-19
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多