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;
}
相关推荐
IvanCodes6 小时前
RAG 实战教程(一):RAG 工作原理与完整流程——分片、索引、召回、重排和生成
人工智能·后端·agent
·薯条大王7 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
今天AI了吗7 小时前
合规场景下的 AI 推理可解释性:Attention 可视化与推理路径追踪的工程实践
人工智能
周末程序猿7 小时前
浅析大模型推理十二篇之KV Cache
人工智能
鱼樱前端7 小时前
用 AI 做内容变收入
前端·人工智能·ai编程
Dawson Zhu8 小时前
基于Palantir Foundry构建半导体制造AI友好型数据中台:从良率分析场景谈起
人工智能·语言模型·架构·制造·agi
fthux8 小时前
装闭 RenoPit 源码解析(04):装修图纸和合同文件上传处理流程
人工智能·ai·开源·github·open source·renopit
weixin_4462608510 小时前
从被动镜像到主动智能体:面向网络物理人工智能的整体论数字孪生(HDT-Net)
网络·人工智能
秋枫要学习10 小时前
你的鼠标,正在被 AI 抢走
人工智能·计算机外设