Build OpenCV text(OCR) module on windows
Background.
AOI software needs to use the OCR feature to recognize the texts on the chips. Because our vision software is based on OpenCV, so the first choice is text module in opencv_contrib.
Procedures.
1. OCR module is not in standard OpenCV package. It is in text module of OpenCV_Contrib. It can be downloaded from opencv_contrib.
2. The core of OCR is using Tesseract, and Tesseract depends on Leptonica, so need to build Leptonica and Tesseract first.
3. Get the Leptonica from https://github.com/charlesw/tesseract-vs2012. This solution can directly build. The output is liblept171d.dll and liblept171d.lib.
4. Get the Tesseract from https://github.com/tesseract-ocr/tesseract. Create the .\include\leptonica folder, copy all the header file from .\tesseract-vs2012\liblept\include (The root folder is in step 3) to it. Create the .\lib folder, copy all the step 3 build generated files(The .dll and .lib) in .\tesseract-vs2012\build\lib to it. Set the project property of tesseract, change the include folder path "..\..\..\include" and "..\..\..\include\leptonica" to "..\..\include" and "..\..\include\leptonica". Then can build the Tessrect project, the output is libtesseract304d.dll and libtesseract304d.lib.
The AddIdTohOCR has compile error, change the content as below can solve the problem.
static void AddIdTohOCR(STRING* hocr_str, const std::string base, int num1, int num2) {
unsigned long bufsize = base.length() + * kMaxIntSize;
char *id_buffer = new char[bufsize];
if (num2 >= ) {
snprintf(id_buffer, bufsize - , "%s_%d_%d", base.c_str(), num1, num2);
}
else {
snprintf(id_buffer, bufsize - , "%s_%d", base.c_str(), num1);
}
id_buffer[bufsize - ] = '\0';
*hocr_str += " id='";
*hocr_str += id_buffer;
*hocr_str += "'";
delete[]id_buffer;
}
5. Use CMake to config the OpenCV solution. Copy the text module from opencv_contrib to .\OpenCV\sources\modules. Run Cmake_Gui, there are 3 options need to set. Lept_library, Tesseract_Include_Dir, Tesseract_Library. Tesseract_Include_Dir set to ../.../tesseract/API. After set, can run CMake to config and generate the solution.
6. Open the OpenCV solution. Open the .\modules\text\src\precomp.hpp file, change the include path to as below.
#ifdef HAVE_TESSERACT
//#include <tesseract/baseapi.h>
//#include <tesseract/resultiterator.h>
#include "baseapi.h"
#include "resultiterator.h"
#endif
If there are header files can not find errors, find and copy them from tesseract to the tesseract/API folder. As i test, below files need to be copied.
#include ""resultiterator.h""
#include "platform.h"
#include "apitypes.h"
#include "thresholder.h"
#include "unichar.h"
#include "tesscallback.h"
#include "publictypes.h"
#include "pageiterator.h"
#include "resultiterator.h"
#include "host.h"
#include "ltrresultiterator.h"
There may be a compile error in function static double NFA(int n, int k, double p, double logNT) with std::numeric_limits<double>::min(); Add below code before the function to fix it.
#undef max
#undef min
7. Download the language test data from https://github.com/tesseract-ocr/tessdata. What i use is the eng.traineddata. Put it to .\tesseract\tessdata.
8. After build OpenCV successfully, then you can create the TestOpenCV project with the below function, before running it, need to copy the liblept171d.dll and libtesseract304d.dll to the output folder(where the exe file is put).
using OCRTesseract = cv::text::OCRTesseract;
void TestOCR()
{
cv::Mat mat = cv::imread(".\\data\\OCRTest.png");
if ( mat.empty() )
return; std::string output_text;
char *dataPath = "C:/tesseract-build/tesseract/tessdata";
cv::Ptr<OCRTesseract> ptrOcr = OCRTesseract::create(dataPath);
ptrOcr->run(mat, output_text );
cout << output_text << endl;
}
9. The Tesseract is sensitive to the text orientation. So need to make the text face up to be better recognized.
Build OpenCV text(OCR) module on windows的更多相关文章
- windows 10 上源码编译OpenCV并支持CUDA | compile opencv with CUDA support on windows 10
本文首发于个人博客https://kezunlin.me/post/6580691f/,欢迎阅读! compile opencv with CUDA support on windows 10 Ser ...
- 应用OpenCV进行OCR字符识别
opencv自带一个字符识别的例子,它的重点不是OCR字符识别,而主要是演示机器学习的应用.它应用的是UCI提供的字符数据(特征数据). DAMILES在网上发布了一个应用OpenCV进行OCR的例子 ...
- Python:ModuleNotFoundError: No module named 'windows'
pymouse安装后,又出现了ModuleNotFoundError: No module named 'windows'的错误 解决: 下载安装pyhook:http://www.lfd.uci.e ...
- OpenCV学习(1)-安装(Windows)
下载安装 在这里下载.我下载了2.4.9的Windows版本.双击安装即可. 配置环境变量 配置环境变量的目的是为了让系统找到OpenCV的动态链接库.因此需要把动态链接库添加到系统环境变量PATH中 ...
- python导入opencv解决no module named cv2问题
最近ubuntu用的比较多,在写神经网络代码时也会经常接触到python,但是python的环境配置确实是个头疼的问题. 尤其是接触到opencv时,需要导入opencv的库文件,网上也有很多方法,本 ...
- [Javascript] Add a browser build to an npm module
In this lesson, we're going to use webpack to create a UMD (Universal Module Definition) build of ou ...
- How to Build MySQL from Source Code on Windows & compile MySQL on win7+vs2010
Not counting obtaining the source code, and once you have the prerequisites satisfied, [Windows] use ...
- 自定义内建模块 - Python Build Your Own Built-In Module
在 python 中, 用户可以通过 py 文件创建自定义的 module, 也可以通过 C 创建 dll, 扩展 python module. 当用户在一个正在编辑的模块 module 中, 引入( ...
- <学习opencv>跨平台和本机windows
/*=========================================================================*/ // 跨平台和本机Windows /*=== ...
随机推荐
- VS中Debug和Realease、及静态库和动态库的区别整理(转)
原文出自:http://www.cnblogs.com/chensu/p/5632486.html 一.Debug和Realease区别产生的原因 Debug 通常称为调试版本,它包含调试信息,并且不 ...
- 创建COM对象时遭遇 800702e4
SolidEdge st5(x64) SDK /vs2012/win8.1 x64 西夏普创建SEApplication COM对象时遭遇800702e4.翻来覆去的调vs进程的权限,调se进程的权限 ...
- winform里操作打开在panel里的form窗体,子窗体操作同级子窗体或者父窗体的方法
最近开始了一个winform项目,原先一直都是web项目.遇到个问题,就是在框架内,左侧和中间的main都是用panel来实现的form,就是把form窗体打开到panel里,实现左侧是导航,中间是操 ...
- sql 跨库查询备忘笔记
select * from OPENDATASOURCE( 'SQLOLEDB', 'Data Source=远程ip;User ID=sa;Password=密码' ).库名.dbo.表名 inse ...
- hdu 5382 GCD?LCM!
先考虑化简f函数 发现,f函数可以写成一个递归式,化简后可以先递推求出所有f函数的值, 所以可以先求出所有S函数的值,对于询问,O(1)回答 代码: //File Name: hdu5382.cpp ...
- SpringMVC使用的几个要点
1.使用 @RequestParam("username") 来对应参数名的时候,这个参数必须要传入,否则会报错.没加@RequestParam则可传可不传 @RequestMap ...
- C#如何获取CPU处理器核心数量 z
有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核心数量和逻辑处理器数量,这些可以不同.两颗双核超线程(启用)处理器的机器情况下有:2个物理处理器.4个核心和8个逻辑处理器. 逻辑处理器 ...
- hbase基础-系统架构
HBase 系统架构 HBase是Apache Hadoop的数据库,能够对大型数据提供随机.实时的读写访问.HBase的目标是存储并处理大型的数据.HBase是一个开源的,分布式的,多版本的,面向列 ...
- 如何将Debug文件夹下的资源打包成一个EXE文件直接执行
前言:前段时间写了个小程序,想分享给好友看看,可所以资源都放在Debug文件夹下,整个文件夹发给人家这也太……,为了显得稍微专业一点,想把它们打包一个EXE文件执行,因为我见到到这样的程序,直接一个E ...
- 对《神奇的C语言》文中例子 5 代码的分析讨论
在春节前,我曾经参与在<神奇的C语言>一文中的例子(5)的讨论,但限于评论内容的有限,现在本文再次对这个问题单独讨论.(此问题原貌,详见<神奇的C语言>,这里我将原文中的代码稍 ...