I have a base image(a number or an operator) which i have to compare with 14 images(0 to 9 and * / - +) to know which one matches with the base image.
I created the base image's histogram and using a loop I create for all the 14 images' histograms and the histograms were normalized.
In the loop, for each newly created histogram i compared with the base histogram using the compareHist() function. And output the resulted double value.
Using Correlation or Chi-square or Intersection or Bhattacharyya method:
I am getting a particular set of values. And when using a different base, I am still getting that same set of values.
Why am I getting this? Do I need to alter the normalize function to get distinct values for different bases?
CODE:
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;
}
}
}
If I understand your question correctly, you isolate and crop a character from some bitmap-like image and you then want to identify what character it is? Like an automatic character recognition?
Perhaps you could use an edge detector instead of comparing histograms?
I would try an algorithm something like:
Numbers that look somewhat similar (8,6,9,3) should still have distinct peaks and valleys either in the horizontal component or the vertical component.