当前位置:  开发笔记 > 编程语言 > 正文

C#实现身份证识别功能的图文代码详解

这篇文章主要介绍了C#身份证识别相关技术详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了C#身份证识别相关技术详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近研究C#相关的OCR技术,图像识别一般C和C++这种底层语言做的比较多,C#主要是依托一些封装好的组件进行调用,这里介绍一种身份证识别的方法。

环境搭建

下载地址:EmguCV官网

不得不说这个类库唯一弊端就是文字识别率太低,图像识别效果也不太好

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 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 imageSource = new Image(bitmap);
   Image imageGrayscale = imageSource.Convert();
   imageGrayscale = randon(imageGrayscale);
   imageThreshold = imageGrayscale.ThresholdBinary(new Gray(100), new Gray(255));
   this.pictureBox2.Image = imageThreshold.ToBitmap();
  }
  /// 
  /// 旋转校正
  /// 
  /// 
  /// 
  private Image randon(Image imageInput)//图像投影旋转法倾斜校正子函数定义
  {
   int nwidth = imageInput.Width;
   int nheight = imageInput.Height;
   int sum;
   int SumOfCha;
   int SumOfChatemp = 0;
   int[] sumhang = new int[nheight];
   Image resultImage = imageInput;
   Image 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
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有