OCR C++ Tesseract按行识别字符

这个程序演示Tesseract OCR按行识别字符。

不仅显示识别结果,还找出每行文本的bounding box(边界框/包围盒)。

输入图片:

程序运行后会在CMD里打印识别结果:

解释一下:在图片里找到了8行文本。

矩形边界框0的左上角坐标是(31,87),宽度是554,高度是40,置信度是94.

把矩形框绘制到输入图片里,大概就是这样:

代码:

cpp 复制代码
#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);
  }
  Pix *image = pixRead("phototest.tif");
  api->SetImage(image);
  Boxa* boxes = api->GetComponentImages(tesseract::RIL_TEXTLINE, true, NULL, NULL);
  printf("Found %d textline image components.\n", boxes->n);
  for (int i = 0; i < boxes->n; i++) {
    BOX* box = boxaGetBox(boxes, i, L_CLONE);
    api->SetRectangle(box->x, box->y, box->w, box->h);
    char* ocrResult = api->GetUTF8Text();
    int conf = api->MeanTextConf();
    fprintf(stdout, "Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s",
                    i, box->x, box->y, box->w, box->h, conf, ocrResult);
    boxDestroy(&box);
  }
  // Destroy used object and release memory
  api->End();
  delete api;
  delete [] outText;
  pixDestroy(&image);

  return 0;
}
相关推荐
hi_ro_a11 分钟前
基于正倒排索引的boost搜索引擎
linux·c++·搜索引擎·项目
shark-chili14 分钟前
关于AI辅助编程的认知
数据库·人工智能·redis·macos·缓存
机核研创社15 分钟前
一次性内裤无人生产线详解:从裁片到成品 12–35 秒一件的整线自动化架构
人工智能·自动化
有Li17 分钟前
【AI答疑】MR数据预处理步骤
人工智能·笔记·算法·语言模型·医学生
神仙别闹22 分钟前
基于 C++ MFC 实现的个人通讯录管理系统
c++
衡石科技24 分钟前
让 BI 能力可编排:CLI、Headless API 与可治理执行
人工智能·chatbi
蓝速科技25 分钟前
国产化信创终端等保合规核心防护与落地方案丨蓝速科技
大数据·运维·数据库·人工智能·科技
GG-_-Bond28 分钟前
9.1kv存储持久化模拟面试
linux·c语言·数据结构·c++
GEO实战经验分享29 分钟前
王涛:GEO 服务商能力评估清单——基础、结构、战略、风控四层怎么核验
大数据·人工智能·chatgpt
干就完事了30 分钟前
机器学习-音乐艺人流行趋势预测
人工智能·机器学习·阿里云