使用OpenCV+Tesseract识别图片文字

一、开源框架OpenCV和TesseractOCRiOS

OpenCV(完成图像处理技术)

OpenCV是一个开源的跨平台计算机视觉和机器学习库,通俗点的说,就是他给计算机提供了一双眼睛,一双可以从图片中获取信息的眼镜,从而完成人脸识别、身份证识别、去红眼、追踪移动物体等等的图像相关的功能。opencv官网

TesseractOCRiOS(完成文字识别技术)

Tesseract是目前可用的最准确的开源OCR引擎,可以读取各种格式的图片并将他们转换成各种语言文本。而TesseractOCRiOS则是针对iOS平台封装的Tesseract引擎库。

二、代码实现:使用OpenCV对图片进行处理,取出需要识别的文字区域,使用TesseractOCRiOS对图片文字进行识别,识别的速度与准确率跟图片预处理有关系,难点也是图片处理这一块。

//扫描身份证图片,并进行预处理,定位号码区域图片并返回

- (UIImage*)opencvScanCard:(UIImage*)image {

//将UIImage转换成Mat

cv::MatresultImage;

UIImageToMat(image, resultImage);

//转为灰度图

cvtColor(resultImage, resultImage,cv::COLOR_BGR2GRAY);

//利用阈值二值化

cv::threshold(resultImage, resultImage,100,255,CV_THRESH_BINARY);

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

cv::MaterodeElement =getStructuringElement(cv::MORPH_RECT,cv::Size(26,26));

cv::erode(resultImage, resultImage, erodeElement);

//轮廊检测

std::vector> contours;//定义一个容器来存储所有检测到的轮廊

cv::findContours(resultImage, contours,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));

//cv::drawContours(resultImage, contours, -1, cv::Scalar(255),4);

//取出身份证号码区域

std::vector rects;

cv::RectnumberRect =cv::Rect(0,0,0,0);

std::vector>::const_iteratoritContours = contours.begin();

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

cv::Rectrect =cv::boundingRect(*itContours);

rects.push_back(rect);

//算法原理

if(rect.width> numberRect.width&& rect.width> rect.height*5) {

numberRect = rect;

}

}

//身份证号码定位失败

if(numberRect.width==0|| numberRect.height==0) {

returnnil;

}

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

cv::MatmatImage;

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);

returnnumberImage;

}


//利用TesseractOCR识别文字

- (void)tesseractRecognizeImage:(UIImage*)image compleate:(CompleateBlock)compleate {

G8RecognitionOperation*operation = [[G8RecognitionOperationalloc]initWithLanguage:@"eng"];

operation.tesseract.engineMode=G8OCREngineModeTesseractOnly;

operation.tesseract.pageSegmentationMode=G8PageSegmentationModeAutoOnly;

operation.delegate=self;

//设置image

operation.tesseract.image= image;

//读取

operation.recognitionCompleteBlock= ^(G8Tesseract*tesseract) {

//执行回调

compleate(tesseract.recognizedText);

};

[self.operationQueueaddOperation:operation];

//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

//G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];

//tesseract.image = [image g8_blackAndWhite];

//tesseract.image = image;

//tesseract.engineMode = G8OCREngineModeTesseractOnly;

//tesseract.pageSegmentationMode = G8PageSegmentationModeAutoOnly;

//// Start the recognition

//[tesseract recognize];

////执行回调

//compleate(tesseract.recognizedText);

//});

}

推荐阅读更多精彩内容