编译下面的程序操作系统必须在安装了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.\n");
        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:\n%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.\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);
  }

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';  \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n",
               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;\nWritingDirection: %d\nTextlineOrder: %d\n" \
         "Deskew angle: %.4f\n",
         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("\t\t ");
                  printf("\t- ");
                  const char* choice = ci.GetUTF8Text();
                  printf("%s conf: %f\n", choice, ci.Confidence());
                  indent = true;
              } while(ci.Next());
          }
          printf("---------------------------------------------\n");
          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\n");         handle = TessBaseAPICreate();
        if(TessBaseAPIInit3(handle, NULL, "eng") != 0)
                die("Error initialising tesseract\n");         TessBaseAPISetImage2(handle, img);
        if(TessBaseAPIRecognize(handle, NULL) != 0)
                die("Error in Tesseract recognition\n");         if((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
                die("Error getting text\n");         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引擎编程实例的更多相关文章

  1. Tesseract Ocr引擎

    Tesseract Ocr引擎 1.Tesseract介绍 tesseract 是一个google支持的开源ocr项目,其项目地址:https://github.com/tesseract-ocr/t ...

  2. Python下Tesseract Ocr引擎及安装介绍

    1.Tesseract介绍 tesseract 是一个google支持的开源ocr项目,其项目地址:https://github.com/tesseract-ocr/tesseract,目前最新的源码 ...

  3. 开源图片文字识别引擎——Tesseract OCR

    Tessseract为一款开源.免费的OCR引擎,能够支持中文十分难得.虽然其识别效果不是很理想,但是对于要求不高的中小型项目来说,已经足够用了. 文字识别可应用于许多领域,如阅读.翻译.文献资料的检 ...

  4. [转]Tesseract-OCR (Tesseract的OCR引擎最先由HP实验室于1985年开始研发)

    光学字符识别(OCR,Optical Character Recognition)是指对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程.OCR技术非常专业,一般多是印刷.打印行 ...

  5. tesseract ocr文字识别Android实例程序和训练工具全部源代码

    tesseract ocr是一个开源的文字识别引擎,Android系统中也可以使用.可以识别50多种语言,通过自己训练识别库的方式,可以大大提高识别的准确率. 为了节省大家的学习时间,现将自己近期的学 ...

  6. Tesseract ocr 3.02学习记录一

    光学字符识别(OCR,Optical Character Recognition)是指对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程.OCR技术非常专业,一般多是印刷.打印行 ...

  7. 转载:[转]如何学好3D游戏引擎编程

      [转]如何学好3D游戏引擎编程 Albert 本帖被 gamengines 从 游戏引擎(Game Engine) 此文为转载,但是值得一看. 此篇文章献给那些为了游戏编程不怕困难的热血青年,它的 ...

  8. 关于如何学好游戏3D引擎编程的一些经验[转]

    此篇文章献给那些为了游戏编程不怕困难的热血青年,它的神秘要我永远不间断的去挑战自我,超越自我,这样才能攀登到游戏技术的最高峰 ——阿哲VS自己 QQ79134054多希望大家一起交流与沟通 这篇文章是 ...

  9. Tesseract——OCR图像识别 入门篇

    Tesseract——OCR图像识别 入门篇 最近给了我一个任务,让我研究图像识别,从我们项目的screenshot中识别文字信息,so我开始了学习,与大家分享下. 我看到目前OCR技术有很多,最主要 ...

随机推荐

  1. CSS浏览器兼容性问题解决方法总结

    CSS浏览器兼容解决总结如下: 1. CSS中几种浏览器对不同关键字的支持,可进行浏览器兼容性重复定义 !important 可被FireFox和IE7识别 * 可被IE6.IE7识别 _ 可被IE6 ...

  2. 创建node.js一个简单的应用实例

    在node.exe所在目录下,创建一个叫 server.js 的文件,并写入以下代码: //使用 require 指令来载入 http 模块 var http = require("http ...

  3. C#winform的datagridview设置选中行

    this.dataGridView1.CurrentCell = this.dataGridView1[colIndex, rowIndex];this.dataGridView1.BindingCo ...

  4. PAT 甲级 1028. List Sorting (25) 【结构体排序】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...

  5. iOS 关于NSNotificationCenter

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,  Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification. NSNotificationCen ...

  6. iOSapp内跳转到设置界面

    从app内跳转到设置界面的代码如下: NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIAp ...

  7. git设置只允许特定类型的文件

    git设置只允许特定类型的文件 # 忽略所有文件 * # 不忽略目录 !*/ # 不忽略文件.gitignore和*.foo !.gitignore !*.foo

  8. HTTP 401.1 - 未授权:登录失败

    1 HTTP 401.1 - 未授权:登录失败 由于用户匿名访问使用的账号(默认是IUSR_机器名)被禁用,或者没有权限访问计算机,将造成用户无法访问.    解决方案: 1 打开IIS,右键站点,选 ...

  9. POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-3261 Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Tot ...

  10. 算法(Algorithms)第4版 练习 1.3.29

    代码实现: //1.3.29 package com.qiusongde.linkedlist; import java.util.Iterator; import java.util.NoSuchE ...