Tesseract(tesseract-ocr)在Visual Studio 2013中的使用

开篇提示:本文为本人原创,本文欢迎转载,但必须注明本文出处,例如。
“该文引用自 CruiseYoung的:Tesseract(tesseract-ocr)在Visual Studio 2013中的使用
http://blog.csdn.net/fksec/article/details/37598479”
否则说明阁下愿意支付以100元人民币每字计的稿费,敬请留意。

0 环境准备

Tesseract(tesseract-ocr)的编译见《Visual Studio 2013、TortoiseSVN、TortoiseGit、msysgit编译Tesseract(tesseract-ocr)》:http://blog.csdn.net/fksec/article/details/32714639
以下操作,以Visual Studio 2013创建“test_tess”工程,并添加默认解决方案“test_tess”;一切采用默认设置。

1 头文件引入

在“解决方案管理器”窗口-->右键“test_tess”工程-->“属性(R)”-->“配置属性”-->“C/C++”-->“常规”-->“附加包含目录”: 在“test_tess”工程中所有“活动解决方案平台”的所有“活动解决方案配置”中都添加如下内容:
D:\comm\tesseract\include;D:\comm\tesseract\include\leptonica;D:\comm\tesseract\include\tesseract;
在用到“OpenCL”库时,还需添加以下内容:
$(AMDAPPSDKROOT)\include;$(INTELOCLSDKROOT)\include;$(NVSDKCUDA_ROOT)\include;

2 库文件引入

2.1 参考文档
本地文档:“tesseract-ocr\vs2008\doc\programming.html”
网络文档:Programming with libtesseract — Visual Studio 2008 Developer Notes for Tesseract-OCR
http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/programming.html
2.2 添加属性表
将“D:\comm\tesseract\include\tesseract\vs2013”目录或“tesseract-build\tesseract-ocr\vs2013”目录下的“include”目录拷贝到本地工程“test_tess”所在目录。即“test_tess\test_tess”目录下;
并通过以下设置:
在“解决方案管理器”所在区域,切换到“属性管理器”窗口;如果没有“属性管理器”窗口,请通过如下方式打开:点击“菜单栏”“视图(V)”-->“其他窗口(E)”-->“属性管理器(M)”;
右键“test_tess”工程-->“添加现有属性表(E)...”-->在“test_tess\test_tess\include”目录下找到“tesseract_versionnumbers.props”-->“打开”;
2.3 配置库文件
以下提供“tesseract”库编译出的5种“活动解决方案配置”的库引入情况,即“DLL_Debug”,“DLL_Release”,“LIB_Debug”,“LIB_Release”,“LIB_Opencl_Release”。
补充1:工程默认会生成“Debug”、“Release”2种“活动解决方案配置”,它与上述5种“活动解决方案配置”没有必然联系。
补充2:“活动解决方案配置”的“新建”与“修改”:在“解决方案管理器”窗口-->右键“test_tess”工程-->“属性(R)”-->“配置管理器(O)...”-->“项目上下文(选定要生成的或部署的项目配置)(R):”-->“项目”:“test_tess”-->“配置”-->下拉“新建”或“编辑”;
在“解决方案管理器”窗口-->右键“test_tess”工程-->“属性(R)”-->“配置属性”-->“连接器”-->“常规”-->“附加库目录”: 在“test_tess”工程中所有“活动解决方案平台”的所有“活动解决方案配置”中都添加如下内容:
D:\comm\tesseract\lib\$(Platform);
在“解决方案管理器”窗口-->右键“test_tess”工程-->“属性(R)”-->“配置属性”-->“C/C++”-->“预处理器”-->“预处理器定义”-->按“活动解决方案配置”类型“添加”如下列表中的“预定义”内容;
在“解决方案管理器”窗口-->右键“test_tess”工程-->“属性(R)”-->“配置属性”-->“连接器”-->“输入”-->“附加依赖项”-->按“活动解决方案配置”类型“添加”如下列表中的“附加库”内容;
“DLL_Debug”:
预定义:
WIN32;_DEBUG;_WINDOWS;USE_STD_NAMESPACE;TESSDLL_IMPORTS;CCUTIL_IMPORTS;LIBLEPT_IMPORTS;
附加库:
ws2_32.lib;user32.lib;liblept$(LIBLEPT_VERSION)d.lib;libtesseract$(LIBTESS_VERSION)d.lib;
“DLL_Release”
预定义:
WIN32;NDEBUG;_WINDOWS;USE_STD_NAMESPACE;TESSDLL_IMPORTS;CCUTIL_IMPORTS;LIBLEPT_IMPORTS;
附加库:
ws2_32.lib;user32.lib;liblept$(LIBLEPT_VERSION).lib;libtesseract$(LIBTESS_VERSION).lib;
“LIB_Debug”
预定义:
WIN32;_DEBUG;_WINDOWS;USE_STD_NAMESPACE;
附加库:
ws2_32.lib;user32.lib;zlib$(ZLIB_VERSION)-static-mtdll-debug.lib;libpng$(LIBPNG_VERSION)-static-mtdll-debug.lib;libjpeg$(LIBJPEG_VERSION)-static-mtdll-debug.lib;giflib$(GIFLIB_VERSION)-static-mtdll-debug.lib;libtiff$(LIBTIFF_VERSION)-static-mtdll-debug.lib;liblept$(LIBLEPT_VERSION)-static-mtdll-debug.lib;libtesseract$(LIBTESS_VERSION)-static-debug.lib;
“LIB_Release”
预定义:
WIN32;NDEBUG;_WINDOWS;USE_STD_NAMESPACE;
附加库:
ws2_32.lib;user32.lib;zlib$(ZLIB_VERSION)-static-mtdll.lib;libpng$(LIBPNG_VERSION)-static-mtdll.lib;libjpeg$(LIBJPEG_VERSION)-static-mtdll.lib;giflib$(GIFLIB_VERSION)-static-mtdll.lib;libtiff$(LIBTIFF_VERSION)-static-mtdll.lib;liblept$(LIBLEPT_VERSION)-static-mtdll.lib;libtesseract$(LIBTESS_VERSION)-static.lib;
“LIB_Opencl_Release”
预定义:
WIN32;NDEBUG;_WINDOWS;USE_STD_NAMESPACE;USE_OPENCL;
附加库:
ws2_32.lib;user32.lib;zlib$(ZLIB_VERSION)-static-mtdll.lib;libpng$(LIBPNG_VERSION)-static-mtdll.lib;libjpeg$(LIBJPEG_VERSION)-static-mtdll.lib;giflib$(GIFLIB_VERSION)-static-mtdll.lib;libtiff$(LIBTIFF_VERSION)-static-mtdll.lib;liblept$(LIBLEPT_VERSION)-static-mtdll.lib;OpenCL.lib;libtesseract$(LIBTESS_VERSION)-opencl-static.lib;

