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

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_*()

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

  1. C#识别图片上的数字

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

  2. python 识别图片上的数字

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

  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. vuejs开发组件分享之H5图片上传、压缩及拍照旋转的问题处理

    一.前言 三年.net开发转前端已经四个月了,前端主要用webpack+vue,由于后端转过来的,前端不够系统,希望分享下开发心得与园友一起学习. 图片的上传之前都是用的插件(ajaxupload), ...

  9. 分享一个react 图片上传组件 支持OSS 七牛云

    react-uplod-img 是一个基于 React antd组件的图片上传组件 支持oss qiniu等服务端自定义获取签名,批量上传, 预览, 删除, 排序等功能 需要 react 版本大于 v ...

随机推荐

  1. javascript 计算倒计时

    function timeDown(second) { var month = '', day = '', hour = '', minute = ''; if (second >= 86400 ...

  2. 使用cxf 发布 jax-rs 风格webservice 。并客户端测试。

    详细介绍:http://www.ibm.com/developerworks/cn/java/j-lo-jaxrs/ 1.定义一个User对象 package com.zf.test; import  ...

  3. Flask初学者:session操作

    cookie:是一种保存数据的格式,也可以看成是保存数据的一个“盒子”,服务器返回cookie给浏览器(由服务器产生),由浏览器保存在本地,下次再访问此服务器时浏览器就会自动将此cookie一起发送给 ...

  4. Gym - 101908G Gasoline 二分+最大流

    G - Gasoline Gym - 101908G 题意:给出R个提供点,P个接收点,每个接收点都要接收满,还有一个运输的时间,问最小时间能够完成所有的运输 题解:首先每次都必须要满流,所以我们只要 ...

  5. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  6. linux系统下单节点hadoop2的配置

    Jdk安装: jdk-7u45-linux-x64.gz cp jdk-7u45-linux-x64.gz /usr/java/ cd /usr/java/ tar -zxvf jdk-7u45-li ...

  7. 从零开始到设计Python+Selenium自动化测试框架-如何开始

    如何开始学习web ui自动化测试?如何选择一门脚本语言?选择什么自动化测试工具? 本人已经做测试快5年,很惭愧,感觉积累不够,很多测试都不会,三年多功能测试,最近两年才开始接触和学习自动化测试.打算 ...

  8. 微信小程序-----校园头条整体概括

    1.项目需求 为了让在校师生可以更加方便的了解学校信息,从而合理的安排自己的时间,避免发生冲突和错过事件,通过小程序的便利性,可以达到随手一查的功能. 2.项目布局 3.效果展示 3.1登录 3.2首 ...

  9. Your branch is ahead of 'origin/master' by 21 commits.

    当切换到主分支后,准备 git pull 拉取远程 master 分支时,提示本地主分支显示有 21 个commits 问题原因: 因为你修改了 local master 本地的主分支,可以选择以下方 ...

  10. sqlserver 操作access数据库

    exec sp_configure 'show advanced options',1  reconfigure  exec sp_configure 'Ad Hoc Distributed Quer ...