Home > Article > Backend Development > Detailed explanation of graphic and text code for realizing ID card recognition function in C#
This article mainly introduces the detailed explanation of C# ID card recognition related technologies, which has certain reference value. Interested friends can refer to
Recent research on C# related OCR technology, image recognition is generally C Compared with low-level languages such as C++, C# mainly relies on some encapsulated components to make calls. Here is a method of ID card recognition.
Environment setup
Download address: EmguCV official website
Download this EXE under the File category and proceedInstallation After installation, you can find the corresponding components in the directory, as well as some application cases.
The dll in the dll folder is referenced to the C# project. x64, x86, and tessdata correspond to the class library and language library recognized by OCR. I have added a Chinese language package to my tessdata. The three folders are placed in the program execution folder.
Demo
The small Demo I made is as shown below: ID cardPicture is downloaded from Baidu
I have to say that the only drawback of this class library is that the text recognition rate is too low, and the image recognition effect is not very good
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) { } } }
The above is the detailed content of Detailed explanation of graphic and text code for realizing ID card recognition function in C#. For more information, please follow other related articles on the PHP Chinese website!