c/c++语言实现tesseract ocr引擎编程实例

摘要:
要编译以下程序,操作系统必须将tesseract库和leptin库安装到Basicexamplec++代码:#include #include intmain(){character*outText;tesseract::TessBaseAPI*api=newtesseract::TessBaseAPI();
编译下面的程序操作系统必须在安装了tesseract库和leptonica库才可以
Basic example

c++ code:

#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. ");
        exit(1);
    }

    // Open input image with leptonica library
    Pix *image = pixRead("/usr/src/tesseract-3.02/phototest.tif");
    api->SetImage(image);
    // Get OCR result
    outText = api->GetUTF8Text();
    printf("OCR output: %s", outText);

    // Destroy used object and release memory
    api->End();
    delete [] outText;
    pixDestroy(&image);

    return 0;
}



GetComponentImages example
  Pix *image = pixRead("/usr/src/tesseract-3.02/phototest.tif");
  tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
  api->Init(NULL, "eng");
  api->SetImage(image);
  Boxa* boxes = api->GetComponentImages(tesseract::RIL_TEXTLINE, true, NULL, NULL);
  printf("Found %d textline image components. ", 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);
  }
Result iterator example

There is posibility to get confidence value and BoundingBox per word from ResultIterator:

  Pix *image = pixRead("/usr/src/tesseract-3.02/phototest.tif");
  tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
  api->Init(NULL, "eng");
  api->SetImage(image);
  api->Recognize(0);
  tesseract::ResultIterator* ri = api->GetIterator();
  tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
  if (ri != 0) {
    do {
      const char* word = ri->GetUTF8Text(level);
      float conf = ri->Confidence(level);
      int x1, y1, x2, y2;
      ri->BoundingBox(level, &x1, &y1, &x2, &y2);
      printf("word: '%s';   conf: %.2f; BoundingBox: %d,%d,%d,%d; ",
               word, conf, x1, y1, x2, y2);
      delete[] word;
    } while (ri->Next(level));
  }
Orientation and script detection (OSD) example
  const char* inputfile = "/usr/src/tesseract-3.02/eurotext.tif";
  tesseract::Orientation orientation;
  tesseract::WritingDirection direction;
  tesseract::TextlineOrder order;
  float deskew_angle;

  PIX *image = pixRead(inputfile);
  tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
  api->Init("/usr/src/tesseract-3.02/", "eng");
  api->SetPageSegMode(tesseract::PSM_AUTO_OSD);
  api->SetImage(image);
  api->Recognize(0);

  tesseract::PageIterator* it =  api->AnalyseLayout();
  it->Orientation(&orientation, &direction, &order, &deskew_angle);
  printf("Orientation: %d; WritingDirection: %d TextlineOrder: %d "
         "Deskew angle: %.4f ",
         orientation, direction, order, deskew_angle);




Example of iterator over the classifier choices for a single symbol
  Pix *image = pixRead("/usr/src/tesseract-3.02/phototest.tif");
  tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
  api->Init(NULL, "eng");
  api->SetImage(image);
  api->SetVariable("save_blob_choices", "T");
  api->SetRectangle(37, 228, 548, 31);
  api->Recognize(NULL);

  tesseract::ResultIterator* ri = api->GetIterator();
  tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
  if(ri != 0) {
      do {
          const char* symbol = ri->GetUTF8Text(level);
          float conf = ri->Confidence(level);
          if(symbol != 0) {
              printf("symbol %s, conf: %f", symbol, conf);
              bool indent = false;
              tesseract::ChoiceIterator ci(*ri);
              do {
                  if (indent) printf(" ");
                  printf(" - ");
                  const char* choice = ci.GetUTF8Text();
                  printf("%s conf: %f ", choice, ci.Confidence());
                  indent = true;
              } while(ci.Next());
          }
          printf("--------------------------------------------- ");
          delete[] symbol;
      } while((ri->Next(level)));
  }


在linux下面编译C++ 程序的方法:
g++ -o myprogram myprogram.cpp -llept -ltesseract

