问题描述
我试图从剪裁的图像中识别文本,但我需要从 Mat
到 PIX
因为X平台编码。
I'm trying to recognize text from a cropped image but I need to pass it from Mat
to PIX
because X-Platform coding.
我试过,和
并执行相同的函数传递 Mat
和 PIX
使用相同的图像,结果非常不同( PIX
它工作得很好,
Mat
它搞乱了)。
And doing the same function passing Mat
and PIX
with the same image, results are very very different (with PIX
it works perfectly, with Mat
it gets messed).
?
感谢。
PD :(这是我使用的代码片段之一) / p>
PD: (This is one of the code snippets that I'm using)
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头部表示库工作在32位每像素深度的图像。此外,tesseract重量颜色,所以右对齐它们应该做(opencv默认存储颜色为BGR,但tesseract等待RGBA)。简历:
Problem is seems to be in a gray image. As tesseract's pix.h header says library works on images with 32-bit per pixel depth. Also tesseract weighs colors, so right alignment of them should be done (opencv on default stores colors as BGR, but tesseract wait RGBA). Resume:
#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);
cahr *outtext = api.GetUTF8Text();
...
这篇关于将Mat转换为PIX以设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!