Search code examples
opencvvisual-c++ocrtesseract

Using Tesseract & Opencv Program for OCR then face some errors


When I run the exe then face some errors in tesseract & opencv ocr. How I solve my problem when I run the program then show this errors any suggestion about that?

The errors are:

D:\OCR\Install-0penCV-with-Tesseract-Windows\OCVTessExample\x64\Debug>OCVTessExample.exe
4.1.0
Error in pixCreateHeader: width must be > 9
Error in pixCreateNoInit: pixd not made
Error in pixCreate: pixd not made
Error in pixGetData: pix not defined
Error in pixGetl: pix not defined
Error in pixGetDimensions: pix not defined
Error in pixGetColormap: pix not defined
Error in pixCopy: pixs not defined
Error in pixGetDepth: pix not defined
Error in pixGetWpl: pix not defined
Error in pixGetYRes: pix not defined
Error in pixClone: pixs not defined
Please call SetImage before attempting recognition.

The version is:

The version of tesseract

My code:

// Create Tesseract object
tesseract::TessBaseAPI *ocr = new tesseract::TessBaseAPI();

cout << ocr->Version() << endl;

// Initialize tesseract to use English (eng) and the LSTM OCR engine. 
ocr->Init(NULL, "eng", tesseract::OEM_LSTM_ONLY);
//ocr->Init("tessdata", "eng", tesseract::OEM_LSTM_ONLY);

// Set Page segmentation mode to PSM_AUTO (3)
ocr->SetPageSegMode(tesseract::PSM_AUTO);
// Open input image using OpenCV
Mat im = cv::imread("img.png", IMREAD_COLOR);

//cv::imwrite("img.png", im);
//im = cv::imread("img.png");

// Set image data
ocr->SetImage(im.data, 2024, 1080, 3, im.step);

// Run Tesseract OCR on image
outText = string(ocr->GetUTF8Text());

// print recognized text
cout << outText << endl;

return EXIT_SUCCESS

Solution

  • Here is simple minimum example of tesseract&opencv (test-opencv.cpp):

    #include <tesseract/baseapi.h>
    #include <leptonica/allheaders.h>
    #include <opencv2/opencv.hpp>
    
    int main() {
        char *outText;
    
        tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
        if (api->Init(".", "eng")) {
            fprintf(stderr, "Could not initialize tesseract.\n");
            exit(1);
        }
    
        cv::Mat im = cv::imread("img.png", cv::IMREAD_COLOR);
        api->SetImage(im.data, im.cols, im.rows, 3, im.step);
        outText = api->GetUTF8Text();
        printf("OCR output:\n%s", outText);
        api->End();
        delete [] outText;
        return 0;
    }
    

    I tested in on linux and compiled it with:

    g++ -O3 -std=c++11 test-opencv.cpp `pkg-config --cflags --libs lept tesseract opencv` -o test-opencv
    

    So you need to adjust visual-c++ compilation argument by yourself.