C # identifies numbers on a picture

Source: Internet
Author: User

The numbers on the image are identified by the EMGU implementation.
Previous steps:
1. Download the EMGU installation file, my version is 2.4.2.1777. Support for Chinese is available for version 3.0.
2. After installation, fill in the environment variable, and add the EMGU installation path to the bin after the environment variable path value. such as C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin;
3. Find the required DLLs under bin such as Emgu.CV.dll and Emgu.CV.OCR.dll.
4. The C:\Emgu\emgucv-windows-x86-gpu folder under 2.4.2.1777\bin The Tessdata is assigned to the program run directory.
Note: A C # version of demo is available under the EMGU path after installation for reference
Key code:
Import the required DLLs into your project.

Private StaticTesseract _OCR;//Create a Recognition object//incoming images for identification Public Static stringorc_ (Bitmap img) {//"" Marked OCR recognition call failed    stringRe =""; if(img = =NULL)        returnre; Else{Bgr Drawcolor=NewBgr (Color.Blue); Try{Image&LT;BGR, byte> image =NewIMAGE&LT;BGR,byte>(IMG); using(Image<gray,byte> Gray = image. Convert<gray, byte>()) {_OCR.                recognize (gray); Tesseract.charactor[] Charactors=_OCR.                Getcharactors (); foreach(Tesseract.charactor Cinchcharactors) {image. Draw (C.region, Drawcolor,1); } Re=_OCR.            GetText (); }            returnre; }        Catch(Exception ex) {returnre; }    }}//recognition methods such as click button RecognitionPrivate voidBtnxidentification_click (Objectsender, EventArgs e) {    Try{_OCR=NewTesseract (@"C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin\tessdata","Eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);//method The first parameter can be "" means to call the library through the environment variable, the second parameter represents the font of the file, the third means the recognition method, can see the document and data lookup. _OCR. SetVariable ("tessedit_char_whitelist","0123456789X");//This method indicates that only 1234567890 and x letters are recognized        stringresult =""; Bitmap Bitmap=NewBitmap (_emguimage.tobitmap ()); Bitmap= BRIGHTNESSP (Bitmap, Convert.ToInt32 ( This. Textboxx3.text));//Picture Highlighting ProcessBitmap = Kicontrast (Bitmap, Convert.ToInt32 ( This. Textboxx2.text));//Adjust the contrast to         This. Picturebox3.image =bitmap; Result=orc_ (bitmap);  This. Textboxx1.text =result; _OCR.    Dispose (); }    Catch(Exception Exception) {MessageBox.Show (Exception.    Message); }}/// <summary>  ///Increase image Brightness/// </summary>  /// <param name= "a" ></param>  /// <param name= "V" ></param>  /// <returns></returns>   Public StaticBitmap BRIGHTNESSP (Bitmap A,intv) {System.Drawing.Imaging.BitmapData Bmpdata= A.lockbits (NewRectangle (0,0, A.width, A.height), System.Drawing.Imaging.ImageLockMode.ReadWrite,    SYSTEM.DRAWING.IMAGING.PIXELFORMAT.FORMAT24BPPRGB); intbytes = A.width * A.height *3; IntPtr ptr=Bmpdata.scan0; intStride =Bmpdata.stride; unsafe    {        byte* p = (byte*) ptr; inttemp;  for(intj =0; J < A.height; J + +)        {             for(inti =0; I < A.width *3; i++, p++) {Temp= (int) (p[0] +v); Temp= (Temp >255) ?255: Temp <0?0: temp; p[0] = (byte) temp; } P+ = Stride-a.width *3;    }} a.unlockbits (Bmpdata); returnA;}///<summary>///Image Contrast Adjustment///</summary>///<param name= "B" >Original Diagram</param>///<param name= "degree" >contrast Ratio [ -100, +]</param>///<returns></returns> Public StaticBitmap Kicontrast (Bitmap B,intdegree) {    if(b = =NULL)    {        return NULL; }    if(Degree <- -) degree =- -; if(Degree > -) degree = -; Try    {        DoublePixel =0; DoubleContrast = (100.0+ degree)/100.0; Contrast*=contrast; intwidth =B.width; intHeight =B.height; BitmapData Data= B.lockbits (NewRectangle (0,0, width, height), imagelockmode.readwrite, Pixelformat.format24bpprgb); unsafe        {            byte* p = (byte*) data.            Scan0; intoffset = data. Stride-width *3;  for(inty =0; Y < height; y++)            {                 for(intx =0; x < width; X + +)                {                    //handles the contrast of pixels at a specified position                     for(inti =0; I <3; i++) {pixel= ((P/255.0-0.5) * Contrast +0.5) *255; if(Pixel <0) pixel =0; if(Pixel >255) pixel =255; P= (byte) pixel; } //Ip + =3; } //xp + =offset; } //y} b.unlockbits (data); returnb; }    Catch(Exception ex) {return NULL; }}

At present I just recognize text and subtitles, 3.0 version although can recognize Chinese, but the rate of misreading is not flattering.
If there are errors in this article, please ask, Gray often thank you!

Transferred from: http://www.sufeinet.com/thread-6690-1-1.html

C # identifies numbers on a picture

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.