OpenCV 文字检测与识别模块

该模块在扩展模块中,需自行下载
下载地址:https://github.com/opencv/opencv_contrib/tree/4.0.0
说明文档:
文字检测 https://docs.opencv.org/4.0.0/da/d56/group__text__detect.html
文字识别 https://docs.opencv.org/4.0.0/d8/df2/group__text__recognize.html
参考文章:https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/text_recognition_cnn.cpp
https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp

文字检测

1.textDetectorCNN

OpenCV的文字检测模块textDetectorCNN中使用了TextBoxes:具有单个深度神经网络的快速文本检测器 链接地址为: https://github.com/MhLiao/TextBoxes
其中已经训练过的文件:

函数名 内容 地址
modelWeightsFilename 描述分类器体系结构的prototxt文件的相对或绝对路径。 textbox.prototxt 在下载的扩展模块源码中opencv_contrib/modules/text/samples/textbox.prototxt
modelWeightsFilename 包含caffe-binary形式的模型的预训练权重的文件的相对或绝对路径。 TextBoxes_icdar13.caffemodel http://pan.baidu.com/s/1qY73XHq
        cv::Mat temp;src.convertTo(temp, CV_8UC3, 1);//src 输入图像cv::imshow("src", temp);dst1 = temp.clone();cv::Ptr<cv::text::TextDetectorCNN> detector= cv::text::TextDetectorCNN::create("textbox.prototxt", "TextBoxes_icdar13.caffemodel");std::vector < cv::Rect > boxes;//识别区域std::vector < float > sources;//评估分数detector->detect(temp, boxes, sources);float threshold = 0.5;for (int i = 0; i < boxes.size(); i++){if (sources[i] > threshold){cv::Rect rect = boxes[i];cv::rectangle(dst1, rect, cv::Scalar(255, 0, 0), 2);}}cv::imshow("Text detection result", dst1);

原图

结果

文字识别

1.OCRHolisticWordRecognizer

OCRHolisticWordRecognizer类提供了分段词语的功能。给定预定义的词汇表,使用DictNet来选择给定输入图像的最可能的词。

DictNet详细描述于:Max Jaderberg等:使用卷积神经网络阅读野外文本,IJCV 2015 http://arxiv.org/abs/1412.1842
模型文件下载地址:https://pan.baidu.com/s/1jl1g6lrNyCl8tM1BbLk6-Q bng0
https://pan.baidu.com/s/10yjRfrRALcQFLBfYKoXq5w 21gq

