zoukankan      html  css  js  c++  java
  • 【C#】身份证识别(一):身份证号定位

    完整项目地址:https://gitee.com/xgpxg/ICRS

    一、图像预处理

    • 灰度化图像。
    • 二值化图像。 利用 AdaptiveThresholdType 全局化阈值,blockSize = 55时,效果最好。
    public static Image<Gray,byte> BinImg(Image <Bgr ,byte> img)
            {
                return img.Convert<Gray, byte>().ThresholdAdaptive(new Gray(255),
                    AdaptiveThresholdType.GaussianC,
                    ThresholdType.Binary,
                    55,
                    new Gray(55)).Erode(9);
            }

    这里写图片描述

    二、身份证号区域提取

    • 扩选图像黑色像素区域,形成连通区域。
    img.Erode(9)

    这里写图片描述

    • 提取轮廓。
    /// <summary>
            /// 获取轮廓
            /// </summary>
            /// <param name="pic"></param>
            /// <returns></returns>
            private static  VectorOfVectorOfPoint GetContours(Image<Gray, byte> pic)
            {
                Image<Gray, byte> p1 = new Image<Gray, byte>(pic.Size);
                Image<Gray, byte> p2 = pic.Canny(60, 255);
                VectorOfVectorOfPoint contours = contours = new VectorOfVectorOfPoint();
                CvInvoke.FindContours(p2, contours, p1, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);
                return contours;
            }
    • 轮廓筛选,提取身份证矩形区域。身份证号的宽高比约为8,将比值为7~9的轮廓保留,并绘制矩形
    //筛选矩形
     public static RotatedRect RotatedRect( Image<Bgr, byte> img)
            {
                Image<Bgr, byte> a = new Image<Bgr, byte>(img.Size);
                VectorOfVectorOfPoint con = GetContours(BinImg(img));
                Point[][] con1 = con.ToArrayOfArray();
                PointF[][] con2 = Array.ConvertAll<Point[], PointF[]>(con1, new Converter<Point[], PointF[]>(PointToPointF));
                for (int i = 0; i < con.Size; i++)
                {
                    RotatedRect rrec = CvInvoke.MinAreaRect(con2[i]); 
                    float w = rrec.Size.Width;
                    float h = rrec.Size.Height;  
                    if (w / h > 6  &&  w / h < 10 && h > 20)
                    {
    
                        PointF[] pointfs = rrec.GetVertices();
                        for (int j = 0; j < pointfs.Length; j++)
                            CvInvoke.Line(a, new Point((int)pointfs[j].X, (int)pointfs[j].Y), new Point((int)pointfs[(j + 1) % 4].X, (int)pointfs[(j + 1) % 4].Y), new MCvScalar(0, 0, 255, 255), 4);
                        return rrec;
                    }
    
                }
                return new RotatedRect();
            }
    
    //绘制矩形
     public static void DrawRotatedRect(RotatedRect rrec, Image<Bgr,byte> img)
            {
                PointF[] pointfs = rrec.GetVertices();
                for (int j = 0; j < pointfs.Length; j++)
                    CvInvoke.Line(img, new Point((int)pointfs[j].X, (int)pointfs[j].Y), new Point((int)pointfs[(j + 1) % 4].X, (int)pointfs[(j + 1) % 4].Y), new MCvScalar(0, 0, 255, 255), 4);
            }

    三、定位结果

    这里写图片描述

    这里写图片描述

    这里写图片描述

  • 相关阅读:
    停止Java线程,小心interrupt()方法
    SLG手游Java服务器的设计与开发——架构分析
    大数据Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解
    【转】阿里云主机购买使用教程
    使用 Apache MINA 开发高性能网络应用程序
    浅谈Java的Mina框架传递对象
    解析Mina代码三部曲
    java 子类继承父类 -- 重写、覆盖
    经度之战
    [USACO1.3]虫洞wormhole
  • 原文地址:https://www.cnblogs.com/cnsec/p/13286780.html
Copyright © 2011-2022 走看看