【问题标题】:Converting Mat to PIX to setImage将 Mat 转换为 PIX 到 setImage
【发布时间】:2014-11-18 17:55:39
【问题描述】:

我正在尝试从裁剪的图像中识别文本,但我需要将其从 Mat 传递到 PIX,因为 X-Platform 编码。

我试过thisthisthis

通过MatPIX 使用相同的图像执行相同的函数,结果非常不同(PIX 可以完美运行,Mat 会变得混乱)。

我可能做错了什么?

谢谢。

PD:(这是我正在使用的代码 sn-ps 之一)

String imgToString(const char* variables, Mat gray) {
    char *outText;

    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    if (api->Init(NULL, "eng")) {
        String returnString = "Could not initialize tesseract.\n";
        fprintf(stderr, "Could not initialize tesseract.\n");
        return returnString;
    }
    api->SetVariable("tessedit_char_whitelist", variables);

    // Open input image with leptonica library
    api->TesseractRect(gray.data, 1, gray.channels() * gray.size().width, 0, 0, gray.cols, gray.rows);
    // Get OCR result
    outText = api->GetUTF8Text();
    return outText;
}

// The one below works fantastic

String imgToString(const char* variables, const char* filename) {
    char *outText;

    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    if (api->Init(NULL, "eng")) {
        String returnString = "Could not initialize tesseract.\n";
        fprintf(stderr, "Could not initialize tesseract.\n");
        return returnString;
    }
    api->SetVariable("tessedit_char_whitelist", variables);

    // Open input image with leptonica library
    Pix *image = pixRead(filename);
    api->SetImage(image);
    // Get OCR result
    outText = api->GetUTF8Text();
    return outText;
}

【问题讨论】:

    标签: c++ opencv tesseract leptonica


    【解决方案1】:

    问题似乎出在灰色图像中。正如 tesseract 的 pix.h 头文件所说,该库适用于每像素深度为 32 位的图像。 tesseract 还对颜色进行加权,因此应该对它们进行右对齐(opencv 默认将颜色存储为 BGR,但 tesseract 等待 RGBA)。简历:

    #include <tesseract/baseapi.h>
    #include <leptonica/allheaders.h>    
    #include <opencv2/opencv.hpp>
    ...
    char imagename[] = "testimg.jpg";
    cv::Mat _mat = cv::imread(imagename);
    cv::cvtColor(_mat, _mat, CV_BGR2RGBA); 
    api.SetImage(_mat.data, _mat.cols, _mat.rows, 4, 4*_mat.cols);
    char *outtext = api.GetUTF8Text();
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2015-01-05
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      相关资源
      最近更新 更多