4

I’m trying to compare two histograms from grayscale images. I’m using the CV_COMP_CHISQR (0.0 perfect match – 1.0 total mismatch). I normalized both histograms to 1. But when I compare the histograms I get result over 40.0 which make no sense. I don´t know if maybe I missing some step or maybe something is wrong. Here is a snap of the code.

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
Du_
  • 915
  • 1
  • 9
  • 16

1 Answers1

1

From book "Learnig OpenCV":

"For chi-square a low score represents a better match than a high score. A perfect match is 0 and a total mismatch is unbounded (depending on the size of the histogram)."

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

Ann Orlova
  • 1,338
  • 15
  • 24