d=1:两幅图完全一致
d越接近1两幅图越相似
d=0:两幅图完全相同
d越小两幅图越相同
d=0:完全相等
d越接近0两幅图越相似
1,首先把图像从RGB色彩空间转换到HSV色彩空间cvtColor
2,计算图像的直方图,然后归一化到[0~1]之间calcHist和normalize;
3,使用上述四种比较方法之一进行比较compareHist
compareHist(//比较直方图相似度
InputArray h1, // 直方图数据,下同
InputArray H2,
int method// 比较方法,上述四种方法之一 )
代码如下:
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat t1,t2,t3,t1h,t2h,t3h;
t1 = imread("1.bmp");
if (!t1.data) {
printf("could not load image...\n");
return -1;
}
t2 = imread("2.bmp");
t3 = imread("5.bmp");
pyrUp(t1, t1, Size(t1.cols * 2, t1.rows * 2));
pyrUp(t1, t1, Size(t1.cols * 2, t1.rows * 2));
pyrUp(t2, t2, Size(t2.cols * 2, t2.rows * 2));
pyrUp(t2, t2, Size(t2.cols * 2, t2.rows * 2));
pyrUp(t3, t3, Size(t3.cols * 2, t3.rows * 2));
pyrUp(t3, t3, Size(t3.cols * 2, t3.rows * 2));
cvtColor(t1, t1h, CV_BGR2HSV);
cvtColor(t2, t2h, CV_BGR2HSV);
cvtColor(t3, t3h, CV_BGR2HSV);
int t10 = 32, t20 = 64;
int t11[] = { t10,t20 };
int channel[] = { 0,1 };
float f1[] = { 0,180 };
float f2[] = { 0,256 };
const float *f3[] = { f1,f2 };
Mat t1c, t2c, t3c;
calcHist(&t1h,1, channel, Mat(), t1c, 2, t11, f3, true, false);
normalize(t1c, t1c, 0, 1,NORM_MINMAX, -1);
calcHist(&t2h, 1, channel, Mat(), t2c, 2, t11, f3, true, false);
normalize(t2c, t2c, 0, 1, NORM_MINMAX, -1);
calcHist(&t3h, 1, channel, Mat(), t3c, 2, t11, f3, true, false);
normalize(t3c, t3c, 0, 1, NORM_MINMAX, -1);
double t12 = compareHist(t1c, t2c,CV_COMP_BHATTACHARYYA);
double t13 = compareHist(t1c, t3c, CV_COMP_BHATTACHARYYA);
double t110 = compareHist(t1c, t1c, CV_COMP_BHATTACHARYYA);
cout << "t12=" << t12 << " t13=" << t13 << " t11=" << t110;
namedWindow("M", 0);
waitKey(0);
return 0;
}