我正在尝试比较灰度图像中的两个直方图.
我正在使用CV_COMP_CHISQR(0.0完美匹配 – 1.0总不匹配).我将两个直方图归一化为1.
但是当我比较直方图时,我得到的结果超过了40.0,这没有任何意义.
我不知道是否可能错过了一些步骤或者可能出现了问题.
这是代码的快照.
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
解决方法:
从书籍“Learnig OpenCV”:
“对于卡方,低分表示比高分更好的匹配.完美匹配为0,总不匹配*限(取决于直方图的大小).”
如果您需要将结果放在段[0,1]中使用method = CV_COMP_INTERSECT,其中高分表示匹配良好,低分表示不匹配.如果两个直方图都归一化为1,则完美匹配为1,总不匹配为0.