通过在Tesseract3+vs2008编译后获得的Tesseract 链接库可以就可以在C语言中调用函数,实现图片的识别,英文字符识别效果还可以,中文的效果就很差了,而且时间很久,具体过程可以参考文章:Tesseract3.01 OCR在VS2008环境下的编译使用(1)

本文主要对Tesseract3在C语言中的使用过程进行简单的测试。

Tesseract的主要函数成员:

初始化函数

(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)void SetPageSegMode(PageSegMode mode);  //设置版面分割的模式,默认为PSM_SINGLE_BLOCK模式
主要的模式如下:
enum PageSegMode { PSM_OSD_ONLY, ///< Orientation and script detection only. PSM_AUTO_OSD, ///< Automatic page segmentation with orientation and ///< script detection. (OSD) PSM_AUTO_ONLY, ///< Automatic page segmentation, but no OSD, or OCR. PSM_AUTO, ///< Fully automatic page segmentation, but no OSD. PSM_SINGLE_COLUMN, ///< Assume a single column of text of variable sizes. PSM_SINGLE_BLOCK_VERT_TEXT, ///< Assume a single uniform block of vertically ///< aligned text. PSM_SINGLE_BLOCK, ///< Assume a single uniform block of text. (Default.) PSM_SINGLE_LINE, ///< Treat the image as a single text line. PSM_SINGLE_WORD, ///< Treat the image as a single word. PSM_CIRCLE_WORD, ///< Treat the image as a single word in a circle. PSM_SINGLE_CHAR, ///< Treat the image as a single character. PSM_COUNT ///< Number of enum entries. };
(2)bool SetVariable(const char* name, const char* value);
我见到的用法如下,但是我测试的时候,觉得没什么效果:
//只识别bcdefghijklmnopqrstuvwsyz
SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwsyz);
//忽略ZXY
SetVariable("tessedit_char_blacklist", "xyz"); 
//只识别数字
SetVariable("classify_bln_numeric_mode", "123456789");

图片输入函数
(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();//识别图像中的文字

这里是我的整个简单测试代码,程序的设置,见我令我一篇博文Tesseract3.01 OCR在VS2008环境下的编译使用(1)

#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; }

 

作者:细雨淅淅

转载请注明地址:

 

 

相关文章: