我正在尝试从裁剪图像中识别文本,但我需要将它从 Mat
传递到 PIX
,因为 X-Platform 编码。
并通过 Mat
和 PIX
使用相同的图像执行相同的功能,结果非常不同(使用 PIX
效果很好,使用 Mat
它会变得一团糟)。
我可能做错了什么?
谢谢。
PD:(这是我正在使用的代码片段之一)
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;
}
最佳答案
问题似乎出在灰度图像中。正如 tesseract 的 pix.h header 所说,库适用于每像素深度为 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();
...
关于c++ - 将 Mat 转换为 PIX 以设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000797/