use*_*634 3 c++ opencv tesseract opencv3.0
我正在尝试使用 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;
}
Run Code Online (Sandbox Code Playgroud)
我在边界框上得到了很好的结果。带有边界框的图像:
然后尝试过,cv::text::OCRTesseract::run但这似乎不起作用。
有人有想法吗?
编辑:我不得不删除大部分代码,因为我实习的公司要求我这样做。但这是我的年终项目,所以一结束我就会用整个项目的 github 链接编辑帖子。
首先感谢 miki 的帮助。这就是我为解决此问题所做的工作。
为每个边界框裁剪原始图像。这将为图像中的许多文本区域提供单独的图像。为此,只需将其Mat cropedImage = small(Rect(rect));放在此行下rectangle(rgb, rect, Scalar(0, 255, 0), 2);
创建 OCRTesseract 类的实例并初始化 tesseract 引擎。为此,请添加此行Ptr<cv::text::OCRTesseract> tess = cv::text::OCRTesseract::create(NULL,NULL,NULL,3,3);(最好在 main 之前,但您可以将它放在任何位置,只要它在此代码中的 for 循环之前)。该参数不是强制性的,因此您可以将Ptr<cv::text::OCRTesseract> tess = cv::text::OCRTesseract::create();.
tess->run(cropedImage, output_string);下面添加这一行Mat cropedImage = small(Rect(rect));请注意,最好在将裁剪后的图像传递给 OCR 之前对其进行处理(阈值化为二值图像,放大裁剪使文本不接触边缘)
| 归档时间: |
|
| 查看次数: |
6202 次 |
| 最近记录: |