我有一个基本图像(一个数字或一个运算符),我必须将其与 14 个图像(0 到 9 和 * / - +)进行比较才能知道哪一个与基本图像匹配。
我创建了基础图像的直方图,并使用我为所有 14 个图像的直方图创建的循环,并且直方图被标准化。
在循环中,对于每个新创建的直方图,我使用 compareHist() 函数与基本直方图进行比较。并输出结果的双精度值。
使用相关或卡方或交集或 Bhattacharyya 方法:
我得到了一组特定的值。当使用不同的 base 时,我仍然得到相同的一组值。
为什么我会得到这个?我是否需要更改 normalize 函数以获得不同碱基的不同值?
代码:
void matchHistogram(){
Mat src_base, hsv_base;
Mat src_test1, hsv_test1;
/// Histograms
MatND hist_base;
MatND hist_test1;
/// Using 30 bins for hue and 32 for saturation
int h_bins = 30; int s_bins = 32;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 255, saturation from 0 to 180
float h_ranges[] = { 0, 255 };
float s_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
for(int i=0;i<noOfcropped;i++){ //get base image //noOfCropped is number of base images i'll compare to 14 images
cout<<" "<<i<<endl;
stringstream croppedimage;
croppedimage<<"CroppedImages/croppedImage"<<i;
croppedimage<<".jpg";
src_base = imread( croppedimage.str(), 1 );
imshow(croppedimage.str(),src_base);
/// Convert to HSV
cvtColor( src_base, hsv_base, CV_BGR2HSV );
/// Calculate the histogram for the HSV images
calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges);
normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );
for(int j=0;j<14;j++){//comparing 1 croppedimage with each different characters
cout<<" "<<j<<endl;
stringstream test1;
test1<<"ImagesToCompare/"<<j;
test1<<".jpg";
src_test1 = imread(test1.str(), 1 );
/// Convert to HSV
cvtColor( src_test1, hsv_test1, CV_BGR2HSV );
/// Calculate the histogram for the HSV images
calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges);
normalize( hist_test1, hist_test1, 0, 1,NORM_MINMAX, -1, Mat() );
/// Apply the histogram comparison methods
int compare_method = 0;
//when 0 or 2, highest comparison values>> best match
//when 1 or 3, lowest comparison values>> best match
double base_test1 = compareHist( hist_base, hist_test1, compare_method );
cout<<base_test1<<endl;
}
}
}