OCR recognition based on two-dimensional correlation coefficients of matrices

Source: Internet
Author: User

I want to use simple template matching for image recognition.

The pre-processed character picture is matched to a sample picture of A to J, respectively. The most significant results indicate that the character picture can be identified.

In practice, I used the OpenCV matchtemplate () function, but I didn't get the effect I wanted.

The function of Matchtemplate () is to search for the specified template in the image, if the template is intercepted from the image to be searched, it will have a good effect. But if the template isn't part of the image you're looking for, it doesn't seem to be the effect I want.

After trying the CORR2 () of Matlab, I found that corr2 can solve my problem well.



Double Card::corr2 (Mat MatA, Mat MATB) {//calculates two two-dimensional correlation coefficients for the same size matrix double corr2 = 0;double Amean2 = 0; Double Bmean2 = 0;for (int m = 0; M < mata.rows;  m++) {uchar* dataa = mata.ptr<uchar> (m); uchar* datab = matb.ptr<uchar> (m);  for (int n = 0; n < mata.cols;n++) {Amean2 = Amean2 + dataa[n];                    Bmean2 = Bmean2 + datab[n];} }amean2 = Amean2/(mata.rows * mata.cols);  Bmean2 = Bmean2/(matb.rows * matb.cols);d ouble Cov = 0;double ASTD = 0;double bstd = 0;for (int m = 0; m < mata.rows;  m++) {uchar* dataa = mata.ptr<uchar> (m); uchar* datab = matb.ptr<uchar> (m); for (int n = 0; n < mata.cols;n++) {//Covariance Cov = Cov + (Dataa[n]-Amean2) * (Datab[n]-Bmean2);//a variance Astd = Astd + (da                    Taa[n]-Amean2) * (Dataa[n]-Amean2);//b Variance bstd = Bstd + (Datab[n]-Bmean2) * (Datab[n]-Bmean2);} }CORR2 = Cov/(sqrt (ASTD * bstd)); return CORR2;}

image to be searched mat Srcimage = Imread ("m:/image processing experiment/verification Code/byx001.bmp", 1); Mat Resizemat = Mat::zeros (+, CV_8UC3);//matrix scaled to 25*25. Because the template size to be related matches is 25*25resize (Srcimage,resizemat,resizemat.size ());//correlation match double ccorrval[26] = {0};d ouble max = 0;int Count = 0;for (int m = 0; m <; m++) {char recogpath[100] = {1};strcpy (Recogpath, "m://image Processing experiment//verification code//capital Letter//"); Char num[2 ] = {1};num[0] = + m;strcat (recogpath, num); strcat (Recogpath, ". bmp"); Mat Img_display;resizemat.copyto (Img_display); Mat std = imread (recogpath,0); Mat RESIZEMATSTD = Mat::zeros (+, cv_8uc3); Resize (std,resizematstd,resizematstd.size ()); Adaptivethreshold ( RESIZEMATSTD, RESIZEMATSTD, 255, Adaptive_thresh_mean_c, thresh_binary,5,1); Corr2val[m] = CORR2 (resizeMatSTD,img_ Display), if (Max <= corr2val[m]) {max = Corr2val[m];count = m;}} Char pathname[100]={1};strcpy (pathname, "m://image Processing experiment//Verification Code//test//character _"); Char num[10];_itoa (i, NUM, ten); Strcat ( pathname, num); char c[2] = {1}; C[0] = + count;strcat (pathname, C); strcat (Pathname, ". bmp"); Imwrite (PAthname, Resizemat); 

Character templates:


Recognition result output:



OCR recognition based on two-dimensional correlation coefficients of matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.