通过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导入到项目中。

  1. private static Tesseract _ocr;//创建识别对象
  2. //传入图片进行识别
  3. public static string ORC_(Bitmap img)
  4. {
  5. //""标示OCR识别调用失败
  6. string re = "";
  7. if (img == null)
  8. return re;
  9. else
  10. {
  11.  
  12. Bgr drawColor = new Bgr(Color.Blue);
  13. try
  14. {
  15. Image<Bgr, Byte> image = new Image<Bgr, byte>(img);
  16.  
  17. using (Image<Gray, byte> gray = image.Convert<Gray, Byte>())
  18. {
  19. _ocr.Recognize(gray);
  20. Tesseract.Charactor[] charactors = _ocr.GetCharactors();
  21. foreach (Tesseract.Charactor c in charactors)
  22. {
  23. image.Draw(c.Region, drawColor, );
  24. }
  25.  
  26. re = _ocr.GetText();
  27.  
  28. }
  29. return re;
  30. }
  31. catch (Exception ex)
  32. {
  33.  
  34. return re;
  35. }
  36. }
  37. }
  38.  
  39. //识别方法如点击按钮识别
  40. private void btnXIdentification_Click(object sender, EventArgs e)
  41. {
  42. try
  43. {
  44. _ocr = new Tesseract(@"C:\Emgu\emgucv-windows-x86-gpu 2.4.2.1777\bin\tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);//方法第一个参数可为""表示通过环境变量调用字库,第二个参数表示字库的文件,第三个表示识别方式,可看文档与资料查找。
  45. _ocr.SetVariable("tessedit_char_whitelist", "0123456789X");//此方法表示只识别1234567890与x字母
  46. string result = "";
  47. Bitmap bitmap = new Bitmap(_emguImage.ToBitmap());
  48. bitmap = BrightnessP(bitmap, Convert.ToInt32(this.textBoxX3.Text));//图片加亮处理
  49. bitmap = KiContrast(bitmap, Convert.ToInt32(this.textBoxX2.Text));//调整对比对
  50. this.pictureBox3.Image = bitmap;
  51. result = ORC_(bitmap);
  52. this.textBoxX1.Text = result;
  53. _ocr.Dispose();
  54. }
  55. catch (Exception exception)
  56. {
  57. MessageBox.Show(exception.Message);
  58. }
  59. }
  60.  
  61. /// <summary>
  62. /// 增加图像亮度
  63. /// </summary>
  64. /// <param name="a"></param>
  65.  
  66. /// <param name="v"></param>
  67. /// <returns></returns>
  68. public static Bitmap BrightnessP(Bitmap a, int v)
  69. {
  70. System.Drawing.Imaging.BitmapData bmpData = a.LockBits(new Rectangle(, , a.Width, a.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  71. int bytes = a.Width * a.Height * ;
  72. IntPtr ptr = bmpData.Scan0;
  73. int stride = bmpData.Stride;
  74. unsafe
  75. {
  76. byte* p = (byte*)ptr;
  77. int temp;
  78. for (int j = ; j < a.Height; j++)
  79. {
  80. for (int i = ; i < a.Width * ; i++, p++)
  81. {
  82. temp = (int)(p[] + v);
  83. temp = (temp > ) ? : temp < ? : temp;
  84. p[] = (byte)temp;
  85. }
  86. p += stride - a.Width * ;
  87. }
  88. }
  89. a.UnlockBits(bmpData);
  90. return a;
  91. }
  92. ///<summary>
  93. ///图像对比度调整
  94. ///</summary>
  95. ///<param name="b">原始图</param>
  96. ///<param name="degree">对比度[-100, 100]</param>
  97. ///<returns></returns>
  98. public static Bitmap KiContrast(Bitmap b, int degree)
  99. {
  100. if (b == null)
  101. {
  102. return null;
  103. }
  104. if (degree < -) degree = -;
  105. if (degree > ) degree = ;
  106. try
  107. {
  108. double pixel = ;
  109. double contrast = (100.0 + degree) / 100.0;
  110. contrast *= contrast;
  111. int width = b.Width;
  112. int height = b.Height;
  113. BitmapData data = b.LockBits(new Rectangle(, , width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  114. unsafe
  115. {
  116. byte* p = (byte*)data.Scan0;
  117. int offset = data.Stride - width * ;
  118. for (int y = ; y < height; y++)
  119. {
  120. for (int x = ; x < width; x++)
  121. {
  122. // 处理指定位置像素的对比度
  123. for (int i = ; i < ; i++)
  124. {
  125. pixel = ((p / 255.0 - 0.5) * contrast + 0.5) * ;
  126. if (pixel < ) pixel = ;
  127. if (pixel > ) pixel = ;
  128. p = (byte)pixel;
  129. } // i
  130. p += ;
  131. } // x
  132. p += offset;
  133. } // y
  134. }
  135. b.UnlockBits(data);
  136. return b;
  137. }
  138. catch (Exception ex)
  139. {
  140. return null;
  141. }
  142. }

目前我只是识别文字与字幕,3.0版本虽然可以识别中文,但误读率实在不敢恭维。
本文如有错误之处请提出,灰常感谢!

转自:http://www.sufeinet.com/thread-6690-1-1.html

C#识别图片上的数字的更多相关文章

  1. python 识别图片上的数字

    https://blog.csdn.net/qq_31446377/article/details/81708006 ython 3.6 版本 Pytesseract 图像验证码识别 环境: (1) ...

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

    通过Emgu实现对图片上的数字进行识别.前期步骤:1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持.2.安装后需填写环境变量,环境变量Path值后加入Emgu安装 ...

  3. c#实现识别图片上的验证码数字

    这篇文章主要介绍了c#实现识别图片上的验证码数字的方法,本文给大家汇总了2种方法,有需要的小伙伴可以参考下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  4. Python3.x:如何识别图片上的文字

    Python3.x:如何识别图片上的文字 安装pytesseract库,必须先安装其依赖的PIL及tesseract-ocr,其中PIL为图像处理库,而后面的tesseract-ocr则为google ...

  5. python 图片上添加数字源代码

    最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/ ...

  6. PHP识别简单的图片上面的数字(可扩展)

    1.场景 最近在学习图片处理,就是特意把数字生成一个图片,然后再用程序去识别图片的数字.这就有了一下的学习过程. 2.原理分析 2.1 首先是将图片像素化,二值化,然后和字模去对比(需要相对于配置字模 ...

  7. KNN识别图像上的数字及python实现

    领导让我每天手工录入BI系统中的数据并判断数据是否存在异常,若有异常点,则检测是系统问题还是业务问题.为了解放双手,我决定写个程序完成每天录入管理驾驶舱数据的任务.首先用按键精灵录了一套脚本把系统中的 ...

  8. 如何大批量的识别图片上的文字,批量图片文字识别OCR软件系统

    软件不需要安装,直接双击打开就可以用,废话不多说直接上图好了,方便说明问题 批量图片OCR(批量名片识别.批量照片识别等)识别,然后就下来研究了一下,下面是成果 使用步骤:打开单个图片识别,导入文件夹 ...

  9. 机器学习进阶-项目实战-信用卡数字识别 1.cv2.findContour(找出轮廓) 2.cv2.boudingRect(轮廓外接矩阵位置) 3.cv2.threshold(图片二值化操作) 4.cv2.MORPH_TOPHAT(礼帽运算突出线条) 5.cv2.MORPH_CLOSE(闭运算图片内部膨胀) 6. cv2.resize(改变图像大小) 7.cv2.putText(在图片上放上文本)

    7. cv2.putText(img, text, loc, text_font, font_scale, color, linestick) # 参数说明:img表示输入图片,text表示需要填写的 ...

随机推荐

  1. ubuntu14.04下arm-linux-gcc 4.5.1的安装与配置

    使用的是友善之臂mini6410自带光盘中的. 1.对新版本arm-linux-gcc-5.4.1进行解压(注意,下面的C是大写的) tar zxvf arm-linux-gcc-4.5.1-v6-v ...

  2. thinkphp的条件的多种写法

    class SelectAction extends Action{   function index(){ //thinkphp 查询语言  //         1.普通查询 //   2.区间查 ...

  3. JQ 动态添加节点

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. gcc常用命令集

    引用:http://developer.51cto.com/art/200609/32317_1.htm 对于GUN编译器来说,程序的编译要经历预处理.编译.汇编.连接四个阶段 假设源程序文件名为te ...

  5. xaml控件样式大全(太有用了)C#

    地址:链接:http://pan.baidu.com/s/1jGlMyEi 密码:zaeg http://blog.csdn.net/lhx527099095/article/category/943 ...

  6. Codeforces Round #194 (Div. 2) D. Chips

    D. Chips time limit per test:1 second memory limit per test:256 megabytes input:standard input outpu ...

  7. 【Android类型SDK测试(一)】认识Android类型的 SDK

    (一)SDK是个什么东东 接触软件相关行业的同学都应该知道,SDK(即 Software Development Kit),软件开发包.其作用就是为开发某些软件提供一些便利的东西,包括工具 集合,文档 ...

  8. php讲中文json数据编码

    <?php function show_jsonmsg($data){ if(is_array($data)){ $return = $data; }else{ $return = array( ...

  9. oracle recyclebin详解(闪回删除的表)

    今天在SOA应用数据库上运用DBMS_REDEFITION包进行在线非分区表转换分区表操作时,本想DROP掉建的临时表cube_scope_temp不小心后面忘记加"temp"直接 ...

  10. win7笔记本电脑实现wifi共享

    前提条件:win 7系统,有wifi 同dos命令就可实现wifi共享 第一步: netsh wlan start hostednetwork pause 第二步: netsh wlan set ho ...