对输入的两张图像计算得到直方图H1与H2,归一化到相同的尺度空间
然后可以通过计算H1与H2的之间的距离得到两个直方图的相似程度进
而比较图像本身的相似程度。Opencv提供的比较方法有四种:
计算结果越接近0,两个直方图越相似
H1,H2分别表示两个图像的直方图数据
计算公式为取两个直方图每个相同位置的值的最小值,然后求和,这个比较方式不是很好,不建议使用
H1,H2分别表示两个图像的直方图数据
比较结果是很准的,计算结果范围为 0-1 ,0表示两个直方图非常相关,1最不相似
H1,H2分别表示两个图像的直方图数据
compareHist(
InputArray h1, // 直方图数据,下同
InputArray H2,
int method// 比较方法,上述四种方法之一
)
#include
#include
#include
using namespace std;
using namespace cv;
string convertToString(double d);
int main(int argc, char** argv) {
// 1. 声明储存基准图像和另外两张对比图像的矩阵( RGB 和 HSV )
Mat base, test1, test2;
Mat hsvbase, hsvtest1, hsvtest2;
// 2. 装载基准图像(src_base) 和两张测试图像:
base = imread("E:/Experiment/OpenCV/Pictures/lenanoise.jpg");
if (!base.data) {
printf("could not load image...\n");
return -1;
}
test1 = imread("E:/Experiment/OpenCV/Pictures/lena.jpg");
test2 = imread("E:/Experiment/OpenCV/Pictures/lena1.jpg");
// 3. 将图像转化到HSV格式:
cvtColor(base, hsvbase, CV_BGR2HSV);//转换为 HSV 颜色空间,也是3通道
cvtColor(test1, hsvtest1, CV_BGR2HSV);
cvtColor(test2, hsvtest2, CV_BGR2HSV);
// 4. 初始化计算直方图需要的实参(bins, 范围,通道 H 和 S ).
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 179, saturation from 0 to 255
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 256 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
// 5. 创建储存直方图的 MatND 实例:
//ND 表示二维或多维的Mat ,typedef Mat MatND; 源码中MatND的声明。 Mat表示二维的数组
MatND hist_base;
MatND hist_test1;
MatND hist_test2;
// 6. 计算基准图像,两张测试图像
calcHist(&hsvbase, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false);
normalize(hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat());//归一化到 0-1 之间
calcHist(&hsvtest1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false);
normalize(hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvtest2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false);
normalize(hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat());
// 7. 使用4种对比标准将基准图像(hist_base)的直方图与其余各直方图进行对比:
//比较的算法 CV_COMP_CORREL CHISQR INTERSECT BHATTACHARYYA
double basebase = compareHist(hist_base, hist_base, CV_COMP_CORREL);
double basetest1 = compareHist(hist_base, hist_test1, CV_COMP_CORREL);
double basetest2 = compareHist(hist_base, hist_test2, CV_COMP_CORREL);
double tes1test2 = compareHist(hist_test1, hist_test2, CV_COMP_CORREL);
printf("test1 compare with test2 correlation value :%f", tes1test2);
Mat test12;
test2.copyTo(test12);
// 8. 将比较的结果转换为string,然后以文字的方式绘制到图形上
putText(base, convertToString(basebase), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
putText(test1, convertToString(basetest1), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
putText(test2, convertToString(basetest2), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
putText(test12, convertToString(tes1test2), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
namedWindow("base", CV_WINDOW_AUTOSIZE);
namedWindow("test1", CV_WINDOW_AUTOSIZE);
namedWindow("test2", CV_WINDOW_AUTOSIZE);
imshow("base", base);
imshow("test1", test1);
imshow("test2", test2);
imshow("test12", test12);
waitKey(0);
return 0;
}
string convertToString(double d) {
ostringstream os;
if (os << d) // << 运算符重载了,将double转成string
return os.str();
return "invalid conversion";
}
- https://blog.csdn.net/huanghuangjin/article/details/81175339
- https://blog.csdn.net/LYKymy/article/details/83210430