c++中使用Tesseract-OCR

1.Tesseract-OCR引擎简介

OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程。

Tesseract的OCR引擎最先由HP实验室于1985年开始研发,至1995年时已经成为OCR业内最准确的三款识别引擎之一。然而,HP不久便决定放弃OCR业务,Tesseract也从此尘封。

数年以后,HP意识到,与其将Tesseract束之高阁,不如贡献给开源软件业,让其重焕新生--2005年,Tesseract由美国内华达州信息技术研究所获得,并求诸于Google对Tesseract进行改进、消除Bug、优化工作。



2.Tesseract-OCR的下载安装

google下载地址:http://code.google.com/p/tesseract-ocr    

需要下载安装程序(tesseract-ocr-setup-3.02.02.exe)和语言库,默认有英文的语言库只能识别英文,所以还需要下载中文语言库,  

由于google经常打不开,所以通过代理下载后,上传到了CSDN,

安装程序下载地址:http://download.csdn.net/detail/whatday/7740469

简体中文语言库下载地址:http://download.csdn.net/detail/whatday/7740531

繁体中文语言库下载地址:http://download.csdn.net/detail/whatday/7740429

安装tesseract-ocr-setup-3.02.02.exe 默认安装就可以了,


因为如果选择了其他项,安装程序会自动从网上下载,语言库和其他文件,有很大一部分是从google下载,由于不能打开所以会出错,先默认安装上,需要什么文件再下载就行。

我系统是windows8.1 默认安装好后目录如下:


其中tesseract.exe是主程序, tessdata目录是存放语言文件 和 配置文件的,下载或自己生成的语言文件放到此目录里就可以了。



3.Tesseract-OCR的命令行使用

打开DOS界面,输入tesseract:


如果出现如上输出,表示安装正常。

我准备了一张验证码1.png放在D盘根目录下 ,简单的执行验证码识别


其中 1.png是验证码图片 result是结果文件的名称 默认是.TXT文件 执行成功后会在验证码图片所在位置生成result.txt 打开结果为:



命令详解:

Usage:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]
pagesegmode values are:
0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.
-l lang and/or -psm pagesegmode must occur before anyconfigfile.


tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]

tesseract    图片名  输出文件名 -l 字库文件 -psm pagesegmode 配置文件

例如:

tesseract code.jpg result  -l chi_sim -psm 7 nobatch

-l chi_sim 表示用简体中文字库(需要下载中文字库文件,解压后,存放到tessdata目录下去,字库文件扩展名为  .raineddata 简体中文字库文件名为:  chi_sim.traineddata)

-psm 7 表示告诉tesseract code.jpg图片是一行文本  这个参数可以减少识别错误率.  默认为 3

configfile 参数值为tessdata\configs 和  tessdata\tessconfigs 目录下的文件名



4.Tesseract-OCR的QA合集

A.ImageMagick是什么?

ImageMagick是一个用于查看、编辑位图文件以及进行图像格式转换的开放源代码软件套装

我在这里之所以提到ImageMagick是因为某些图片格式需要用这个工具来转换。

B.Leptonica 是什么?

Leptonica 是一图像处理与图像分析工具,tesseract依赖于它。而且不是所有的格式(如jpg)都能处理,所以我们需要借助imagemagick做格式转换。

Here's a summary of compression support and limitations:
    - All formats except JPEG support 1 bpp binary.
    - All formats support 8 bpp grayscale (GIF must have a colormap).
    - All formats except GIF support 24 bpp rgb color.
    - All formats except PNM support 8 bpp colormap.
    - PNG and PNM support 2 and 4 bpp images.
    - PNG supports 2 and 4 bpp colormap, and 16 bpp without colormap.
    - PNG, JPEG, TIFF and GIF support image compression; PNM and BMP do not.
    - WEBP supports 24 bpp rgb color.

C.提高图片质量?

识别成功率跟图片质量关系密切,一般拿到后的验证码都得经过灰度化,二值化,去噪,利用imgick就可以很方便的做到.

convert -monochrome foo.png bar.png #将图片二值化

D.我只想识别字符和数字?

结尾仅需要加digits

命令实例:tesseract imagename outputbase digits

E.训练你的tesseract

不得不说,tesseract英文识别率已经很不错了(现有的tesseract-data-eng),但是验证码识别还是太鸡肋了。但是请别忘记,tesseract的智能识别是需要训练的.

F.命令执行出现empty page!!错误

严格来说,这不是一个bug(tesseract 3.0),出现这个错误是因为tesseract搞不清图像的字符布局

