【发布时间】:2011-11-13 22:44:43
【问题描述】:
我正在使用 OpenCV 提取扫描文档的子图像,并希望使用 tesseract 对此子图像执行 OCR。
我发现我可以在 tesseract 中使用两种方法进行文本识别,但到目前为止我还没有找到可行的解决方案。
A.) 如何将 cv::Mat 转换为 PIX*?
(PIX* 是leptonica 的数据类型)
基于下面的 vasiles 代码,这基本上是我当前的代码:
cv::Mat image = cv::imread("c:/image.png");
cv::Mat subImage = image(cv::Rect(50, 200, 300, 100));
int depth;
if(subImage.depth() == CV_8U)
depth = 8;
//other cases not considered yet
PIX* pix = pixCreateHeader(subImage.size().width, subImage.size().height, depth);
pix->data = (l_uint32*) subImage.data;
tesseract::TessBaseAPI tess;
STRING text;
if(tess.ProcessPage(pix, 0, 0, &text))
{
std::cout << text.string();
}
虽然它没有崩溃或发生任何事情,但 OCR 结果仍然是错误的。它应该可以识别我的示例图像中的一个单词,但它会返回一些不可读的字符。
PIX_HEADER这个方法不存在,所以我用pixCreateHeader,但是它没有把通道数作为参数。那么如何设置频道数呢?
B.) 我如何将cv::Mat 用于TesseractRect()?
Tesseract 提供了另一种使用此签名的文本识别方法:
char * TessBaseAPI::TesseractRect (
const UINT8 * imagedata,
int bytes_per_pixel,
int bytes_per_line,
int left,
int top,
int width,
int height
)
目前我正在使用以下代码,但它也会返回不可读的字符(尽管与上面的代码不同。
char* cr = tess.TesseractRect(
subImage.data,
subImage.channels(),
subImage.channels() * subImage.size().width,
0,
0,
subImage.size().width,
subImage.size().height);
【问题讨论】: