OpenCV Digital Verification Code identification

Source: Internet
Author: User

post-Update code download LINK here!

!!

Click I download
This article is for OPENCV beginners. Because I do not specialize in the image, just to complete a pattern recognition of small jobs.

The main function is to proactively identify the figures in the image. The picture contains the normal picture, with a scratch image and a noisy image.

Respectively for example the following

first. Look at the recognition effect!

Then we'll start with some dry goods:
  • OPENCV Installation and configuration: If this is going to be able to write a blog post again, I would have nothing to do with a opencv trouble died, the final reference online studio2012 configuration method succeeded, in this skip. See here your OPENCV still can not use, hurriedly don't look down, first put OpenCV well again come!
  • OPENCV Basic picture operation:
    • Because OPENCV has 2.0 and 3.0 version number differences, so the function or type found on the web are two formats, the proposed new version, what impimage* type is 2.0 version number of the wording. All I use is the mat. Must be unified well, not a new one will be old, will error.

    • Read the picture imread, show imshow, wait for Waitkey and so on. These must first be familiar
    • The power of OPENCV is that almost all image operations have a ready-made function to call, which is very handy. More than Google, there will be a function has been implemented you want to complete the function.

  • Binary: The background is not clean, whether it is the original image or a drawing with scratches or noise. The impact on the recognition is still very bad, so we need to first binary. Distinguish the black and white pixel points. However, the right side of the picture is significantly darker than the left side, so when the threshold is more difficult to select, it is very hard to use a fixed value to the two parts of the image is very ideal, so the use of forcing lattice higher adaptive binary (adaptivethreshold), Tips: Two histogram equalization before the value of the effect will be better.

  • Median filter: Median filtering is a very good solution for noisy and scratched images. The median parameter can be adjusted to eliminate the effects of noise very well. The disadvantage is that the number of bad adjustment ah, the tune of Want to die.

  • Template matching: The source of the template can be from the image to be identified from the point of view, but we work to provide a template picture, so this step can be omitted. OPENCV provides a very powerful matchtemplate function. Ability to calculate a similar value for a given picture and template in accordance with the calculation method you specify. and store the corresponding coordinates. The only thing you need to do is to have a larger (or smaller) image frame that is relevant to the function you specified to calculate the similarity.
  • Form scan: To improve the recognition rate. I set a form to scan the original image, the movement of the scanned form set a rule, that is, if the previous form did not match to the number to fine-tune the form position, assuming the match to the number will move the left axis of the form to the right side of the number to match. Repeat the scan again.

The basic dry goods are so much. The rest is the constant number of parameters and scan the position of the form, the flaw of this method is for different pictures. Both the parameter and the scan window change, for example, a row or three of rows of numbers, you must change the function of the scanned form. There are steps of every step and the like. It's a pretty sore egg!

Here are some of the core code

Pretreatment. Includes adaptive binary and median filtering

