OpenMinded OpenMinded - 1 year ago 237
C++ Question

How to implement Tesseract to run with project in Visual Studio 2010

I have a C++ project in Visual Studio 2010 and wish to use OCR. I came across many "tutorials" for Tesseract but sadly, all I got was a headache and wasted time.

In my project I have an image stored as a Mat. One solution to my problem is to save this Mat as an image (image.jpg for example) and then call Tesseract executable file like this:

system("tesseract.exe image.jpg out");

Which gets me an output out.txt and then I call ("out.txt");

to read the output from Tesseract.

It is all good and works like a chair but it is not an optimal solution. In my project I am processing a video so save/call .exe/write/read at 10+ FPS is not what I am really looking for. I want to implement Tesseract to existing code so to be able to pass a Mat as an argument and immediately get a result as a String.

Do you know any good tutorial(pref. step-by-step) to implement Tesseract OCR with Visual Studio 2010? Or your own solution?

Answer Source

OK, I figured it out but it works for Release and Win32 configuration only (No debug or x64). There are many linking errors under Debug configuration.


1. First of all, download prepared library folder(Tesseract + Leptonica) here:

Mirror 1(Google Drive)

Mirror 2(MediaFire)

2. Extract to C:\

3. In Visual Studio, go under C/C++ > General > Additional Include Directories

Insert C:\tesseract\include

4. Under Linker > General > Additional Library Directories

Insert C:\tesseract\lib

5. Under Linker > Input > Additional Dependencies



Sample code should look like this:

#include <tesseract\baseapi.h>
#include <leptonica\allheaders.h>
#include <iostream>

using namespace std;

int main(void){

    tesseract::TessBaseAPI api;
    api.Init("", "eng", tesseract::OEM_DEFAULT);

    cout<<"File name:";
    char image[256];
    PIX   *pixs = pixRead(image);

    STRING text_out;
    api.ProcessPages(image, NULL, 0, &text_out);



For interaction with OpenCV and Mat type images look HERE

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download