结果compareHist在openCV [英] results compareHist in openCV
问题描述
我试图比较两个我存储为数组的直方图。我是新的与c + +接口(cv :: Mat)和计算直方图在OpenCV。
I'm trying to compare two histograms which I stored as an array. I'm new with the c++ interface (cv::Mat) and calculating histograms in OpenCV.
我的代码:
int testArr1[4] = {12, 10, 11, 11};
int testArr2[4] = {12, 0, 11, 0};
cv::Mat M1 = cv::Mat(1,4,CV_8UC1, testArr1);
cv::Mat M2 = cv::Mat(1,4,CV_8UC1, testArr2);
int histSize = 4;
float range[] = {0, 20};
const float* histRange = {range};
bool uniform = true;
bool accumulate = false;
cv::Mat a1_hist, a2_hist;
cv::calcHist(&M1, 1, 0, cv::Mat(), a1_hist, 1, &histSize, &histRange, uniform, accumulate );
cv::calcHist(&M2, 1, 0, cv::Mat(), a2_hist, 1, &histSize, &histRange, uniform, accumulate );
double compar_c = cv::compareHist(a1_hist, a2_hist, CV_COMP_CORREL);
double compar_chi = cv::compareHist(a1_hist, a2_hist, CV_COMP_CHISQR);
double compar_bh = cv::compareHist(a1_hist, a2_hist, CV_COMP_BHATTACHARYYA);
double compar_i = cv::compareHist(a1_hist, a2_hist, CV_COMP_INTERSECT);
cout << "compare(CV_COMP_CORREL): " << compar_c << "\n";
cout << "compare(CV_COMP_CHISQR): " << compar_chi << "\n";
cout << "compare(CV_COMP_BHATTACHARYYA): " << compar_bh << "\n";
cout << "compare(CV_COMP_INTERSECT): " << compar_i << "\n";
结果有点意外:
compare(CV_COMP_CORREL): 1
compare(CV_COMP_CHISQR): 0
compare(CV_COMP_BHATTACHARYYA): 0
compare(CV_COMP_INTERSECT): 4
对于交集,例如,我预计像0.5。我做错了什么?我可以不把数组在cv :: mat?或者我选择了错误的直方图设置?
For intersection, for example, I expected something like 0.5. What am I doing wrong? Can I not put arrays in a cv::mat? Or did I choose the wrong histogram "settings"?
推荐答案
问题是你的前4行,的整数到字符矩阵。构造函数假定为一个char数组,因此无法正确读取值。您的矩阵M1和M2不包含正确的值。
The problem are your first 4 lines where you are converting the c array of integers to a matrix of chars. The constructor assumes a char array and therefore can't read the values properly. Your matrices M1 and M2 don't contain the correct values.
但是如果您更改以下行,以便数组的类型匹配矩阵的类型:
But if you change the following lines, so that the type of the array matches the type of the matrix:
char testArr1[4] = {12, 10, 11, 11};
char testArr2[4] = {12, 0, 11, 0};
我从程序中获得以下输出:
I get the following output from your program:
compare(CV_COMP_CORREL): 0.57735
compare(CV_COMP_CHISQR): 2.66667
compare(CV_COMP_BHATTACHARYYA): 0.541196
compare(CV_COMP_INTERSECT): 2
这篇关于结果compareHist在openCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!