对比直方图:compareHist 函数
double compareHist(InputArray H1, InputArray H2, int method);
- H1,H2,是要进行比较的直方图。
- method,比较方法。有如下选择:
方法名 | 标识符 | 计算公式 |
相关 Correlation |
HISTCMP_CORREL
|
其中 是直方图中bin的数目。 |
卡方 Chi-square |
HISTCMP_CHISQR
|
|
相交 Intersection |
HISTCMP_INTERSECT |
|
巴氏距离 Bhattacharyya |
HISTCMP_BHATTACHARYYA
|
代码示例:
#include<opencv.hpp>
#include<iostream>
#include<string>
using namespace std;
using namespace cv;
int main() {
Mat src1 = imread("C:/Users/齐明洋/Desktop/证件照/6.jpg");
Mat src2 = imread("C:/Users/齐明洋/Desktop/证件照/8.jpg");
Mat src3 = imread("C:/Users/齐明洋/Desktop/证件照/10.jpg");
imshow("src1", src1);
imshow("src2", src2);
imshow("src3", src3);
//将图片转换成 HSV 类型
//对 HS 两通道进行直方图统计
cvtColor(src1, src1, COLOR_BGR2HSV);
cvtColor(src2, src2, COLOR_BGR2HSV);
cvtColor(src3, src3, COLOR_BGR2HSV);
int channels[] = { 0,1 };
int histsize[] = { 180,255 };
float r1[] = { 0,180 };
float r2[] = { 0,255 };
const float *ranges[] = { r1,r2 };
Mat hist1, hist2, hist3;
calcHist(&src1, 3, channels, Mat(), hist1, 2, histsize, ranges, true);
//https://www.cnblogs.com/bjxqmy/p/12292421.html
normalize(hist1, hist1, 1, 0, NORM_L1);
calcHist(&src2, 3, channels, Mat(), hist2, 2, histsize, ranges, true);
normalize(hist2, hist2, 1, 0, NORM_L1);
calcHist(&src3, 3, channels, Mat(), hist3, 2, histsize, ranges, true);
normalize(hist3, hist3, 1, 0, NORM_L1);
int method[] = { HISTCMP_CORREL,HISTCMP_CHISQR,HISTCMP_INTERSECT,HISTCMP_BHATTACHARYYA };
string names[] = { "HISTCMP_CORREL","HISTCMP_CHISQR","HISTCMP_INTERSECT","HISTCMP_BHATTACHARYYA" };
for (int i = 0; i < 4; i++) {
cout << names[i] << ":" << endl;
cout << "src1 and src1 :" << compareHist(hist1, hist1, method[i]) << endl;
cout << "src1 and src2 :" << compareHist(hist1, hist2, method[i]) << endl;
cout << "src1 and src3 :" << compareHist(hist1, hist3, method[i]) << endl;
cout << endl;
}
waitKey(0);
}
效果演示:
借鉴博客:https://blog.csdn.net/qq_18343569/article/details/48028457