使用 Tesseract 和 Opencv 程序进行 OCR 然后会遇到一些错误

Using Tesseract & Opencv Program for OCR then face some errors

当我 运行 exe 然后在 tesseract 和 opencv ocr 中遇到一些错误。 当我 运行 程序显示此错误时,我如何解决我的问题?有什么建议吗?

errors 是:

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.

版本是:

我的代码:

// 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

这里是 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;
}

我在 linux 上进行了测试,并使用以下代码对其进行了编译:

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

所以你需要自己调整visual-c++的编译参数。