原文来自:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461f1d32a2e57d76511980853a3c50f11e41bca770216c5d61aa9ec88240dcb1993c6ad567627f42d103548e45f49a15&p=882a9546dc8b0ef50be296391b0cc9&newp=9865c70d85cc43e908e2947d074497231610db2151d2d61539&user=baidu&fm=sc&query=Leptonica&qid=&p1=2

最近一直跟OCR打交道,学习了下google的OCR引擎TESSERACT,是个很好的识别工具。tesseract-3.0已支持版面分析,功能很强大。安装tesseract前可选择性地安装leptonica和libtiff。不过建议先安装这两个库。不安装tiff的话只能处理bmp文件。

这里只是说明怎么识别中文。依次安装好libtiff,leptonica和tesseract后,下载简体中文和繁体中文的训练数据,在tesseract的下载页可以找到。放到某个目录的tessdata文件夹下。然后设置环境变量TESSDATA_PREFIX=tessdata的目录。然后,新建一个ocr.cpp文件,编写如下代码:

#include <mfcpch.h>

#include <ctype.h>

#include <sys/time.h>

#include "applybox.h"

#include "control.h"

#include "tessvars.h"

#include "tessedit.h"

#include "baseapi.h"

#include "thresholder.h"

#include "pageres.h"

#include "imgs.h"

#include "varabled.h"

#include "tprintf.h"

#include "stderr.h"

#include "notdll.h"

#include "mainblk.h"

#include "output.h"

#include "globals.h"

#include "helpers.h"

#include "blread.h"

#include "tfacep.h"

#include "callnet.h"

#include "allheaders.h"

int main(int argc,char **argv){

if(argc!=3){

printf("usage:%s <bmp file> <txt file>/n",argv[0]);

return -1;

}

char *image_file=argv[1];

char *txt_file=argv[2];

STRING text_out;

struct timeval beg,end;

tesseract::TessBaseAPI  api;

IMAGE image;

api.Init(argv[0], "chi_sim", NULL, 0, false);//初始化api对象

api.SetPageSegMode(tesseract::PSM_AUTO);//设置自动进行版面分析

api.SetAccuracyVSpeed(tesseract::AVS_FASTEST);//要求速度最快

if (image.read_header(image_file) < 0) {//读取bmp文件的元信息

printf("Read of file %s failed./n", image_file);

exit(1);

}

if (image.read(image.get_ysize ()) < 0){//读取bmp文件

printf("Read of image %s error/n", image_file);

exit(1);

}

invert_image(&image);//反转图像的每个像素,即使1变0,0变1

int bytes_per_line = check_legal_image_size(image.get_xsize(),

image.get_ysize(),

image.get_bpp());//计算每一行像素所占字节数

api.SetImage(image.get_buffer(), image.get_xsize(), image.get_ysize(),

image.get_bpp() / 8, bytes_per_line);//设置图像

gettimeofday(&beg,NULL);

char* text = api.GetUTF8Text();//识别图像中的文字

gettimeofday(&end,NULL);

printf("%s:reconize sec=%f/n",argv[0],end.tv_sec-beg.tv_sec+(double)(end.tv_usec-beg.tv_usec)/1000000.0);//打印识别的时间

text_out += text;

delete [] text;

FILE* fout = fopen(txt_file, "w");

fwrite(text_out.string(), 1, text_out.length(), fout);//将识别结果写入输出文件

fclose(fout);

}

再编写一个makefile文件如下:

all:ocr

CFLAGS=-Wall -g

LDFLAGS= -lz  -lm  -ltesseract_textord /

-ltesseract_wordrec -ltesseract_classify -ltesseract_dict -ltesseract_ccstruct/

-ltesseract_ccstruct -ltesseract_cutil -ltesseract_viewer -ltesseract_ccutil/

-ltesseract_api -ltesseract_image -ltesseract_main -llept

LD_LIBRARY_PATH =

INCLUDES= -I/usr/local/include/tesseract/ -I/usr/local/include/leptonica/

%.o:%.cpp

g++ -c $(CFLAGS) $(INCLUDES)  $(SOURCE) -o $@ $<

ocr:ocr.o

g++ -o $@ $^ -g $(LD_LIBRARY_PATH) $(LDFLAGS)

clean:

rm ocr.o

在该目录下运行make编译成可执行文件ocr,运行./ocr 1.bmp 1.txt就可以将图像1.bmp识别结果写到1.txt了,程序会打印识别的时间。值得注意的是,tesseract中文识别速度很慢,运行几分钟很正常。不知有哪位大虾知道怎么调优?

更郁闷的是tesseract不支持多线程,不能在同一进程中运行多个实例。。

最新文章

  1. 用Python套接字创建HTTP客户与服务器程序
  2. 三、jQuery--jQuery基础--jQuery基础课程--第8章 jQuery 实现Ajax应用
  3. C#借助谷歌翻译实现翻译小工具(一)基本功能实现
  4. Java 中equals和toString()方法重写
  5. bzoj 2956 数学展开,分段处理
  6. 通过gdb跟踪Linux内核装载和启动可执行程序过程
  7. 高德地图 JavaScript API 开发系列教程(二)
  8. AOP 在javascript 中的使用
  9. Linux后台运行命令 nohup command &gt; myout.file 2&gt;&amp;1
  10. java中hashCode()与equals()详解
  11. [BZOJ 3110] [ZJOI 2013] K大数查询
  12. java.lang.ClassNotFoundException: com.sun.xml.ws.spi.ProviderImpl解决办法
  13. 我的FP感悟
  14. HTML5 实现获取 gzip 压缩数据,并进行解压,同时解决汉字乱码,相关 pako.js
  15. PSP(3.30——4.5)以及周记录
  16. C++指针二(易错模型)
  17. ubuntu系统下怎么安装gcc编译器
  18. content_form.class.php文件不完整 解决方案
  19. Json格式String类型字符串转为Map工具类
  20. 【BZOJ2437】【NOI2011】兔兔与蛋蛋(博弈论,二分图匹配)

热门文章

  1. Struts 和Spring的核心控制器
  2. UI基础:UILabel.UIFont
  3. linux线程之pthread_join
  4. jQuery 插件 flexslider 初步使用
  5. 转-——推荐几个web中常用的一些js图表插件 - zccst
  6. 八皇后问题-回溯法(MATLAB)
  7. 无法在web服务器上启动调试,此项目在使用一个被配置为使用特定IP地址的网站。请在项目URL中指定计算机名称。
  8. 0610 python 基础03
  9. Linux 系统之Systemd
  10. 【转】AC算法详解