如果你安装tesssract的时候不是通常的路径,需要手动指定头文件的include和库lib的路径。加上选项-I和-L如:
g++ -o myprogram myprogram.cpp -I/home/nick/local/include/tesseract -L/home/nick/local/lib -llept -ltesseract


c语言程序的例子:
#include <stdio.h>
#include <allheaders.h>
#include <capi.h>

void die(const char *errstr) {
        fputs(errstr, stderr);
        exit(1);
}

int main(int argc, char *argv[]) {
        TessBaseAPI *handle;
        PIX *img;
        char *text;

        if((img = pixRead("img.png")) == NULL)
                die("Error reading image ");

        handle = TessBaseAPICreate();
        if(TessBaseAPIInit3(handle, NULL, "eng") != 0)
                die("Error initialising tesseract ");

        TessBaseAPISetImage2(handle, img);
        if(TessBaseAPIRecognize(handle, NULL) != 0)
                die("Error in Tesseract recognition ");

        if((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
                die("Error getting text ");

        fputs(text, stdout);

        TessDeleteText(text);
        TessBaseAPIEnd(handle);
        TessBaseAPIDelete(handle);
        pixDestroy(&img);

        return 0;
}
在linux下面编译C语言程序的方法和c++的方法是一样的,只不过换个编译器就好:
gcc -o myprogram myprogram.cpp -llept -ltesseract

如果你安装tesssract的时候不是通常的路径,需要手动指定头文件的include和库lib的路径。加上选项-I和-L如:
gcc -o myprogram myprogram.cpp -I/home/nick/local/include/tesseract -L/home/nick/local/lib -llept -ltesseract
运行方法:
把一个图片复制到当前目录下命名为
img.png
./myprogram img.png

免责声明:文章转载自《c/c++语言实现tesseract ocr引擎编程实例》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python3的leetcode题,两个数求和等于目标值,返回这两个数的索引组成的列表(三种方法)uniapp打包h5页面ios唤起软键盘踩坑下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

kubernetes 1.21部署 kubeprometheus

https://github.com/prometheus-operator/kube-prometheus 1. helm方式 helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update helm search...

h264格式的flv和mkv无损转换成mp4的方法

现在很多flv和mkv视频都是采用的h264封装,移动设备往往并不支持这些格式的文件,但却对h264封装的mp4支持良好。因此,为了视频能在电脑和移动设备间共享,我通常会将其转换成h264封装的mp4文件。 由于视频转码非常耗时间和cpu,如果flv和mkv本来就是采用的h264封装,完全不需要转码,只需要把h264视频和音频文件分离出来,重新混流一次即可...

python图像处理之pyocr

使用pyocr类库进行ocr识别,其中tools为’Tesseract’ #!/usr/bin/env python #coding=utf-8 __author__ = 'zhangdebin' from PIL import Image import sys import pyocr tools = pyocr.get_available_to...

Kaggle系列1:手把手教你用tensorflow建立卷积神经网络实现猫狗图像分类

去年研一的时候想做kaggle上的一道题目:猫狗分类,但是苦于对卷积神经网络一直没有很好的认识,现在把这篇文章的内容补上去。(部分代码参考网上的,我改变了卷积神经网络的网络结构,其实主要部分我加了一层1X1的卷积层,至于作用,我会在后文详细介绍) 题目地址:猫狗大战 同时数据集也可以在上面下载到。 既然是手把手,那么就要从前期的导入数据开始: 导入...

js获取元素下所有子元素总宽度赋值给父元素

这个问题是今天在网上看到有人提的。 想要获取#box下面所有div的宽度之和,然后赋值给#box,不论加多少个div,#box的宽都会随着div的增加而改变。 <style> #box{display:inline-block; border:1px #333 solid;} </style> <...

halcon图像拼接

read_image (Image, 'D:/Working/工程项目/多张图像拼接/1.0D-3.5HZ_C001H001S0001/1.tif') dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image) get_image_size (Ima...