OCR C++ Tesseract设置矩形识别区域
2026/7/15 21:51:44 网站建设 项目流程

这个程序演示在Tesseract OCR里指定一个矩形区域进行光学字符识别。

先看一下输入图片:

代码里第20行设置了识别区域的左上角坐标为(30,86),宽度是590,高度是100.

绘制到输入图片上就是下面红色标注的区域,也就是第一段。

程序运行后会识别红色框里的文字,也就是第一段:

代码:

#include <tesseract/baseapi.h> #include <leptonica/allheaders.h> int main() { char *outText; tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); // Initialize tesseract-ocr with English, without specifying tessdata path if (api->Init(NULL, "eng")) { fprintf(stderr, "Could not initialize tesseract.\n"); exit(1); } // Open input image with leptonica library Pix *image = pixRead("phototest.tif"); api->SetImage(image); // Restrict recognition to a sub-rectangle of the image // SetRectangle(left, top, width, height) api->SetRectangle(30, 86, 590, 100); // Get OCR result outText = api->GetUTF8Text(); printf("OCR output:\n%s", outText); // Destroy used object and release memory api->End(); delete api; delete [] outText; pixDestroy(&image); return 0; }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询