最近在搞文字识别,使用了Google的Tessract-OCR文本识别引擎,在此做个总结。
参考了以下资料:
- Google Tessetact项目地址
- 开源OCR引擎Tesseract使用介绍
- Tesseract-ocr体系结构
- 浅谈OCR之Tesseract
- 使用Tesseract OC 浅谈OCR之TesseractR 提取复杂图像中的文字 - physoft - 博客园
- tesseract 识别中文字符 - haoran-10 - ITeye技术网站
- Tesseract OCR 中文识别尝试 - muse
- tesseract-ocr训练方法 » 李鑫 Blog
- tesseract3.01的训练和使用
- Google leptonica项目地址 - 在Tesseract中使用的图像处理库
- LibTIFF - TIFF Library and Utilities-- 在Tesseract中使用的图像处理库
- 在VS2010下编译和使用tesseract_ocr识别验证码
- tesseract OCR的多语言,多字体字符识别(推荐)
- tesseract OCR训练新字体对图片的预处理和要求(推荐)
- tesseract OCR Engine overview字符识别学习(推荐)
- OCR学习及tesseract的一些测试(推荐)
- 一些开源的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;
}
本文介绍了如何在Visual Studio 2010中集成和使用Google的Tesseract-OCR引擎进行文字识别。内容涉及OCR的基础知识、相关资源链接以及Tesseract的训练和使用技巧。

9632

被折叠的 条评论
为什么被折叠?



