【C#】身份证识别(一):身份证号定位
2017-09-22 12:48
260 查看
完整项目地址:https://gitee.com/xgpxg/ICRS
二值化图像。 利用
提取轮廓。
轮廓筛选,提取身份证矩形区域。身份证号的宽高比约为8,将比值为7~9的轮廓保留,并绘制矩形
一、图像预处理
灰度化图像。二值化图像。 利用
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); }
三、定位结果
相关文章推荐
- 【C#】身份证识别(三):身份证信息识别
- C# 打开指定目录并定位到文件
- 在C#程序中对MessageBox进行定位的方法
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#实现的18位身份证格式验证算法
- C#随机生成18位身份证号码
- 用.NET(C#)实现IP定位地理位置
- 特别收集一些身份证验证算法的C#版 C#实现的18位身份证格式验证算法
- 使用C#实现身份证号码15位到18位的转换
- C#实现身份证号码验证的方法
- C# 身份证号验证
- 身份证格式验证(含15位转换到18位)C#
- GPS定位,经纬度附近地点查询–C#实现方法
- 【转】C# 使用Google API进行手机基站定位资料整理
- 黄聪:C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#身份证识别相关技术
- c# 版 18位身份证严格验证及 15位转18位
- C#实现的18位身份证格式验证算法
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#-winform地图定位的实现-百度地图API