分享C#识别图片上的数字

通过Emgu实现对图片上的数字进行识别。
前期步骤:
1.下载Emgu安装文件,我的版本是2.4.2.1777。3.0版本则实现对中文的支持。
2.安装后需填写环境变量,环境变量Path值后加入Emgu安装路径到bin下。如C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin;
3.在bin下查找需要的dll如Emgu.CV.dll与Emgu.CV.OCR.dll等。
4.将C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin下的文件夹tessdata赋值到程序运行目录下。
注:安装后的Emgu路径下有C#版本的demo可供参考
关键代码:
将需要的dll导入到项目中。

private static Tesseract _ocr;//创建识别对象
//传入图片进行识别
public static string ORC_(Bitmap img)
        {
            //""标示OCR识别调用失败
            string re = "";
            if (img == null)
                return re;
            else
            {
 
 
                Bgr drawColor = new Bgr(Color.Blue);
                try
                {
                    Image<Bgr, Byte> image = new Image<Bgr, byte>(img);
 
 
                    using (Image<Gray, byte> gray = image.Convert<Gray, Byte>())
                    {
                        _ocr.Recognize(gray);
                        Tesseract.Charactor[] charactors = _ocr.GetCharactors();
                        foreach (Tesseract.Charactor c in charactors)
                        {
                            image.Draw(c.Region, drawColor, 1);
                        }
 
 
                        re = _ocr.GetText();
 
 
                    }
                    return re;
                }
                catch (Exception ex)
                {
                     
                    return re;
                }
            }
        }
 
//识别方法如点击按钮识别
private void btnXIdentification_Click(object sender, EventArgs e)
        {
            try
            {
                _ocr = new Tesseract(@"C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin\tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);//方法第一个参数可为""表示通过环境变量调用字库,第二个参数表示字库的文件,第三个表示识别方式,可看文档与资料查找。
                _ocr.SetVariable("tessedit_char_whitelist", "0123456789X");//此方法表示只识别1234567890与x字母
                string result = "";
                Bitmap bitmap = new Bitmap(_emguImage.ToBitmap());
                bitmap = BrightnessP(bitmap, Convert.ToInt32(this.textBoxX3.Text));//图片加亮处理
                bitmap = KiContrast(bitmap, Convert.ToInt32(this.textBoxX2.Text));//调整对比对
                this.pictureBox3.Image = bitmap;
                result = ORC_(bitmap);
                this.textBoxX1.Text = result;
                _ocr.Dispose();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
 
/// <summary>  
        /// 增加图像亮度  
        /// </summary>  
        /// <param name="a"></param>  
 
        /// <param name="v"></param>  
        /// <returns></returns>  
        public static Bitmap BrightnessP(Bitmap a, int v)
        {
            System.Drawing.Imaging.BitmapData bmpData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            int bytes = a.Width * a.Height * 3;
            IntPtr ptr = bmpData.Scan0;
            int stride = bmpData.Stride;
            unsafe
            {
                byte* p = (byte*)ptr;
                int temp;
                for (int j = 0; j < a.Height; j++)
                {
                    for (int i = 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);
            return a;
        }
        ///<summary>
        ///图像对比度调整
        ///</summary>
        ///<param name="b">原始图</param>
        ///<param name="degree">对比度[-100, 100]</param>
        ///<returns></returns>
        public static Bitmap KiContrast(Bitmap b, int degree)
        {
            if (b == null)
            {
                return null;
            }
            if (degree < -100) degree = -100;
            if (degree > 100) degree = 100;
            try
            {
                double pixel = 0;
                double contrast = (100.0 + degree) / 100.0;
                contrast *= contrast;
                int width = b.Width;
                int height = b.Height;
                BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* p = (byte*)data.Scan0;
                    int offset = data.Stride - width * 3;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            // 处理指定位置像素的对比度
                            for (int i = 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;
                            } // i
                            p += 3;
                        } // x
                        p += offset;
                    } // y
                }
                b.UnlockBits(data);
                return b;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

OEM_TESSERACT_ONLY,           // Run Tesseract only - fastest运行只TESSERACT - 最快
OEM_CUBE_ONLY,                // Run Cube only - better accuracy, but slower只运行立方 - 更好的精度,但速度较慢
OEM_TESSERACT_CUBE_COMBINED,  // Run both and combine results - best accuracy运行和结果相结合 - 最佳精度
OEM_DEFAULT                   // Specify this mode when calling init_*(),指定此模式下,当调用init_*()

posted @ 2017-05-31 10:57  君莫笑·秋  阅读(6744)  评论(1编辑  收藏  举报