这篇文章主要介绍了c#身份证识别相关技术详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近研究c#相关的ocr技术,图像识别一般c和c++这种底层语言做的比较多,c#主要是依托一些封装好的组件进行调用,这里介绍一种身份证识别的方法。
环境搭建
下载地址:emgucv官网
在file类别下下载这个exe,进行安装,安装后在目录下能找相应组件,还有些应用的案例。
dll文件夹中的dll引用到c#项目中,x64,x86,tessdata对应ocr识别的类库和语言库,我tessdata中已添加中文语言包,将这三个文件夹放入程序执行文件夹中。
demo
自己做的小demo如图:身份证图片是百度上下载的
不得不说这个类库唯一弊端就是文字识别率太低,图像识别效果也不太好
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)
{
}
}
}
以上就是c#实现身份证识别功能的图文代码详解的详细内容。