C#實現(xiàn)身份證識別功能的方法

這篇文章將為大家詳細講解有關(guān)C#實現(xiàn)身份證識別功能的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了湘潭縣免費建站歡迎大家使用!

最近研究C#相關(guān)的OCR技術(shù),圖像識別一般C和C++這種底層語言做的比較多,C#主要是依托一些封裝好的組件進行調(diào)用,這里介紹一種身份證識別的方法。

環(huán)境搭建

下載地址:EmguCV官網(wǎng)

C#實現(xiàn)身份證識別功能的方法

在File類別下下載這個EXE,進行安裝,安裝后在目錄下能找相應組件,還有些應用的案例。

dll文件夾中的dll引用到C#項目中,x64,x86,tessdata對應OCR識別的類庫和語言庫,我tessdata中已添加中文語言包,將這三個文件夾放入程序執(zhí)行文件夾中。

Demo

自己做的小Demo如圖:身份證圖片是百度上下載的

C#實現(xiàn)身份證識別功能的方法

不得不說這個類庫唯一弊端就是文字識別率太低,圖像識別效果也不太好

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)
  {
   //第一個參數(shù)是語言包文件夾的地址,不寫默認在執(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 = "請選擇圖片";
   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)
  {

  }
 }
}

關(guān)于C#實現(xiàn)身份證識別功能的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享文章:C#實現(xiàn)身份證識別功能的方法
鏈接地址:http://muchs.cn/article30/gphipo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、標簽優(yōu)化、電子商務網(wǎng)站排名、、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設