我正在尝试使用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->run(rgb(rect), output_string);
之类的代码即可。 - MikiPtr<OCRTesseract> tess = OCRTesseract::create(...); tess->run(...)
- Miki