繁体   English   中英

如何比较并找出两个直方图是否相同?

[英]How can I compare and find if two histograms are the same?

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

然而,我通过比较值添加的三个吐司得到的结果是,

  • ** comparisonValueB:** 1984.5519 ** comparisonValueG:** 2159.2307 ** comparisonValueR:** 3420.9038

我不明白这些价值意味着什么。 有人能让我知道这些价值意味着什么,以及如何判断图像是否相似。 也不是假设值介于0和1之间,其中1是最高匹配,零是最低匹配?

我是OpenCV的新手,所以请帮帮我。 如果我比较错误,请告诉我一个正确的方法。

首先你的问题是数字意味着什么。 你首先找到直方图。 然后将其标准化。 然后比较两个标准化直方图的差异。 见下图。 没有标准化,它表示强度级别的像素总数。 通过归一化,它是在一个颜色级别找到特定像素的概率。 所以你要做的是比较两个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)

在此输入图像描述 在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM