這篇文章主要介紹了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#實現身分證辨識功能的圖文代碼詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!