Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching images using Histogram comparison c++

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;
    }
}

}

like image 537
user2919740 Avatar asked Jan 31 '26 12:01

user2919740


1 Answers

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:

  1. find, isolate and crop the character to be identified
  2. Scale it to a predetermined horizontal and vertical size to normalize it.
  3. Sweep a directional edge detector over the character, something simple like a Sobel edge detector.
    • First apply a Horizontal edge detector, then horizontally "flatten" the edge-map to obtain a vector representing the edge information at each pixel row. (i.e. count the 1, and 0 in the edge-map for each pixel row)
    • Second apply a Vertical edge detector, and vertically flatten the edge-map giving another vector that represents the edge information at each pixel column. (i.e. cumulate and count the edge information for each pixel column)
  4. Concatenate these two vectors [horizontal-edge information, vertical-edge information]
  5. Then compare the final concatenated vector to a library of precomputed vectors of your known test samples (0-9, +/*-)

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.

like image 186
rgrou Avatar answered Feb 02 '26 02:02

rgrou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!