当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS身份证识别(OCR源码)详解及实例代码

IOS身份证识别(OCR源码)详解及实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

嗜血撒旦惹火妻,异界之轮转太一,安吉浴巾照

ios身份证识别(ocr源码)详解

最近项目用到身份证识别,在github上搜了一堆demo,在google上找了一堆代码,有能识别出证件照的,但是都是打包成.a的静态库,没有源码,我努力吃了几天书,有了一点研究成果,现在贴出来与大家分享,要是有更好的方法,希望大神指正,共同探讨解决方案。(以下代码本人亲测可用,正在进一步探索智能识别,如有兴趣,请加入)

这里用到了两个开源库:opencv、tesseractocrios,两个语言包chi_sim、eng。身份证识别的流程主要有:灰度化,阀值二值化,腐蚀,轮廓检测,取出身份证号码区域,tesseractocr识别文字。

身份证识别核心源码:

uiimage * image = [uiimage imagenamed:@"abc.png"];

//将uiimage转换成matcv::mat resultimage;

uiimagetomat(image, resultimage);

//转为灰度图

cvtcolor(resultimage, resultimage, 6);

//利用阈值二值化

cv::threshold(resultimage, resultimage, 100, 255, cv_thresh_binary);

//腐蚀,填充(腐蚀是让黑色点变大)

cv::mat erodeelement = getstructuringelement(cv::morph_rect, cv::size(140,140)); 

cv::erode(resultimage, resultimage, erodeelement);

//轮廊检测std::vector> contours;

//定义一个容器来存储所有检测到的轮廊

cv::findcontours(resultimage, contours, cv_retr_tree, cv_chain_approx_simple, cvpoint(0, 0));

//取出身份证号码区域

std::vectorrects;cv::rect numberrect = cv::rect(0,0,0,0); std::vector>::const_iterator itcontours = contours.begin();

for ( ; itcontours != contours.end(); ++itcontours) {

cv::rect rect = cv::boundingrect(*itcontours);

rects.push_back(rect);

nslog(@"位置分别为:x=%d,y=%d,width=%d,height%d",rect.x,rect.y,rect.width,rect.height);

//算法原理:如果新的区域范围宽度大于已赋值区域宽度,并且宽度为高度的五倍则赋予新值

    if (rect.width > numberrect.width && rect.width > rect.height * 5 && rect.height > 200 && rect.height < 300) {

    numberrect = rect;

    }

}

//定位成功成功,去原图截取身份证号码区域,并转换成灰度图、进行二值化处理

cv::mat matimage;

uiimagetomat(image, matimage);

resultimage = matimage(numberrect);

cvtcolor(resultimage, resultimage, cv::color_bgr2gray);

cv::threshold(resultimage, resultimage, 80, 255, cv_thresh_binary);

//将mat转换成uiimage

uiimage *numberimage = mattouiimage(resultimage);

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网