概述
得到图像的直方图之后,通常要用某些具体的标准来比较两个直方图的相似度。要对两个直方图进行比较,首先必须选择一个衡量直方图相似度的对比标准。OpenCV中用compareHsit()函数来比较两个直方图的相似度。
对比直方图:compareHsit()函数
compareHsit()函数用于对两幅直方图进行比较,有两个版本,函数原型:
double compareHist(InputArray H1, InputArray H2, int mathod)
double compareHist(const SparseMat& H1, const SparseMat& H2)
它们的前两个参数是要比较的直方图,第三个是要选择的距离标准,可以选择的有4种方法:
相关,Correlation(method=CV_COMP_CORREL)
卡方,Chi-Square(method=CV_COMP_CHISQR)
直方图相交,intersection(method=CV_COMP_INTERSECT)
Bhattacharyya距离(method=CV_COMP_BAHATTACHARYYA)
代码示例:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
Mat srcImage_base, hsvImage_base;
Mat srcImage_test1, hsvImage_test1;
Mat srcImage_test2, hsvImage_test2;
Mat hsvImage_halfDown;
srcImage_base = imread("/Users/dwz/Desktop/cpp/mogu.jpg", 1);
srcImage_test1 = imread("/Users/dwz/Desktop/cpp/rain.jpg", 1);
srcImage_test2 = imread("/Users/dwz/Desktop/cpp/1.jpg", 1);
cvtColor(srcImage_base, hsvImage_base, COLOR_BGR2HSV);
cvtColor(srcImage_test1, hsvImage_test1, COLOR_BGR2HSV);
cvtColor(srcImage_test2, hsvImage_test2, COLOR_BGR2HSV);
hsvImage_halfDown = hsvImage_base(Range(hsvImage_base.rows/2, hsvImage_base.rows-1), Range(0, hsvImage_base.cols-1));
int h_bins = 50, s_bins = 60;
int histSize[] = {h_bins, s_bins};
float h_ranges[] = {0, 256};
float s_ranges[] = {0, 180};
const float* ranges[] = {h_ranges, s_ranges};
int channels[] = {0, 1};
MatND baseHist;
MatND halfDownHist;
MatND testHist1;
MatND testHist2;
calcHist(&hsvImage_base, 1, channels, Mat(), baseHist, 2, histSize, ranges, true, false);
normalize(baseHist, baseHist, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvImage_halfDown, 1, channels, Mat(), halfDownHist, 2, histSize, ranges, true, false);
normalize(halfDownHist, halfDownHist, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvImage_test1, 1, channels, Mat(), testHist1, 2, histSize, ranges, true, false);
normalize(testHist1, testHist1, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvImage_test2, 1, channels, Mat(), testHist2, 2, histSize, ranges, true, false);
normalize(testHist2, testHist2, 0, 1, NORM_MINMAX, -1, Mat());
for (int i=0; i< 4; i++)
{
int compare_method = i;
double base_base = compareHist(baseHist, baseHist, compare_method);
double base_half = compareHist(baseHist, halfDownHist, compare_method);
double base_test1 = compareHist(baseHist, testHist1, compare_method);
double base_test2 = compareHist(baseHist, testHist2, compare_method);
printf("方法[%d]的匹配结果:nn【基准图-基准图】:%f,【基准图-半身图】:%f,【基准图-测试图1】:%f,【基准图-测试图2】:%fn----------------------n",
i, base_base, base_half, base_test1, base_test2);
}
return 0;
}
输入:
输出:
最后
以上就是发嗲薯片为你收集整理的OpenCV之直方图对比的全部内容,希望文章能够帮你解决OpenCV之直方图对比所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复