voidPreprocess () {//Adaptive binary & median filterMat out;//Adaptive binaryAdaptivethreshold (source, source,255, Cv_adaptive_thresh_mean_c, Cv_thresh_binary, Adaptivebisize, Adaptivebiparam);//Median filterNamedwindow ("Binary"); Imshow ("Binary", source); Waitkey (0); Medianblur (Source, out, medianblursize); Namedwindow ("Medianblur"); Imshow ("Medianblur", out); Waitkey (0); Source = out; Srcresult = out;//used to display}

The

BOOL Match (Mat src) {int SRCW,SRCH,TEMPLATW, Templath, Curtemplatw,curtemplath,resulth, RESULTW;        Mat Templat,result;          SRCW = Src.cols;        SrcH = src.rows; Double Currentminch=1; int currentindex=0;          Double MinValue, MaxValue;         Point Minloc, Maxloc,matchloc; /* * * * * * calculation method of Similarity * *0: Cv_tm_sqdiff squared Difference matching method. The best match value is0The worse the match, the greater the match value * *1: cv_tm_sqdiff_normed Normalized squared difference matching method * *2: Cv_tm_ccorr Correlation Matching Method: The method adopts multiplication operation; The larger the value, the better the match. * *3: cv_tm_ccorr_normed Normalization Related matching method * *4: Cv_tm_ccoeff correlation coefficient matching method:1Represents a perfect match. -1Represents the worst match. **5: cv_tm_ccoeff_normed normalization correlation coefficient matching method */int methodtype=1; Cyclic inference8Figure which number template is closest to being measured trying to look like a for(int i=0;i<8; i++) {Templat = Templatvec[i];              TEMPLATW = Templat.cols; Templath = templat.rows;if(SRCW < TEMPLATW | | SrcH < TEMPLATH) {cout <<"The template cannot be larger than the original image"<< Endl;return 0; } RESULTW = Srcw-templatw +1; Resulth = Srch-templath +1; result = Cvcreateimage (Cvsize (RESULTW, Resulth),1,1);               Matchtemplate (SRC, templat, result, methodtype);            Minmaxloc (result, &minvalue, &maxvalue, &minloc, &maxloc,mat ()); If it is smaller than the current minimum, store the value, subscript, and coordinatesif(Minvalue<currentminch) {Currentminch= MinValue;                Currentindex=i;                matchloc.x=minloc.x+window_x;                matchloc.y=minloc.y+window_y;                CURTEMPLATW = TEMPLATW;            Curtemplath = Templath; }}//cout<<"Min:"<<currentminch<<endl; The minimum value is smaller than the set threshold value. Then infer that this number is recognizedif(Currentminch<threshold) {numresult.push_back (Index[currentindex]); cout<<"section"<<countnumbers<<"The numbers are:"<<index[currentIndex]<<endl; /*cout<<the upper-left corner coordinates are: ("<<matchLoc.x<<","<<matchLoc.y<<")"<<endl; cout<<"Top Right Coordinate: ("<<matchLoc.x+templatW<<","<<matchLoc.y<<")"<<endl; cout<<"lower left corner coordinates: ("<<matchLoc.x<<","<<matchLoc.y+templatH<<")"<<endl;*/countnumbers++; Rectangle (Srcresult, Matchloc, Cvpoint (matchloc.x + CURTEMPLATW, matchloc.y+ curtemplath), Cvscalar (0,0,255)); /*namedwindow ("Tmpresult"); Imshow ("Tmpresult", Srcresult); Waitkey (0); * * window_x =matchloc.x+curtemplatw-1;return true; }//greater than the threshold value is determined to be non-characters. The scanning window moves right one unit window_x++;return false; }

form scanning, virtual functions need to be implemented

virtual voidProcessscan(){SourceW = Source.cols;SourceH = source.rows; window_x =0; Window_y =3; Add 10 to increase fault tolerance bool last =false; while(window_x<Sourcew-scanwindoww+5){if(window_x+scanwindoww>SourceW) {window_x =SourceW-SCANWINDOWW; Last =true;            } Mat tmp = Scanwindow (window_x,window_y); Match (TMP);if(last) Break; } window_x = -; SCANWINDOWH = *; window_y=SourceH-SCANWINDOWH; while(window_x<=Sourcew-scanwindoww-Ten) {Mat tmp = Scanwindow (window_x,window_y);        Match (TMP); }    }

Different classes are created for different images to achieve:

//Identify noisy imagesclassNoisypic: Publicpicture{ Public: Noisypic () {picture (); Threshold =0.5; Path="Test\\noisy.bmp"; Adaptivebisize = -; adaptivebiparam= +; Medianblursize =5; SCANWINDOWW = -; SCANWINDOWH = -; }voidDisplayresult () {cout<<"The image that is currently identified is a noisy point." The recognition result is: "<<endl; for(unsigned intI=0; I<numresult.size (); i++) {cout<<numresult[i]<<" ";}cout<<endl;cout<<"====================================================="<<endl; Namedwindow ("Final"); Imshow ("Final", Srcresult); Waitkey (0); }};//with scratches on the imageclassDirtypic: Publicpicture{ Public: Dirtypic () {picture (); Threshold =0.48; Path="Test\\dirty.bmp"; Adaptivebisize = +; adaptivebiparam= at; Medianblursize =7; SCANWINDOWW = $; SCANWINDOWH = -; }Virtual voidDisplayresult () {cout<<"The image that is currently identified is a scratch and the recognition result is:"<<endl; for(unsigned intI=0; I<numresult.size (); i++) {cout<<numresult[i]<<" ";}cout<<endl;cout<<"====================================================="<<endl; Namedwindow ("Final"); Imshow ("Final", Srcresult); Waitkey (0); }};

Main function

int main()  {      //正常图像,构造函数不指定參数时。默认识别第一张图    //构造函数能够指定识别第几张图。以下以第三张为例    Picture pic = Picture(3);    pic.startRecognize();    //识别有噪声图像    noisyPic noisyPic;    noisyPic.startRecognize();    //识别有划痕图像    dirtyPic dirtyPic;    dirtyPic.startRecognize();    //识别放大缩小图像    scale = scalePic(1);    scale.startRecognize();    return0;  }  
In order to facilitate children's shoes to participate in, provide a download link, just to spend a little points of Oh! (LZ under the other is also to point Ah!)

poke me, poke me

OpenCV Digital Verification Code identification

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.