JavaCv compares 2 histograms

I am trying to compare two histograms with grayscale images. I am using CV_COMP_CHISQR (0.0 perfect match - 1.0 general mismatch). I normalized both histograms to 1. But when I compare the histograms, I get a result of more than 40.0, which makes no sense. I don’t know, maybe I missed some step or maybe something is wrong. Here is the code binding.

public class Histogram { public static void main(String[] args) throws Exception { String baseFilename = ".../imgs/lp.jpg"; String contrastFilename = ".../imgs/lpUrb.jpg";c/surf_javacv/box_in_scene.png"; IplImage baseImage = cvLoadImage(baseFilename); CvHistogram hist=getHueHistogram(baseImage); IplImage contrastImage = cvLoadImage(contrastFilename); CvHistogram hist1=getHueHistogram(contrastImage); double matchValue=cvCompareHist(hist, hist1, CV_COMP_CHISQR ); System.out.println(matchValue); } private static CvHistogram getHueHistogram(IplImage image){ if(image==null || image.nChannels()<1) new Exception("Error!"); IplImage greyImage= cvCreateImage(image.cvSize(), image.depth(), 1); cvCvtColor(image, greyImage, CV_RGB2GRAY); //bins and value-range int numberOfBins=256; float minRange= 0f; float maxRange= 255f; // Allocate histogram object int dims = 1; int[]sizes = new int[]{numberOfBins}; int histType = CV_HIST_ARRAY; float[] minMax = new float[]{minRange, maxRange}; float[][] ranges = new float[][]{minMax}; int uniform = 1; CvHistogram hist = cvCreateHist(dims, sizes, histType, ranges, uniform); // Compute histogram int accumulate = 0; IplImage mask = null; IplImage[] aux = new IplImage[]{greyImage}; cvCalcHist(aux,hist, accumulate, null); cvNormalizeHist(hist, 1); cvGetMinMaxHistValue(hist, minMax, minMax, sizes, sizes); System.out.println("Min="+minMax[0]); //Less than 0.01 System.out.println("Max="+minMax[1]); //255 return hist; } }//CLass end 
+4
source share
1 answer

From the book "Learnig OpenCV":

β€œFor a chi-square, a low score represents a better match than a high score. The perfect match is 0, and the total mismatch is unlimited (depending on the size of the histogram).

If you want your result to be in the [0,1] segment, use the = CV_COMP_INTERSECT method, where high scores show good matches and low scores indicate bad matches. If both histograms are normalized to 1, then the perfect match is 1, and the complete mismatch is 0.

+1
source

All Articles