C#身份證識(shí)別相關(guān)技術(shù)功能詳解
最近研究C#相關(guān)的OCR技術(shù),圖像識(shí)別一般C和C++這種底層語(yǔ)言做的比較多,C#主要是依托一些封裝好的組件進(jìn)行調(diào)用,這里介紹一種身份證識(shí)別的方法。
環(huán)境搭建
下載地址:EmguCV官網(wǎng)

在File類別下下載這個(gè)EXE,進(jìn)行安裝,安裝后在目錄下能找相應(yīng)組件,還有些應(yīng)用的案例。
dll文件夾中的dll引用到C#項(xiàng)目中,x64,x86,tessdata對(duì)應(yīng)OCR識(shí)別的類庫(kù)和語(yǔ)言庫(kù),我tessdata中已添加中文語(yǔ)言包,將這三個(gè)文件夾放入程序執(zhí)行文件夾中。
Demo
自己做的小Demo如圖:身份證圖片是百度上下載的

不得不說(shuō)這個(gè)類庫(kù)唯一弊端就是文字識(shí)別率太低,圖像識(shí)別效果也不太好
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)
{
//第一個(gè)參數(shù)是語(yǔ)言包文件夾的地址,不寫(xiě)默認(rèn)在執(zhí)行文件夾下
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 = "請(qǐng)選擇圖片";
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>
/// 旋轉(zhuǎn)校正
/// </summary>
/// <param name="imageInput"></param>
/// <returns></returns>
private Image<Gray, Byte> randon(Image<Gray, Byte> imageInput)//圖像投影旋轉(zhuǎn)法傾斜校正子函數(shù)定義
{
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度范圍內(nèi)的調(diào)整
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)
{
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫(kù)中的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫(kù)中的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#讀取Excel表數(shù)據(jù)及導(dǎo)入Sql Server數(shù)據(jù)庫(kù)的具體操作步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼
這篇文章主要介紹了c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
Unity 按鈕事件封裝操作(EventTriggerListener)
這篇文章主要介紹了Unity 按鈕事件封裝操作(EventTriggerListener),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C#結(jié)束Excel進(jìn)程的步驟教學(xué)
在本篇文章里小編給大家分享了關(guān)于C#結(jié)束Excel進(jìn)程的步驟教學(xué)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-01-01
C#實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)和DataTable
Excel文件是存儲(chǔ)表格數(shù)據(jù)的普遍格式,因此能夠高效地讀取和提取信息對(duì)于我們來(lái)說(shuō)至關(guān)重要,下面我們就來(lái)看看C#如何實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)和DataTable吧2024-03-03

