C++调用Tesseract
2015-11-18 11:39
771 查看
Refer from http://blog.csdn.net/fengbingchun/article/details/8493877
1、 从http://code.google.com/p/tesseract-ocr/downloads/list下载tesseract-ocr-3.02-vs2008、tesseract-ocr-3.02.chi_sim.tar、tesseract-ocr-3.02.02.tar、tesseract-ocr-3.02.02-doc-html.tar、leptonica-1.68-win32-lib-include-dirs相关文件;
2、 将所有文件存放在E:\OCR\tesseract_ocr3.02文件夹下并解压缩;
3、 打开tesseract-ocr-3.02-vs2008文件夹下的tesseract.sln工程;
4、 将tesseract-ocr-3.02.02文件夹下的对应文件如api、ccmain等复制到E:\OCR\tesseract_ocr3.02\tesseract-ocr-3.02-vs2008\tesseract-ocr文件夹下,把leptonica-1.68-win32-lib-include-dirs文件夹下的include文件夹复制到E:\OCR\tesseract_ocr3.02\tesseract-ocr-3.02-vs2008文件夹下,将tesseract_ocr3.02\tesseract-ocr-3.02.02\tesseract-ocr\vs2008\port文件夹下的文件复制到E:\OCR\tesseract_ocr3.02\tesseract-ocr-3.02-vs2008\tesseract-ocr\vs2008\port文件夹下,将leptonica-1.68-win32-lib-include-dirs文件夹下lib文件夹复制到tesseract_ocr3.02\tesseract-ocr-3.02-vs2008文件夹下;
5、 将ccmain文件夹下的equationdetect.cpp文件中的static const STRING kCharsToEx[] = {"'", "`","\"", "\\", ",", ".",
"〈", "〉", "《", "》", "」", "「", ""};修改成static const STRING kCharsToEx[] = {"'", "`","\"", "\\", ",",".","<", ">", "<<",">>", ""};(注:不改动编译时始终出错,其它方法暂未发现,3.01版本中没有此文件,编译3.01不用对源文件作任何修改)
6、 重新编译整个Solution;
7、 创建一个控制台空工程,配置环境仿照tesseract工程,并在执行文件夹下创建一个tessdata文件夹,里面存放chi_sim.traineddata数据文件,示例代码如下:
[cpp]
view plaincopy
#include "allheaders.h"
#include "baseapi.h"
#include "basedir.h"
#include "strngs.h"
#include "tesseractmain.h"
#include "tprintf.h"
int main(int argc, char **argv)
{
tesseract::TessBaseAPI api;
STRING tessdata_dir;
truncate_path(argv[0], &tessdata_dir);
int rc = api.Init(tessdata_dir.string(), NULL);
if (rc)
{
fprintf(stderr, ("Could not initialize tesseract.\n"));
exit(1);
}
api.End();
// Make the order of args a bit more forgiving than it used to be.
const char* lang = "chi_sim";//eng
const char* image = "E:\\OCR\\tesseract_ocr3.02\\tesseract-ocr-3.02-vs2008\\tesseract-ocr\\vs2008\\Debug\\ABC.tif";//NULL;
const char* output = "E:\\OCR\\tesseract_ocr3.02\\tesseract-ocr-3.02-vs2008\\tesseract-ocr\\vs2008\\Debug\\xxxxx";//NULL;
tesseract::PageSegMode pagesegmode = tesseract::PSM_AUTO;
int arg = 1;
api.SetOutputName(output);
rc = api.Init(tessdata_dir.string(), lang, tesseract::OEM_DEFAULT,
&(argv[arg]), argc - arg, NULL, NULL, false);
if (rc)
{
fprintf(stderr, ("Could not initialize tesseract.\n"));
exit(1);
}
if (api.GetPageSegMode() == tesseract::PSM_SINGLE_BLOCK)
{
api.SetPageSegMode(pagesegmode);
}
tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n", tesseract::TessBaseAPI::Version());
FILE* fin = fopen(image, "rb");
if (fin == NULL)
{
fprintf(stderr, ("Cannot open input file: %s\n"), image);
exit(2);
}
fclose(fin);
PIX *pixs;
if ((pixs = pixRead(image)) == NULL)
{
fprintf(stderr, ("Unsupported image type.\n"));
exit(3);
}
pixDestroy(&pixs);
STRING text_out;
if (!api.ProcessPages(image, NULL, 0, &text_out))
{
fprintf(stderr, ("Error during processing.\n"));
}
bool output_hocr = false;
api.GetBoolVariable("tessedit_create_hocr", &output_hocr);
bool output_box = false;
api.GetBoolVariable("tessedit_create_boxfile", &output_box);
STRING outfile = output;
outfile += output_hocr ? ".html" : output_box ? ".box" : ".txt";
FILE* fout = fopen(outfile.string(), "wb");
if (fout == NULL)
{
fprintf(stderr, ("Cannot create output file %s\n"), outfile.string());
exit(1);
}
fwrite(text_out.string(), 1, text_out.length(), fout);
fclose(fout);
return 0; // Normal exit
}
以上代码编译运行成功,直接输入整幅图像进行字符识别,效果一般。
1、 从http://code.google.com/p/tesseract-ocr/downloads/list下载tesseract-ocr-3.02-vs2008、tesseract-ocr-3.02.chi_sim.tar、tesseract-ocr-3.02.02.tar、tesseract-ocr-3.02.02-doc-html.tar、leptonica-1.68-win32-lib-include-dirs相关文件;
2、 将所有文件存放在E:\OCR\tesseract_ocr3.02文件夹下并解压缩;
3、 打开tesseract-ocr-3.02-vs2008文件夹下的tesseract.sln工程;
4、 将tesseract-ocr-3.02.02文件夹下的对应文件如api、ccmain等复制到E:\OCR\tesseract_ocr3.02\tesseract-ocr-3.02-vs2008\tesseract-ocr文件夹下,把leptonica-1.68-win32-lib-include-dirs文件夹下的include文件夹复制到E:\OCR\tesseract_ocr3.02\tesseract-ocr-3.02-vs2008文件夹下,将tesseract_ocr3.02\tesseract-ocr-3.02.02\tesseract-ocr\vs2008\port文件夹下的文件复制到E:\OCR\tesseract_ocr3.02\tesseract-ocr-3.02-vs2008\tesseract-ocr\vs2008\port文件夹下,将leptonica-1.68-win32-lib-include-dirs文件夹下lib文件夹复制到tesseract_ocr3.02\tesseract-ocr-3.02-vs2008文件夹下;
5、 将ccmain文件夹下的equationdetect.cpp文件中的static const STRING kCharsToEx[] = {"'", "`","\"", "\\", ",", ".",
"〈", "〉", "《", "》", "」", "「", ""};修改成static const STRING kCharsToEx[] = {"'", "`","\"", "\\", ",",".","<", ">", "<<",">>", ""};(注:不改动编译时始终出错,其它方法暂未发现,3.01版本中没有此文件,编译3.01不用对源文件作任何修改)
6、 重新编译整个Solution;
7、 创建一个控制台空工程,配置环境仿照tesseract工程,并在执行文件夹下创建一个tessdata文件夹,里面存放chi_sim.traineddata数据文件,示例代码如下:
[cpp]
view plaincopy
#include "allheaders.h"
#include "baseapi.h"
#include "basedir.h"
#include "strngs.h"
#include "tesseractmain.h"
#include "tprintf.h"
int main(int argc, char **argv)
{
tesseract::TessBaseAPI api;
STRING tessdata_dir;
truncate_path(argv[0], &tessdata_dir);
int rc = api.Init(tessdata_dir.string(), NULL);
if (rc)
{
fprintf(stderr, ("Could not initialize tesseract.\n"));
exit(1);
}
api.End();
// Make the order of args a bit more forgiving than it used to be.
const char* lang = "chi_sim";//eng
const char* image = "E:\\OCR\\tesseract_ocr3.02\\tesseract-ocr-3.02-vs2008\\tesseract-ocr\\vs2008\\Debug\\ABC.tif";//NULL;
const char* output = "E:\\OCR\\tesseract_ocr3.02\\tesseract-ocr-3.02-vs2008\\tesseract-ocr\\vs2008\\Debug\\xxxxx";//NULL;
tesseract::PageSegMode pagesegmode = tesseract::PSM_AUTO;
int arg = 1;
api.SetOutputName(output);
rc = api.Init(tessdata_dir.string(), lang, tesseract::OEM_DEFAULT,
&(argv[arg]), argc - arg, NULL, NULL, false);
if (rc)
{
fprintf(stderr, ("Could not initialize tesseract.\n"));
exit(1);
}
if (api.GetPageSegMode() == tesseract::PSM_SINGLE_BLOCK)
{
api.SetPageSegMode(pagesegmode);
}
tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n", tesseract::TessBaseAPI::Version());
FILE* fin = fopen(image, "rb");
if (fin == NULL)
{
fprintf(stderr, ("Cannot open input file: %s\n"), image);
exit(2);
}
fclose(fin);
PIX *pixs;
if ((pixs = pixRead(image)) == NULL)
{
fprintf(stderr, ("Unsupported image type.\n"));
exit(3);
}
pixDestroy(&pixs);
STRING text_out;
if (!api.ProcessPages(image, NULL, 0, &text_out))
{
fprintf(stderr, ("Error during processing.\n"));
}
bool output_hocr = false;
api.GetBoolVariable("tessedit_create_hocr", &output_hocr);
bool output_box = false;
api.GetBoolVariable("tessedit_create_boxfile", &output_box);
STRING outfile = output;
outfile += output_hocr ? ".html" : output_box ? ".box" : ".txt";
FILE* fout = fopen(outfile.string(), "wb");
if (fout == NULL)
{
fprintf(stderr, ("Cannot create output file %s\n"), outfile.string());
exit(1);
}
fwrite(text_out.string(), 1, text_out.length(), fout);
fclose(fout);
return 0; // Normal exit
}
以上代码编译运行成功,直接输入整幅图像进行字符识别,效果一般。
相关文章推荐
- 剑指offer第三十二题【把数组排成最小的数】c++实现
- C++ 对象的内存布局
- 约瑟夫环-顺序表-C++
- 腾讯c++ 阿里java
- 小顶堆第二弹-----堆降序排序(C语言非递归)
- C++编程规范---读书有感(2)
- C语言——符号的声明与定义
- C++类中的枚举类型
- C语言---迷宫游戏 代码
- C++中的单例模式其实也不简单
- C++文件 读写操作大全
- 剑指offer第三十一题【整数中1出现的次数(从1到n整数中1出现的次数)】c++实现
- C语言之动态内存分配
- C++的虚函数表
- C++输入输出操作符重载
- C++输入输出操作符重载
- 【c++】公司职员系统
- 【C++】多态的实现原理
- 单片机C语言代码-代码格式
- C语言之指针高级