【问题标题】:How can I compare and find if two histograms are the same?如何比较和查找两个直方图是否相同?
【发布时间】:2019-10-18 05:17:13
【问题描述】:

我正在尝试比较并检查两张图片是否相互匹配。我在 android studio 中找不到与 OpenCV java 进行比较的合适教程。所以,我找到了一些步骤并开始计算直方图,这样我就可以比较两张图像的直方图,然后看看它们是否匹配。

我已经编写了以下方法来计算直方图,然后进行比较。

Mat matB2 = new Mat(sourceSize, sourceMat.type());
                Mat matG2 = new Mat(sourceSize, sourceMat.type());
                Mat matR2 = new Mat(sourceSize, sourceMat.type());

                    Imgproc.calcHist(channels2, allChannel2[0], new Mat(),  matB2, hisSize2, histRange2);
                    Imgproc.calcHist(channels2, allChannel2[1], new Mat(), matG2, hisSize2, histRange2);
                    Imgproc.calcHist(channels2, allChannel2[2], new Mat(), matR2, hisSize2, histRange2);

                    Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();

                    int graphHeight2 = 300;
                    int graphWidth2 = 200;
                    int binWidth2 = 3;

                    Mat graphMat2 = new Mat(graphHeight2, graphWidth2, CvType.CV_8UC3, new Scalar(0, 0, 0));

                    //Normalize channel
                    Core.normalize(matB2, matB2, graphMat2.height(), 0, Core.NORM_INF);
                    Core.normalize(matG2, matG2, graphMat2.height(), 0, Core.NORM_INF);
                    Core.normalize(matR2, matR2, graphMat2.height(), 0, Core.NORM_INF);



  //Comparing histograms
                    int compareMethod = 1;
                    double comparisonValueB = Imgproc.compareHist(matB,matB2, Imgproc.CV_COMP_CORREL);
                    double comparisonValueG = Imgproc.compareHist(matG,matG2,Imgproc.CV_COMP_CORREL);
                    double comparisonValueR = Imgproc.compareHist(matR,matR2,Imgproc.CV_COMP_CORREL);

                    Toast.makeText(MainActivity.this, "comparisonValueB::"+comparisonValueB, Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "comparisonValueG::"+comparisonValueG, Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "comparisonValueR::"+comparisonValueR, Toast.LENGTH_SHORT).show();

但是,我从添加了比较值的三个 toast 中得到的结果是,

  • **comparisonValueB:**1984.5519 **比较值G:**2159.2307 **comparisonValueR:**3420.9038

我不明白这些值的含义。有人可以让我知道这些值的含义以及如何确定图像是否相似。 值也不应该介于 0 和 1 之间,其中 1 是最高匹配,0 是最低匹配?

我对 OpenCV 很陌生,所以请帮助我。如果我比较错误,请告诉我正确的方法。

【问题讨论】:

    标签: java android opencv


    【解决方案1】:

    首先你的问题是这个数字是什么意思。您首先找到直方图。然后规范化它。然后比较两个归一化直方图的差异。见下图。没有归一化,它表示一个强度级别的像素总数。使用归一化,它是在一种颜色级别上找到特定像素的概率。 所以你正在做的是比较两个 PDF 的总差异。

    其次,如果您的任务是比较并检查两个图像是否相互匹配。那么比较直方图可能是最糟糕的选择请参见下面的示例。你的想法是,如果 A 和 B 之间的直方图相同,则 2 幅图像是相同的。但我可以告诉你,它现在与从底部看到的相同。在底部,2个直方图是相同的,但图像代表不同的东西。

    检查两个图像是否相同的最简单方法是进行模板匹配。您可以在下面的链接中找到相同的代码

    https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

    > void matchTemplate(InputArray image, InputArray templ, OutputArray
    > result, int method)
    

    【讨论】:

      猜你喜欢
      • 2023-02-04
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多