Bhattacharyya distance 巴氏距离
(1)相关性计算(CV_COMP_CORREL)
(2)卡方计算(CV_COMP_CHISQR)
(3)十字计算(CV_COMP_INTERSECT)
(4)巴氏距离计算(CV_COMP_BHATTACHARYYA )
相关操作流程:
API介绍:
compareHist(
InputArray h1, // 直方图数据1
InputArray H2,//直方图数据2
int method// 比较方法,上述四种方法之一
)//返回一个double类型的数据。
以下是代码演示:
#include
#include
#include
using namespace std;
using namespace cv;
//由于opencv不支持double类型到string类型转换,所以借助这个函数进行实现。
string convertToString(double d);
int main(int argc, char** argv) {
Mat base, test1, test2;
Mat hsvbase, hsvtest1, hsvtest2;
base = imread("D:/photo/4.jpg");
if (!base.data) {
printf("could not load image...\n");
return -1;
}
test1 = imread("D:/photo/8.jpg");
test2 = imread("D:/photo/9.jpg");
//图像转换到HSV空间进行比较
cvtColor(base, hsvbase, CV_BGR2HSV);
cvtColor(test1, hsvtest1, CV_BGR2HSV);
cvtColor(test2, hsvtest2, CV_BGR2HSV);
//设定直方图需要相关参数
int h_bins = 50; int s_bins = 60; int v_bins = 60;
int histSize[] = { h_bins, s_bins, v_bins};
// hue varies from 0 to 179, saturation from 0 to 255
float h_ranges[] = { 0, 180 }; //色调H用角度度量,取值范围为0°~360°,从红色开始按逆时针方向计算,红色为0°,绿色为120°,蓝色为240°。它们的补色是:黄色为60°,青色为180°,品红为300°;
float s_ranges[] = { 0, 256 };
float v_ranges[] = { 0, 256 };
const float* ranges[] = { h_ranges, s_ranges, v_ranges};
// Use the o-th and 1-st channels
int channels[] = {0 , 1};//比较H和S通道直方图,由于函数只能最多比较2维直方图,所以需要进行选择。
//int channels[] = { 1, 2};//比较S和V通道直方图
//int channels[] = {0};//只比较第一个H通道的直方图选择。
//新建多维度Mat类型变量
MatND hist_base;
MatND hist_test1;
MatND hist_test2;
//进行直方图提取以及归一化计算
calcHist(&hsvbase, 1, channels, Mat(), hist_base, 1, histSize, ranges, true, false);
normalize(hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvtest1, 1, channels, Mat(), hist_test1, 1, histSize, ranges, true, false);
normalize(hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvtest2, 1, channels, Mat(), hist_test2, 1, histSize, ranges, true, false);
normalize(hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat());
//进行直方图比较,分别列出了几个不同的类型
double basebase = compareHist(hist_base, hist_base, 0);
double basetest1 = compareHist(hist_base, hist_test1, 1);
double basetest2 = compareHist(hist_base, hist_test2, 2);
double tes1test2 = compareHist(hist_test1, hist_test2, 3);
printf("test1 compare with test2 correlation value :%f", tes1test2);
//设置图像进行相关显示
Mat test12;
test2.copyTo(test12);
putText(base, convertToString(basebase), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, CV_AA);
putText(test1, convertToString(basetest1), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, CV_AA);
putText(test2, convertToString(basetest2), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, CV_AA);
putText(test12, convertToString(tes1test2), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, CV_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)
return os.str();
return "invalid conversion";
}
相关结果显示:
(1)只计算H通道的情况:
(2)计算H和S双通道直方图结果:
(3)计算S和V双通道结果: