void CompareHist() { imshow("input1", image); imshow("input2", temp); Mat hsv1, hsv2; cvtColor(image, hsv1, COLOR_BGR2HSV); cvtColor(temp, hsv2, COLOR_BGR2HSV); int h_bins = 60; int s_bins = 64; int histSize[] = { h_bins, s_bins }; float h_ranges[] = { 0, 180 }; float s_ranges[] = { 0, 256 }; const float* ranges[] = { h_ranges, s_ranges }; int channels[] = { 0, 1 }; Mat hist1, hist2; calcHist(&hsv1, 1, channels, Mat(), hist1, 2, histSize, ranges, true, false); calcHist(&hsv2, 1, channels, Mat(), hist2, 2, histSize, ranges, true, false); normalize(hist1, hist1, 0, 1, NORM_MINMAX, -1, Mat()); normalize(hist2, hist2, 0, 1, NORM_MINMAX, -1, Mat()); //4 种比较方法 for (int i = 0; i < 4; i++) { int compare_method = i; double src1_src2 = compareHist(hist1, hist2, compare_method); qDebug() << " Method" << i << ": src1_src2:" << src1_src2; printf(" Method [%d] : src1_src2 : %f \n", i, src1_src2); } waitKey(0); }
void calcHist(const Mat * images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int* histSize, const float** ranges, bool uniform = true, bool accumulate = false);
images:输入的图像的指针;
nimages:输入图像个数;
channels:需要统计直方图的第几通道;
mask:掩模,mask必须是一个8位(CV_8U)的数组并且和images的数组大小相同;
hist:直方图计算的输出值;
dims:输出直方图的维度(由channels指定);
histSize:直方图中每个dims维度需要分成多少个区间(如果把直方图看作一个一个竖条的话,就是竖条的个数);
ranges:统计像素值的区间;
uniform = true:是否对得到的直方图数组进行归一化处理;
accumulate = false:在多个图像时,是否累积计算像素值的个数;
方法1:Correlation ( CV_COMP_CORREL )
相关性比较,取值范围[0,1],越接近1,直方图相似度越高
方法2:Chi-Square ( CV_COMP_CHISQR )
卡方检验,取值范围[0,1],越接近0,直方图相似度越高
方法3:Intersection ( method=CV_COMP_INTERSECT )
相交,取值高,直方图相似度越高
方法4:Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )
巴氏距离在直方图比较中效果最佳,完全匹配为1,完全不匹配为0