Tesseract3的常用API

初始化函数

(1) int Init(const char* datapath, const char* language,  char **configs, int configs_size, bool configs_global_only);
(2) int Init(const char* datapath, const char* language) { return Init(datapath, language, 0, 0, false);  }
(3) int InitLangMod(const char* datapath, const char* language);
(4) int InitWithoutLangModel(const char* datapath, const char* language);

函数主要参数:datapath表示语言包的路径,language:语言使用ISO 639-3 string或者默认使用英文(NULL),比如中文为”chi_sim”,英文为默认(NULL)或者写“eng”,其他的参数可采用默认。

初始化函数

(1) int Init(const char* datapath, const char* language,  char **configs, int configs_size, bool configs_global_only);
(2) int Init(const char* datapath, const char* language) { return Init(datapath, language, 0, 0, false);  }
(3) int InitLangMod(const char* datapath, const char* language);
(4) int InitWithoutLangModel(const char* datapath, const char* language);

函数主要参数:datapath表示语言包的路径,language:语言使用ISO 639-3 string或者默认使用英文(NULL),比如中文为”chi_sim”,英文为默认(NULL)或者写“eng”,其他的参数可采用默认

图片输入函数

(1) char* TesseractRect(const unsigned char* imagedata, int bytes_per_pixel, int bytes_per_line,

                        int left, int top, int width, int height);

TesseractRect函数:输入需要处理的图片,并且设定区域,imagedata:8位或者24位,32位彩色图片,其他调色板的图片需转换为24位图像
bytes_per_pixel:每像素的字节数;bytes_per_line,每行的字节数(对齐后的),其他的不解释

这个函数也可以拆分为一下几个函数:

(2) void SetImage(const unsigned char* imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line);
(3)  void SetRectangle(int left, int top, int width, int height);

SetImage函数:输入需要处理的图片,和TesseractRect的参数解释相同,注意的是这个函数会修改输入的图像

SetRectangle:设置需要处理的区域

获得识别结果

(4)char* GetUTF8Text();

获取文字图像中的文字信息,UTF8格式,API上说需要对获取的char*进行delete,但是我在测试的delete[]会出现错误。

对字符信任度评价

(5)int MeanTextConf();   //获取图像中文字识别结果的平均可信任度,大小为0~100
(6)int* AllWordConfidences(); //获取每个字符的可信任度,与GetUTF8Text获取的字符对应,值为0~100之间

个人觉得这类函数也是蛮重要的一类,可以对识别的结果做出大致的评价,对于评价较差的,可以另作处理,我测试的时候,做的好的识别,信任度识别都在80以上,做的不好的,就在80一下,还是可以大致说明识别结果的大致情况。

结束函数:

(7)void Clear(); //清tesseract的内部图片空间以及识别结果,可以多次使用
(8)void End();  //释放tesseract的所有内存,释放API

记得释放,尤其是循环使用的时候,使用clear释放上一次操作的空间。

tesseract也提供一些输出中间过程的函数,我没做研究,没有测试,API说明如下:

 
 /*在SetImage或者TesseractRect之后,获取内部阈值后图像的一个COPY*/
  Pix* GetThresholdedImage();

  /*获得版面分析的结果(layout analysis)
    在分析之前或者之后调用.*/
  Boxa* GetRegions(Pixa** pixa);

  /**
   * Get the textlines as a leptonica-style
   * Boxa, Pixa pair, in reading order.
   * Can be called before or after Recognize.
   * If blockids is not NULL, the block-id of each line is also returned
   * as an array of one element per line. delete [] after use.
   */
  Boxa* GetTextlines(Pixa** pixa, int** blockids);

  /**
   * Get the words as a leptonica-style
   * Boxa, Pixa pair, in reading order.
   * Can be called before or after Recognize.
   */
  Boxa* GetWords(Pixa** pixa);

  // Gets the individual connected (text) components (created
  // after pages segmentation step, but before recognition)
  // as a leptonica-style Boxa, Pixa pair, in reading order.
  // Can be called before or after Recognize.
  // Note: the caller is responsible for calling boxaDestroy()
  // on the returned Boxa array and pixaDestroy() on cc array.
  Boxa* GetConnectedComponents(Pixa** cc);

  // Get the given level kind of components (block, textline, word etc.) as a
  // leptonica-style Boxa, Pixa pair, in reading order.
  // Can be called before or after Recognize.
  // If blockids is not NULL, the block-id of each component is also returned
  // as an array of one element per component. delete [] after use.
  Boxa* GetComponentImages(PageIteratorLevel level,
                           Pixa** pixa, int** blockids);


