Tesseract-OCR引擎和VS2010工程简单调用

最近在搞文字识别,使用了Google的Tessract-OCR文本识别引擎,在此做个总结。

参考了以下资料:

两种可调用方法:
方法一:
指定矩形区域进行识别
// Tesseract3.2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#pragma comment(lib, "public/lib/libtesseract302.lib")
#pragma comment(lib, "lib/liblept.lib")

#include "public/include/tesseract/baseapi.h"
#include "public/include/tesseract/basedir.h"
#include "public/include/tesseract/strngs.h"
#include "include/leptonica/allheaders.h"

//到谷歌官网下载tesseract-3.02.02-win32-lib-include-dirs.zip 识别引擎
//以及leptonica-1.68-win32-lib-include-dirs.zip  图像处理库

void ConvertUtf8ToGBK(char **amp,char *strUtf8)
{
	int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, NULL,0); 
	unsigned short * wszGBK = new unsigned short[len+1]; 
	memset(wszGBK, 0, len * 2 + 2); 
	MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, (LPWSTR)wszGBK, len); 
	len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL); 
	//char *szGBK=new char[len + 1]; 
	*amp = new char[len+1];
	memset(*amp, 0, len + 1); 
	WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, *amp, len, NULL,NULL); 
	//amp=szGBK; 
}

int _tmain(int argc, _TCHAR* argv[])
{
	char *filepath = "ocr_en.tif";
	tesseract::TessBaseAPI api;
	//api.SetOutputName("output");
	api.SetInputName(filepath);
	api.SetPageSegMode(tesseract::PSM_AUTO);
	int nRet = api.Init("tessdata", "eng");
	if (nRet != 0)
	{
		#if _DEBUG
				printf("初始化字库失败!");
		#endif
				return 0;
	}

	PIX *pix = pixRead(filepath);
	api.SetImage(pix);


	char *pdata = NULL;
	char *pResult = NULL;
	api.SetRectangle(0, 0, 400, 80);

	pdata = api.GetUTF8Text();//进行识别
	ConvertUtf8ToGBK(&pResult, pdata); //对结果转码,保存在pResultl里

#if _DEBUG
	printf("识别结果:%s", pResult);
	getchar();
#endif
	delete pResult;
	pixDestroy(&pix);
	api.Clear();
	api.End();
	return 0;
}
方法二:
对整幅图像进行识别
#pragma  comment(lib,"libtesseract302.lib")
#pragma  comment(lib,"liblept168.dll")

using namespace std;
using namespace cv;


string UTF8ToGBK(const std::string& strUTF8);//编码转换

/*
 * 调用tesseract api 引擎进行识别
 * 输入:char *str -->待识别的图片
 * 输出:识别出的文字,改进:把图片显示出来
 */

int tesseract_ocr(char*picname)
{
	double t;  	
	tesseract::TessBaseAPI  api;    
	api.Init(NULL, "chi_sim", tesseract::OEM_DEFAULT);   

	t = (double)getTickCount(); 
	STRING text_out;
	if (!api.ProcessPages(picname, NULL, 0, &text_out)) {    
		printf("Can not read %s this picture!!!",picname);
		return 0;    
	}

	t = ((double)getTickCount() - t)/getTickFrequency();  
	cout<<"耗时"<<t<<"秒"<<endl<<endl;
	string gbk=UTF8ToGBK(text_out.string());//显示乱码,输出的utf-8,要转换
	cout<<gbk<<endl;  //C++如何把string类型保存在文件中

	//将识别结果写入输出文件
	FILE* fout = fopen("ocr_result.txt", "a");
	fprintf(fout,"图片:%s\n%s",picname,gbk);
	//fwrite(text_out.string(), 1, text_out.length(), fout);
	fclose(fout);

	return 0;

}


/*
 *	utf-8转gbk,在终端就不会显示为乱码
 */
string UTF8ToGBK(const std::string& strUTF8)  
{  
	int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);  
	unsigned short * wszGBK = new unsigned short[len + 1];  
	memset(wszGBK, 0, len * 2 + 2);  
	MultiByteToWideChar(CP_UTF8, 0,LPCSTR(strUTF8.c_str()), -1, LPWSTR(wszGBK), len);  

	len = WideCharToMultiByte(CP_ACP, 0,LPCTSTR(wszGBK), -1, NULL, 0, NULL, NULL);  
	char *szGBK = new char[len + 1];  
	memset(szGBK, 0, len + 1);  
	WideCharToMultiByte(CP_ACP,0, LPCTSTR(wszGBK), -1, szGBK, len, NULL, NULL);  
	//strUTF8 = szGBK;  
	std::string strTemp(szGBK);  
	delete[]szGBK;  
	delete[]wszGBK;  
	return strTemp;  
}  


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页