3 测试代码

参考文档:
本地文档:“tesseract-ocr\api\tesseractmain.cpp”以及“tesseract-ocr-API-Example-vs2008\APIExample\baseapitester\baseapitester.cpp”文件;
网络文档:APIExample - tesseract-ocr - API examples - An OCR Engine that was developed at HP Labs between 1985 and 1995... and now at Google. - Google Project Hosting:http://code.google.com/p/tesseract-ocr/wiki/APIExample
测试代码1:
#include <leptonica\allheaders.h>
#include <tesseract\baseapi.h>
#include <tesseract\renderer.h>

int main(int argc, char* argv[])
{
    const char * image = "image.jpg";
    const char * outputbase = "out";
    
    tesseract::TessBaseAPI api;
    api.SetOutputName(outputbase);

    int rc = api.Init(NULL, "eng", tesseract::OEM_DEFAULT);
    if (rc) {
        printf("Could not initialize tesseract.\n");
        exit(1);
    }

    //api.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

    Pix* pixs = pixRead(image);
    if (!pixs) {
        printf("Cannot open input file: %s\n", image);
        exit(2);
    }
    api.SetImage(pixs);

    tesseract::TessResultRenderer* renderer = new tesseract::TessTextRenderer(outputbase);
    if (!api.ProcessPages(image, NULL, 0, renderer)) {
        printf("Error during processing.\n");
    }

    delete renderer;
    pixDestroy(&pixs);
    return 0;
}
测试代码2:
#include <stdio.h>
#include <allheaders.h>
#include <capi.h>

void die(const char *errstr) {
        fputs(errstr, stderr);
        exit(1);
}

int main(int argc, char *argv[]) {
        TessBaseAPI *handle;
        PIX *img;
        char *text;

        if((img = pixRead("img.png")) == NULL)
                die("Error reading image\n");

        handle = TessBaseAPICreate();
        if(TessBaseAPIInit3(handle, NULL, "eng") != 0)
                die("Error initialising tesseract\n");

        TessBaseAPISetImage2(handle, img);
        if(TessBaseAPIRecognize(handle, NULL) != 0)
                die("Error in Tesseract recognition\n");

        if((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
                die("Error getting text\n");

        fputs(text, stdout);

        TessDeleteText(text);
        TessBaseAPIEnd(handle);
        TessBaseAPIDelete(handle);
        pixDestroy(&img);

        return 0;
}
在测试时别忘了拷贝“语言库tessdata”目录到执行文件夹
网络例子:dsoprea/CTesseract:
https://github.com/dsoprea/CTesseract

4 参考文档

Visual Studio 2013、TortoiseSVN、TortoiseGit、msysgit编译Tesseract(tesseract-ocr)
http://blog.csdn.net/fksec/article/details/32714639
Tesseract3.02训练生成新的识别语言库的详细步骤 - 科研的起点
http://blog.csdn.net/tuling_research/article/details/41091163
Tesseract3.01 OCR在VS2008环境下的编译使用(1)
http://www.cnblogs.com/zsb517/archive/2012/06/03/2532488.html
OCR之Tesseract在VS2010中的使用
http://blog.csdn.net/yangbingzhou/article/details/20230533
Tesseract3的常用API
http://blog.csdn.net/yangbingzhou/article/details/20231667
tesseract开发,tesseract资源下载
http://www.csdn.net/tag/tesseract
Tesseract: TessBaseAPI Class Reference
http://tesseract-ocr.repairfaq.org/classTessBaseAPI.html
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值