我使用Tesseract OCR和OpenCV从Mat图像中获取数字,特别是零,但经常发生Tesseract没有以正确的方式识别零。我认为这是因为边界矩形太小而削减了数字的一部分,所以这是一个' 0'成了一个' c'或者是' n。 有办法扩大边界矩形吗?这是我的代码:
int main()
{
Mat img1 = imread("Input/05.png");
cvtColor(img1, img1, CV_BGR2GRAY);
Size size(img1.cols*1.5,img1.rows*1.5);
Mat img;
resize(img1, img, size);
//pre-processing start
IplImage* image = new IplImage(img);
IplImage* bWImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
cvThreshold(image, bWImage, 180, 255, THRESH_BINARY);
Mat patch = cvarrToMat(bWImage, true);
//pre-processing end
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
api->Init(NULL, "eng");
api->SetImage((uchar*)patch.data, patch.cols, patch.rows, 1, patch.cols);
//api->SetVariable("classify_bln_numeric_mode", "1");
//api->SetRectangle(30, 400, 400, 40);
api->Recognize(NULL);
tesseract::ResultIterator* ri = api->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
vector<Point> points;
if (ri != 0){
do {
const char* word = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
int x1, y1, x2, y2;
if(word != 0){
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
rectangle(patch, Point(x1,y1), Point(x2,y2), 0, 1, 8);
imshow("Zero", patch);
waitKey();
printf("word: '%s'; \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n", word, conf, x1, y1, x2, y2);
}
delete[] word;
} while (ri->Next(level));
}
}