上面的函数足以完成图像字符的识别,但是tesseract也提供了其他函数,比如图像读取,对识别的字符可信性进行评估以及获取识别过程中的中间图像

读取图像函数

(1) INT8 IMAGE::read_header ( const char *  name  );
(2) inT32 check_legal_image_size(                     //get rest of image
inT32 x,                      //x size required
inT32 y,                    //ysize required
inT8 bits_per_pixel  //bpp required
);
(3)inT8 read(inT32 buflines);

参考别人的例子的时候,会使用这个函数读取函数,但是我在使用的时候,发现3.0的版本并没发现IMAGE类里面的read函数和

read_header函数,可能是我用的文件问题吧,但是我本省也不想使用这个类,更想使用opencv完成图像的读取和预处理的工作,这里不多做说明了,如果哪位知道是哪里问题,可以告诉我哦。。。不适用提供的函数,使用OPENCV其实也很方便,不需要做任何转换,看下面的代码:

	IplImage *iplimg =  NULL;
	iplimg = cvLoadImage("1.jpg");
	tesseract::TessBaseAPI  api;
	//api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
	//api.SetVariable("classify_bln_numeric_mode", "123456789");
	api.Init("C:\\BuildFolder\\tesseract-3.01\\tessdata", NULL);
	//api.SetPageSegMode(PSM_SINGLE_BLOCK);
	api.SetImage((unsigned char*)(iplimg->imageData), 
						iplimg->width, iplimg->height,iplimg->nChannels  , iplimg->widthStep);//设置图像
	char* text = api.GetUTF8Text();//识别图像中的文字


这里是我的整个简单测试代码:


#include "stdafx.h"

#include "allheaders.h"
#include "baseapi.h"
#include "resultiterator.h"
#include "strngs.h"
#include "blobs.h"

#include "cv.h"
#include "highgui.h"
#include "cxcore.h"

#include "stdlib.h"
using namespace  tesseract;

int _tmain(int argc, _TCHAR* argv[])

{
	STRING text_out;

	IplImage *iplimg =  NULL;
	iplimg = cvLoadImage("1.jpg");
	tesseract::TessBaseAPI  api;
	//api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
	//SetVariable("tessedit_char_blacklist", "xyz"); to ignore x, y and z.
	//api.SetVariable("classify_bln_numeric_mode", "123456789");

	api.Init("C:\\BuildFolder\\tesseract-3.01\\tessdata", NULL);
	//api.SetPageSegMode(PSM_SINGLE_BLOCK);
	api.SetImage((unsigned char*)(iplimg->imageData), 
						iplimg->width, iplimg->height,iplimg->nChannels  , iplimg->widthStep);//设置图像
	char* text = api.GetUTF8Text();//识别图像中的文字

	printf("%s\n","获得的结果");
	printf("%s\n",text);
	FILE* fout = fopen("txt_file.TXT", "w");


	//fwrite(text_out.string(), 1, text_out.length(), fout);//将识别结果写入输出文件

	fprintf(fout,"%s\n","获得的结果");
	fprintf(fout,"%s\n",text);
	fclose(fout);

	
	UINT d = api.MeanTextConf();
	fprintf(fout,"%d\n",d);
	printf("%d\n",d);

	int *gg = api.AllWordConfidences();

	while (*gg != '\0')
	{
		printf("%d\n",*gg);
		gg ++ ;
	}


	getchar();

	api.Clear();
	api.End();
	return 0;

}

 转载自:http://www.cnblogs.com/zsb517/archive/2012/06/06/2537540.html




  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值