-psm N
    Set Tesseract to only run a subset of layout analysis and assume a certain form of image. The options for N are:


    0 = Orientation and script detection (OSD) only.
    1 = Automatic page segmentation with OSD.
    2 = Automatic page segmentation, but no OSD, or OCR.
    3 = Fully automatic page segmentation, but no OSD. (Default)
    4 = Assume a single column of text of variable sizes.
    5 = Assume a single uniform block of vertically aligned text.
    6 = Assume a single uniform block of text.
    7 = Treat the image as a single text line.
    8 = Treat the image as a single word.
    9 = Treat the image as a single word in a circle.
    10 = Treat the image as a single character.

对于我们的验证码a.tif排列来说,采用-psm 7(single text line)比较合适。



5.Tesseract-OCR的训练方法

A.使用jTessBoxEditor工具

1.下载地址:http://download.csdn.net/detail/whatday/7740739

这个工具是用来训练样本用的,由于该工具是用JAVA开发的,需要安装JAVA虚拟机1.6才能运行。

2. 获取样本图像。用画图工具绘制了5张0-9的文样本图像(当然样本越多越好),如下图所示:

  

  


3.合并样本图像。运行jTessBoxEditor工具,在点击菜单栏中Tools--->Merge TIFF。在弹出的对话框中选择样本图像(按Shift选择多张),合并成num.font.exp0.tif文件。

4.生成Box File文件。打开命令行,执行命令:

  tesseract.exe num.font.exp0.tif num.font.exp0 batch.nochop makebox

  生成的BOX文件为num.font.exp0.box,BOX文件为Tessercat识别出的文字和其坐标。

注:Make Box File的命令格式为:

  tesseract [lang].[fontname].exp[num].tif [lang].[fontname].exp[num] batch.nochop makebox

其中lang为语言名称,fontname为字体名称,num为序号,可以随便定义。

5.文字校正。运行jTessBoxEditor工具,打开num.font.exp0.tif文件(必须将上一步生成的.box和.tif样本文件放在同一目录),如下图所示。可以看出有些字符识别的不正确,可以通过该工具手动对每张图片中识别错误的字符进行校正。校正完成后保存即可。




6.定义字体特征文件。Tesseract-OCR3.01以上的版本在训练之前需要创建一个名称为font_properties的字体特征文件。

font_properties不含有BOM头,文件内容格式如下:

<fontname> <italic> <bold> <fixed> <serif> <fraktur>

其中fontname为字体名称,必须与[lang].[fontname].exp[num].box中的名称保持一致。<italic> 、<bold> 、<fixed> 、<serif>、 <fraktur>的取值为1或0,表示字体是否具有这些属性。

这里在样本图片所在目录下创建一个名称为font_properties的文件,用记事本打开,输入以下下内容:

font 0 0 0 0 0
这里全取值为0,表示字体不是粗体、斜体等等。

7.生成语言文件。在样本图片所在目录下创建一个批处理文件,输入如下内容。

rem 执行改批处理前先要目录下创建font_properties文件

echo Run Tesseract for Training..
tesseract.exe num.font.exp0.tif num.font.exp0 nobatch box.train

echo Compute the Character Set..
unicharset_extractor.exe num.font.exp0.box
mftraining -F font_properties -U unicharset -O num.unicharset num.font.exp0.tr

echo Clustering..
cntraining.exe num.font.exp0.tr

echo Rename Files..
rename normproto num.normproto
rename inttemp num.inttemp
rename pffmtable num.pffmtable
rename shapetable num.shapetable 

echo Create Tessdata..
combine_tessdata.exe num.

将批处理通过命令行执行。执行后的结果如下:


需确认打印结果中的Offset 1、3、4、5、13这些项不是-1。这样,一个新的语言文件就生成了。

num.traineddata便是最终生成的语言文件,将生成的num.traineddata拷贝到Tesseract-OCR-->tessdata目录下。可以用它来进行字符识别了。

训练前:

1.准备一副待识别的图像,这里用画图工具随便写了一串数字,保存为number.jpg,如下图所示:
        

2.  打开命令行,定位到Tesseract-OCR目录,输入命令:

 tesseract.exe number.jpg result -l eng

     其中result表示输出结果文件txt名称,eng表示用以识别的语言文件为英文。

3.  打开Tesseract-OCR目录下的result.txt文件,看到识别的结果为7542315857,有3个字符识别错误,识别率还不是很高,那有没有什么方法来提供识别率呢?Tesseract提供了一套训练样本的方法,用以生成自己所需的识别语言库。下面介绍一下具体训练样本的方法。


     


