通过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, );
} 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(, , a.Width, a.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int bytes = a.Width * a.Height * ;
IntPtr ptr = bmpData.Scan0;
int stride = bmpData.Stride;
unsafe
{
byte* p = (byte*)ptr;
int temp;
for (int j = ; j < a.Height; j++)
{
for (int i = ; i < a.Width * ; i++, p++)
{
temp = (int)(p[] + v);
temp = (temp > ) ? : temp < ? : temp;
p[] = (byte)temp;
}
p += stride - a.Width * ;
}
}
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 < -) degree = -;
if (degree > ) degree = ;
try
{
double pixel = ;
double contrast = (100.0 + degree) / 100.0;
contrast *= contrast;
int width = b.Width;
int height = b.Height;
BitmapData data = b.LockBits(new Rectangle(, , width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)data.Scan0;
int offset = data.Stride - width * ;
for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
// 处理指定位置像素的对比度
for (int i = ; i < ; i++)
{
pixel = ((p / 255.0 - 0.5) * contrast + 0.5) * ;
if (pixel < ) pixel = ;
if (pixel > ) pixel = ;
p = (byte)pixel;
} // i
p += ;
} // 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_*()

分享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. 【php】session_start 报 no such file

    如果是yum安装修改php-fpm.conf 里面的 session.save_path 如果是编译的,修改php.ini 的session.save_path (此条未测试)

  2. Python开发不可不知的虚拟环境

    一.python3.3之后自带的venv模块 1. 创建虚拟环境 python3.6 -m venv project-env 2. 加入虚拟环境目录 cd pronject-env 3. 激活虚拟环境 ...

  3. StartWith 测试

    var clientConfiguration = GetConfiguration("couchbase.json"); ClusterHelper.Initialize(cli ...

  4. Python框架之Django学习笔记(十二)

    Django站点管理 十一转眼结束,说好的充电没能顺利开展,反而悠闲的看了电视剧以及去影院看了新上映的<心花路放>.<亲爱的>以及<黄金时代>,说好的劳逸结合现在回 ...

  5. 纯js国际化(i18n)

    i18n,是internationalization单词的简写,中间18个字符略去,简称i18n,意图就是实现国际化,方便产品在不同的场景下使用 目标:可以点击切换语言或者ChangeLanguage ...

  6. uncompyle2反编译python的.py文件

    前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...

  7. 踩坑 Uncaught RangeError: Maximum call stack size exceeded

    今天遇到了一个错误, 堆栈溢出,很好奇就是一个简单ajax请求怎么会报这个错误,研究了一下,发现犯了一个很低级的错误,data的参数错误了: passWord是未定义的变量,值为空,然后导致了这个问题 ...

  8. 【LeetCode】Implement strStr()(实现strStr())

    这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...

  9. [python][django学习篇][9]设计正在博客视图(3)

    需求: 真正的首页视图函数,当用户访问我们的博客首页时,他将看到我们发表的博客文章列表,就像 演示项目 里展示的这样.t https://docs.djangoproject.com/en/1.10/ ...

  10. springmvc支持跨域的请求(复制)

    Spring MVC 新增跨域支持 发表于2017/5/8 22:01:24  48人阅读 分类: SpringMVC Spring MVC 4.2 增加 CORS 支持 跨站 HTTP 请求(Cro ...