wordSpotter = (cv::text::OCRHolisticWordRecognizer::create("dictnet_vgg_deploy.prototxt", "dictnet_vgg.caffemodel", "dictnet_vgg_labels.txt"));
dst1 = src.clone();for (size_t i = 0; i < textBoxes.size(); i++){cv::Mat wordImg;cv::cvtColor(src(textBoxes[i]), wordImg, cv::COLOR_BGR2GRAY);std::string word;std::vector<float> confs;wordSpotter->run(wordImg, word, NULL, NULL, &confs);//检测cv::Rect currrentBox = textBoxes[i];cv::rectangle(dst1, currrentBox, cv::Scalar(0, 255, 255), 2, cv::LINE_AA);int baseLine = 0;cv::Size labelSize = cv::getTextSize(word, cv::FONT_HERSHEY_PLAIN, 1, 1, &baseLine);int yLeftBottom = currrentBox.y>labelSize.height? currrentBox.y: labelSize.height;cv::rectangle(dst1, cv::Point(currrentBox.x, yLeftBottom - labelSize.height),cv::Point(currrentBox.x + labelSize.width, yLeftBottom + baseLine), cv::Scalar(255, 255, 255), cv::FILLED);cv::putText(dst1, word, cv::Point(currrentBox.x, yLeftBottom), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 0), 1, cv::LINE_AA);}cv::imshow("Text recognition", dst1);

结果

(极值区域)文本定位与识别法

与上方的卷积神经网络识别,存在识别不稳定的问题。

void textRecognize(int REGION_TYPE,int GROUPING_ALGORITHM){cv::Mat gray;std::vector<cv::Mat> channels;cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);std::vector<std::vector<cv::text::ERStat> > regions(2);channels.clear();channels.push_back(gray);channels.push_back(255 - gray);regions[0].clear();regions[1].clear();switch (REGION_TYPE){case 0: // ERStatsparallel_for_(cv::Range(0, (int)channels.size()), Parallel_extractCSER(channels, regions, er_filters1, er_filters2));break;case 1: // MSERstd::vector<std::vector< cv::Point> > contours;std::vector<cv::Rect> bboxes;cv::Ptr<cv::MSER> mser = cv::MSER::create(21, (int)(0.00002*gray.cols*gray.rows), (int)(0.05*gray.cols*gray.rows), 1, 0.7);mser->detectRegions(gray, contours, bboxes);//Convert the output of MSER to suitable input for the grouping/recognition algorithmsif (contours.size() > 0)MSERsToERStats(gray, contours, regions);break;}// Detect character groupsstd::vector< std::vector<cv::Vec2i> > nm_region_groups;textBoxes.clear();switch (GROUPING_ALGORITHM){case 0: // exhaustive_searcherGrouping(src, channels, regions, nm_region_groups, textBoxes, cv::text::ERGROUPING_ORIENTATION_HORIZ);break;case 1: //multiorientederGrouping(src, channels, regions, nm_region_groups, textBoxes, cv::text::ERGROUPING_ORIENTATION_ANY, "./trained_classifier_erGrouping.xml", 0.5);break;}/*Text Recognition (OCR)*/int bottom_bar_height = src.rows / 7;cv::copyMakeBorder(src, dst1, 0, bottom_bar_height, 0, 0, cv::BORDER_CONSTANT, cv::Scalar(150, 150, 150));float scale_font = (float)(bottom_bar_height / 85.0);std::vector<cv::Mat> detections;//只有字体的图片cv::Mat temp;src.convertTo(temp, CV_8UC1, 1);for (int i = 0; i < (int)textBoxes.size(); i++)//字体的矩形数量{cv::rectangle(dst1, textBoxes[i].tl(), textBoxes[i].br(), cv::Scalar(255, 255, 0), 3);cv::Mat group_img = cv::Mat::zeros(src.rows + 2, src.cols + 2, CV_8UC1);er_draw(channels, regions, nm_region_groups[i], group_img);group_img(textBoxes[i]).copyTo(group_img);copyMakeBorder(group_img, group_img, 15, 15, 15, 15, cv::BORDER_CONSTANT, cv::Scalar(0));detections.push_back(group_img);}cv::imshow("text find", dst1);std::vector<std::string> outputs((int)detections.size());std::vector< std::vector<cv::Rect> > boxes((int)detections.size());std::vector< std::vector<std::string> > words((int)detections.size());std::vector< std::vector<float> > confidences((int)detections.size());float min_confidence1 = 0.f, min_confidence2 = 0.f;min_confidence1 = 51.f;min_confidence2 = 60.f;// parallel process detections in batches of ocrs.size() (== num_ocrs)for (int i = 0; i < (int)detections.size(); i = i + (int)num_ocrs){cv::Range r;if (i + (int)num_ocrs <= (int)detections.size())r = cv::Range(i, i + (int)num_ocrs);elser = cv::Range(i, (int)detections.size());// NM_chain_features + KNNparallel_for_(r, Parallel_OCR<cv::text::OCRHMMDecoder>(detections, outputs, boxes, words, confidences, decoders));}showText(outputs);for (int i = 0; i < (int)detections.size(); i++){outputs[i].erase(remove(outputs[i].begin(), outputs[i].end(), '\n'), outputs[i].end());//cout << "OCR output = \"" << outputs[i] << "\" length = " << outputs[i].size() << endl;if (outputs[i].size() < 3)continue;for (int j = 0; j < (int)boxes[i].size(); j++){boxes[i][j].x += textBoxes[i].x - 15;boxes[i][j].y += textBoxes[i].y - 15;if ((words[i][j].size() < 2)  ||((words[i][j].size() == 2) && (words[i][j][0] == words[i][j][1]))  ||isRepetitive(words[i][j]))continue;cv::rectangle(dst1, boxes[i][j].tl(), boxes[i][j].br(), cv::Scalar(255, 0, 255), 3);cv::Size word_size = cv::getTextSize(words[i][j], cv::FONT_HERSHEY_SIMPLEX, (double)scale_font, (int)(3 * scale_font), NULL);cv::rectangle(dst1, boxes[i][j].tl() - cv::Point(3, word_size.height + 3), boxes[i][j].tl() + cv::Point(word_size.width, 0), cv::Scalar(255, 0, 255), -1);cv::putText(dst1, words[i][j], boxes[i][j].tl() - cv::Point(1, 1), cv::FONT_HERSHEY_SIMPLEX, scale_font, cv::Scalar(255, 255, 255), (int)(3 * scale_font));}}cv::imshow("result", dst1);}void er_draw(std::vector<cv::Mat> &channels, std::vector<std::vector<cv::text::ERStat> > &regions, std::vector<cv::Vec2i> group, cv::Mat& segmentation){for (int r = 0; r < (int)group.size(); r++){cv::text::ERStat er = regions[group[r][0]][group[r][1]];if (er.parent != NULL) // deprecate the root region{int newMaskVal = 255;int flags = 4 + (newMaskVal << 8) + cv::FLOODFILL_FIXED_RANGE + cv::FLOODFILL_MASK_ONLY;cv::floodFill(channels[group[r][0]], segmentation, cv::Point(er.pixel%channels[group[r][0]].cols, er.pixel / channels[group[r][0]].cols),cv::Scalar(255), 0, cv::Scalar(er.level), cv::Scalar(0), flags);}}}bool isRepetitive(const std::string& s){int count = 0;int count2 = 0;int count3 = 0;int first = (int)s[0];int last = (int)s[(int)s.size() - 1];for (int i = 0; i < (int)s.size(); i++){if ((s[i] == 'i') ||(s[i] == 'l') ||(s[i] == 'I'))count++;if ((int)s[i] == first)count2++;if ((int)s[i] == last)count3++;}if ((count > ((int)s.size() + 1) / 2) || (count2 == (int)s.size()) || (count3 > ((int)s.size() * 2) / 3)){return true;}return false;}

原图

文本定位图

文本识别图

OpenCV 文字检测与识别模块相关推荐

  1. 基于YOLOv3 与CRNN的中文自然场景文字检测与识别

    (欢迎关注"我爱计算机视觉"公众号,一个有价值有深度的公众号~) 52CV君曾经分享过多篇关于文字检测与识别的文章: 华科白翔老师团队ECCV2018 OCR论文:Mask Tex ...

  2. 金连文:“文字检测与识别:现状及展望” | CAAI AIDL 演讲实录

    点击我爱计算机视觉标星,更快获取CVML新技术 CAAI原创 丨 作者金连文 转自中国人工智能学会,52CV获得金老师授权转载,严禁二次转载. 8月31日-9月1日,由中国人工智能学会主办,华中科技大 ...

  3. 文字检测与识别资料整理

    博主关注文字检测和识别,资料整理和论文解读都非常详细: https://www.cnblogs.com/lillylin/p/6893500.html#4033329 博主的阅读习惯,积累和输出输出: ...

  4. 中文文字检测及识别(ORC)

    中文文字检测及识别(ORC) https://github.com/471417367/chinese_ocr_api 首先基于CTPN检测到文字(可以是中英文以及数字),然后基于RCNN进行文字识别 ...

  5. 【项目实践】中英文文字检测与识别项目(CTPN+CRNN+CTC Loss原理讲解)

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自:opencv学堂 OCR--简介 文字识别也是图像领域一 ...

  6. 实战:OpenVINO+OpenCV 文本检测与识别

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自|OpenCV学堂 模型介绍 文本检测模型 OpenVIN ...

  7. OpenVINO+OpenCV 文本检测与识别

    本文转载自OpenCV学堂. 1 模型介绍 文本检测模型 OpenVINO支持场景文字检测是基于MobileNet的PixelLink模型,该模型有两个输出,分别是分割输出与bounding Boxe ...

  8. 文字检测与识别1-MSER

    导语 文字识别在现实场景中的用途非常广泛,现在已经有很多公司将这项技术用于实际中.比如车牌识别,图片转换成文档,拍照搜题,拍照翻译等.这让很多人有了错觉,感觉文字识别的技术已经炉火纯青,可以广泛应用. ...

  9. 文字检测与识别项目整理

    一.文字检测 (1)方法 方法主要是基于EAST和Refinenet进行的.首先,图像经过Resnet50得到不同层的特征,然后通过Refinet的特征融合方式融合多层特征,接着,经过多任务回归预测每 ...

最新文章

  1. 工信部:推动窄带物联网、5G泛在信息基础设施
  2. 在线白板,基于socket.io的多人在线协作工具
  3. python网络编程—TCP协议(一)
  4. Windows保护模式学习笔记(六)—— 10-10-12分页
  5. 二阶振荡环节的谐振频率_困惑了很久的串联/并联谐振电路详解
  6. 厨房电器机械EN60335-2-14检测标准及项目
  7. 如何制作和使用自签名证书
  8. 2017计算机一级考试试题题库,2017年计算机一级考试题库试题及答案
  9. hikaricp mysql_JAVA连接数据库 #03# HikariCP
  10. Python打开文件权限及编码方式
  11. PS教程:清凉一下美女海报设计教程及素材
  12. @Value 注解用法
  13. win32Day06:控件
  14. Element表格数据居中
  15. 有无孔孟之道,太阳照常升起
  16. eBay Inc(EBAY)2020年第三季度收益电话会议记录
  17. insmod depmod modprobe的区别及用法
  18. 【java】java学习笔记之java oop(面向对象)
  19. MIGO/CO11N 批次创建增强 自定义批次号创建规则
  20. SQL 中判断条件的先后顺序,会引起索引失效么?

热门文章

  1. AD画PCB常规问题分析
  2. 软件测试基础理论学习和小案例(一)
  3. 从零开始写第一个Android应用程序
  4. thinkphp6-学习记录-应用手册
  5. 华为交换机 STP MSTP BPDU保护 边缘端口 BPDU过滤 根保护 环路保护 TC保护
  6. “锟斤拷“的前世今生
  7. The server time zone value ‘锟叫癸拷锟斤拷\u05FC时锟斤拷‘ is unrecognized or represents more than one time zone
  8. h5活动是什么意思_H5是什么,怎么用H5做运营活动?
  9. 避免2.4GHz ISM频段各种类型无线设备干扰的技术【转】
  10. VMware连接U盘后无法显示U盘