最近研究c#相关的ocr技术,图像识别一般c和c++这种底层语言做的比较多,c#主要是依托一些封装好的组件进行调用,这里介绍一种身份证识别的方法。
环境搭建
下载地址:emgucv官网
在file类别下下载这个exe,进行安装,安装后在目录下能找相应组件,还有些应用的案例。
dll文件夹中的dll引用到c#项目中,x64,x86,tessdata对应ocr识别的类库和语言库,我tessdata中已添加中文语言包,将这三个文件夹放入程序执行文件夹中。
demo
自己做的小demo如图:身份证图片是百度上下载的
不得不说这个类库唯一弊端就是文字识别率太低,图像识别效果也不太好
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using emgu.cv; using emgu.cv.ocr; using emgu.cv.structure; using system.io; namespace emgucv { public partial class form1 : form { image<gray, byte > imagethreshold; public form1() { initializecomponent(); picturebox1.enabled = false ; } private void form1_load( object sender, eventargs e) { } private void button1_click( object sender, eventargs e) { //第一个参数是语言包文件夹的地址,不写默认在执行文件夹下 tesseract _ocr = new tesseract( @"" , "chi_sim" , ocrenginemode.tesseractonly); _ocr.setimage(imagethreshold); _ocr.recognize(); string text = _ocr.getutf8text(); this .textbox1.text = text; } private void picturebox2_click( object sender, eventargs e) { openfiledialog of = new openfiledialog(); of.title = "请选择图片" ; if (of.showdialog() == dialogresult.ok) { string file = of.filename; image img = image.fromfile(file); picturebox1.image = img; } bitmap bitmap = (bitmap) this .picturebox1.image; image<bgr, byte > imagesource = new image<bgr, byte >(bitmap); image<gray, byte > imagegrayscale = imagesource.convert<gray, byte >(); imagegrayscale = randon(imagegrayscale); imagethreshold = imagegrayscale.thresholdbinary( new gray(100), new gray(255)); this .picturebox2.image = imagethreshold.tobitmap(); } /// <summary> /// 旋转校正 /// </summary> /// <param name="imageinput"></param> /// <returns></returns> private image<gray, byte > randon(image<gray, byte > imageinput) //图像投影旋转法倾斜校正子函数定义 { int nwidth = imageinput.width; int nheight = imageinput.height; int sum; int sumofcha; int sumofchatemp = 0; int [] sumhang = new int [nheight]; image<gray, byte > resultimage = imageinput; image<gray, byte > imrotaimage; //20度范围内的调整 for ( int ang = -20; ang < 20; ang = ang + 1) { imrotaimage = imageinput.rotate(ang, new gray(1)); for ( int i = 0; i < nheight; i++) { sum = 0; for ( int j = 0; j < nwidth; j++) { sum += imrotaimage.data[i, j, 0]; } sumhang[i] = sum; } sumofcha = 0; for ( int k = 0; k < nheight - 1; k++) { sumofcha = sumofcha + (math.abs(sumhang[k] - sumhang[k + 1])); } if (sumofcha > sumofchatemp) { resultimage = imrotaimage; sumofchatemp = sumofcha; } } return resultimage; } private void picturebox1_click( object sender, eventargs e) { } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/AlexZha/p/7171927.html?utm_source=tuicool&utm_medium=referral