训练后:

用训练后的语言库识别number.jpg文件, 打开命令行,定位到Tesseract-OCR目录,输入命令:

tesseract.exe number.jpg result -l eng

识别结果如如图所示,可以看到识别率提高了不少。通过自定义训练样本,可以进行图形验证码、车牌号码识别等。感兴趣的朋友可以研究研究。




B.使用 CowBoxer工具

下载地址为:http://download.csdn.net/detail/whatday/7740815

第一步生成第一个 box 文件
演示中将 Tesseract 解压到了 E:\tesseract-ocr 目录。然后在该目录中建立了一个 build 目录用于存放原始数据和训练过程中生成的文件。原始图片数据一个有 3 个 (test.001.tif - test.003.tif):





首先生成第一个图片 test.001.tif 的 box 文件,这里使用官方的 eng 语言数据进行文字识别:

E:\tesseract-ocr\build>..\tesseract test.001.tif test.001  -l eng batch.nochop makebox
Tesseract Open Source OCR Engine with Leptonica
Number of found pages:  1.

执行完这个命令之后,build 目录下就生成了一个 test.001.box。使用 CowBoxer 打开这个 box 文件,CowBoxer 会自动找到同名的 tif 文件显示出来。



CowBoxer 的使用方法可以看 Help -> About 中的说明。修改完成之后 File -> Save box file 保存文件。

生成初始的 traineddata

接下来使用这一个 box 文件先生成一个 traineddata,在接下来生成其他图片的 box 文件时,使用这个 traineddata 有利于提高识别的正确率,减少修改次数。

..\tesseract test.001.tif test.001 nobatch box.train
..\training\unicharset_extractor test.001.box
..\training\mftraining  -U unicharset  -O test.unicharset test.001.tr
..\training\cntraining test.001.tr
rename normproto test.normproto
rename Microfeat test.Microfeat
rename inttemp test.inttemp
rename pffmtable test.pffmtable
..\training\combine_tessdata test.

在 build 目录下执行完这一系列命令之后,就生成了可用的 test.traineddata。

生成其余 box 文件

将上一步生成的 test.traineddata 移动到 tesseract-ocr\tessdata 目录中,接下来生成其他 box 文件时就可以通过 -l test 参数使用它了。

..\tesseract test.002.tif test.002  -l  test batch.nochop makebox
..\tesseract test.003.tif test.003  -l  test batch.nochop makebox

这里仅仅是使用 3 个原始文件作为例子。实际制作训练文件时,什么时候生成一个 traineddata 根据情况而定。中途生成 traineddata 的目的只是为了提高文字识别的准确率,使后面生成的 box 文件能少做修改。

生成最终的 traineddata

在所有的 box 都制作完成后,就可以生成最终的 traineddata 了。

..\tesseract test.001.tif test.001 nobatch box.train
..\tesseract test.002.tif test.002 nobatch box.train
..\tesseract test.003.tif test.003 nobatch box.train
..\training\unicharset_extractor test.001.box test.002.box test.003.box
..\training\mftraining  -U unicharset  -O test.unicharset test.001.tr test.002.tr test.003.tr
..\training\cntraining test.001.tr test.002.tr test.003.tr
rename normproto test.normproto
rename Microfeat test.Microfeat
rename inttemp test.inttemp
rename pffmtable test.pffmtable
..\training\combine_tessdata test.

在文件较多时可以用程序生成这种脚本执行。



