有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何比较并发现两个直方图是否相同?

我试着比较并检查两张图片是否匹配。在安卓 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 **比较值:*2159.2307 **比较值:*3420.9038

我不明白这些价值观意味着什么。有人能告诉我这些值是什么意思,以及我怎样才能知道这些图像是否相似。 另外,这些值不应该在0和1之间,其中1是最高匹配项,0是最低匹配项吗

我是OpenCV的新手,所以请帮我解决这个问题。如果我比较错了,请告诉我正确的方法


共 (1) 个答案

  1. # 1 楼答案

    首先你的问题是这个数字是什么意思。首先找到柱状图。然后将其正常化。然后比较两个标准化直方图的差异。见下图。如果不进行规格化,则表示强度级别的像素总数。标准化是指在一个颜色级别上找到特定像素的概率。 所以你要做的是比较两个PDF的总差异

    enter image description here

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

    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