【发布时间】:2016-07-28 16:15:16
【问题描述】:
我正在尝试使用 OpenCV 和 Tesseract 从图像中提取文本。我已经设法检测到文本区域并使用边界框来分隔它们。但现在我找不到如何将边界框传递给 Tesseract。
for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
{
Rect rect = boundingRect(contours[idx]);
Mat maskROI(mask, rect);
maskROI = Scalar(0, 0, 0);
// fill the contour
drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
// ratio of non-zero pixels in the filled region
double r = (double)countNonZero(maskROI)/(rect.width*rect.height);
if (r > .45 /* assume at least 45% of the area is filled if it contains text */
&&
(rect.height > 8 && rect.width > 8) /* constraints on region size */
/* these two conditions alone are not very robust. better to use something
like the number of significant peaks in a horizontal projection as a third condition */
)
{
rectangle(rgb, rect, Scalar(0, 255, 0), 2);
}
}
imwrite(OUTPUT_FOLDER_PATH + string("/rgb.jpg"), rgb);
return 0;
}
我在使用边界框时获得了非常好的结果。带边界框的图像:
然后尝试cv::text::OCRTesseract::run 但这似乎不起作用。
有人有想法吗?
编辑:我不得不删除大部分代码,因为我正在实习的公司要求我这样做。但这是我的年终项目,所以一旦我结束了这一年,我将使用整个项目的 github 链接编辑帖子。
【问题讨论】:
-
你不能通过裁剪的图像吗?
-
此代码中没有裁剪图像。它只检测文本区域,然后对它们进行轮廓/
-
是的,我明白了...你不能在每个矩形上裁剪图像,然后将每个裁剪传递给 tesseract 吗?
-
这正是我想要做的。我还没有找到任何文档或示例来帮助我做到这一点。大多数文档都说您可以将边界框作为参数传递给 openCv 中的 Tesseract,但我不知道该怎么做
-
您可以从 OpenCV tesseract 中检索矩形,而不是将它们用作输入。当你有一个很好的检测时,只需调用
tesseract->run(rgb(rect), output_string);.. 或类似的东西
标签: c++ opencv tesseract opencv3.0