未完。。。

  • 7
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Note that this is a text-only and possibly out-of-date version of the wiki ReadMe, which is located at: http://code.google.com/p/tesseract-ocr/wiki/ReadMe Introduction ============ This package contains the Tesseract Open Source OCR Engine. Originally developed at Hewlett Packard Laboratories Bristol and at Hewlett Packard Co, Greeley Colorado, all the code in this distribution is now licensed under the Apache License: ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. Dependencies and Licenses ========================= Leptonica is required. (www.leptonica.com). Tesseract no longer compiles without Leptonica. Libtiff is no longer required as a direct dependency. Installing and Running Tesseract -------------------------------- All Users Do NOT Ignore! The tarballs are split into pieces. tesseract-x.xx.tar.gz contains all the source code. tesseract-x.xx.<lang>.tar.gz contains the language data files for <lang>. You need at least one of these or Tesseract will not work. Note that tesseract-x.xx.tar.gz unpacks to the tesseract-ocr directory. tesseract-x.xx.<lang>.tar.gz unpacks to the tessdata directory which belongs inside your tesseract-ocr directory. It is therefore best to download them into your tesseract-x.xx directory, so you can use unpack here or equivalent. You can unpack as many of the language packs as you care to, as they all contain different files. Note that if you are using make install you should unpack your language data to your source tree before you run make install. If you unpack them as root to the destination directory of make install, then the user ids and access permissions might be messed up. boxtiff-2.xx.<lang>.tar.gz contains data that was used in training for those that want to do their own training. Most users should NOT download these files. Instructions for using the training tools are documented separately at Tesseract wiki http://code.google.com/p/tesseract-ocr/w/list Windows ------- Please use installer (for 3.00 and above). Tesseract is library with command line interface. If you need GUI, please check AddOns wiki page http://code.google.com/p/tesseract-ocr/wiki/AddOns#GUI If you are building from the sources, the recommended build platform is VC++ Express 2008 (optionally 2010). The executables are built with static linking, so they stand more chance of working out of the box on more windows systems. The executable must reside in the same directory as the tessdata directory or you need to set up environment variable TESSDATA_PREFIX. Installer will set it up for you. The command line is: tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfiles...] If you need interface to other applications, please check wrapper section on AddOns wiki page: http://code.google.com/p/tesseract-ocr/wiki/AddOns#Tesseract_3.0x Non-Windows (or Cygwin) ----------------------- You have to tell Tesseract through a standard unix mechanism where to find its data directory. You must either: ./autogen.sh ./configure make make install sudo ldconfig to move the data files to the standard place, or: export TESSDATA_PREFIX="directory in which your tessdata resides/" In either case the command line is: tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfiles...] New there is a tesseract.spec for making rpms. (Thanks to Andrew Ziem for the help.) It might work with your OS if you know how to do that. If you are linking to the libraries, as Ocropus does, please link to libtesseract_api. History ======= The engine was developed at Hewlett Packard Laboratories Bristol and at Hewlett Packard Co, Greeley Colorado between 1985 and 1994, with some more changes made in 1996 to port to Windows, and some C++izing in 1998. A lot of the code was written in C, and then some more was written in C++. Since then all the code has been converted to at least compile with a C++ compiler. Currently it builds under Linux with gcc4.4.3 and under Windows with VC++2008. The C++ code makes heavy use of a list system using macros. This predates stl, was portable before stl, and is more efficient than stl lists, but has the big negative that if you do get a segmentation violation, it is hard to debug. The most recent change is that Tesseract can now recognize 39 languages, including Arabic, Hindi, Vietnamese, plus 3 Fraktur variants is fully UTF8 capable, and is fully trainable. See TrainingTesseract for more information on training. Tesseract was included in UNLV's Fo
Tesseract-OCR是一个开源的OCR(光学字符识别)引擎,它可以用来识别图像的文本。下面是使用C语言配置Tesseract-OCR的步骤: 1. 首先,你需要安装Tesseract-OCR引擎。你可以从Tesseract的官方GitHub仓库(https://github.com/tesseract-ocr/tesseract)下载源代码并按照其README的指示进行安装。 2. 安装完成后,你需要下载训练数据(语言数据集)。你可以从Tesseract的官方GitHub仓库(https://github.com/tesseract-ocr/tessdata)下载需要的语言数据集。将这些数据集放在你的项目目录。 3. 在你的C语言项目,你需要包含tesseract公共头文件。在源文件添加以下代码: ```c #include <tesseract/capi.h> ``` 4. 在你的代码,你需要初始化Tesseract引擎并设置语言数据集的路径。下面是一个简单的例子: ```c TessBaseAPI* tesseract = TessBaseAPICreate(); if (TessBaseAPIInit3(tesseract, NULL, "eng") != 0) { fprintf(stderr, "Could not initialize Tesseract.\n"); return 1; } TessBaseAPISetPageSegMode(tesseract, PSM_AUTO); TessBaseAPISetImageFile(tesseract, "path/to/your/image.png"); TessBaseAPIRecognize(tesseract, NULL); char* text = TessBaseAPIGetUTF8Text(tesseract); printf("Recognized text: %s\n", text); TessDeleteText(text); TessBaseAPIEnd(tesseract); ``` 在上面的示例,我们将图像文件的路径设置为`"path/to/your/image.png"`,并使用英语语言数据集("eng")进行初始化。你可以根据你的需要更改这些参数。 5. 编译并运行你的C程序。确保你已经正确链接了Tesseract库,并且能够访问语言数据集。如果一切正常,你应该能够看到识别出的文本输出。 这就是使用C语言配置Tesseract-OCR的基本步骤。希望对你有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值