如何比较并确定两个直方图是否相同?

时间:2019-06-02 10:01:23

标签: java android opencv

我正在尝试比较并检查两个图像是否匹配。我找不到在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();

但是我从我添加的三个吐司中得到的结果与比较值是

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

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

我是OpenCV的新手,请帮助我。如果我比较错误,也请让我知道一种正确的方法。

1 个答案:

答案 0 :(得分:0)

首先,您的问题是数字是什么意思。您首先找到直方图。然后将其标准化。然后,您比较两个标准化直方图的差。参见下图。如果不进行归一化,则表示强度级别的像素总数。通过归一化,这是找到一种颜色级别的特定像素的概率。 因此,您要做的是比较两个PDF的总差。

enter image description here

第二个如果您的任务是比较并检查两个图像是否匹配。那么比较直方图可能是最糟糕的选择,请参见下面的示例。您的想法是,如果A和B之间的直方图相同,则2张图像是相同的。但是我可以向您展示,从底部看,它现在是相同的。在底部,两个直方图相同,但是图像代表不同的事物。

enter image description here

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

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)

enter image description here enter image description here