Tesseract-OCR 3.04简单使用举例(读入图像输出识别结果)

        下面code是对Tesseract-OCR 3.04版本进行简单使用的举例:包括两段,一个是读入带有中文字符的图像,一个是读入仅有英文字符的图像:

#include "funset.hpp"

#include <iostream>
#include <string>
#include <vector>

#include <allheaders.h>

#include <baseapi.h>
#include <basedir.h>
#include <renderer.h>
#include <strngs.h>
#include <tprintf.h>
#include <openclwrapper.h>

namespace {

#ifdef _MSC_VER
void utf8_to_gbk(const char* utf8, char* gbk)
{
	const int maxlen = 128;
	wchar_t unicode_str[maxlen];
	int outlen = MultiByteToWideChar(CP_UTF8, 0, utf8, strlen(utf8), unicode_str, maxlen);
	outlen = WideCharToMultiByte(CP_ACP, 0, unicode_str, outlen, gbk, 128, nullptr, nullptr);
	gbk[outlen] = '\0';
}
#endif

} // namespace

int test_recognize_image_content_1()
{
	// reference: https://github.com/tesseract-ocr/tesseract/wiki/APIExample
{ // chinese
	// Initialize tesseract-ocr
	tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
	const char* datapath{ "E:/GitCode/OCR_Test/test_data/tessdata/" };
	if (api->Init(datapath, "chi_sim")) {
		fprintf(stderr, "Could not initialize tesseract.\n");
		return -1;
	}

	// Open input image with leptonica library
	Pix* image = pixRead("E:/GitCode/OCR_Test/test_data/chi_sim_1.png");
	api->SetImage(image);
	// Get OCR result
	char* outText = api->GetUTF8Text();
	char gbk[256];
	utf8_to_gbk(outText, gbk);
	fprintf(stdout, "OCR output: %s\n", gbk);

	// Destroy used object and release memory
	api->End();
	delete api;
	delete[] outText;
	pixDestroy(&image);
}

{ // english
	// Initialize tesseract-ocr
	tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
	const char* datapath{ "E:/GitCode/OCR_Test/test_data/tessdata/" };
	if (api->Init(datapath, "eng")) {
		fprintf(stderr, "Could not initialize tesseract.\n");
		return -1;
	}

	// Open input image with leptonica library
	Pix* image = pixRead("E:/GitCode/OCR_Test/test_data/eng_1.png");
	api->SetImage(image);
	// Get OCR result
	char* outText = api->GetUTF8Text();
	char gbk[256];
	utf8_to_gbk(outText, gbk);
	fprintf(stdout, "OCR output: %s\n", gbk);

	// Destroy used object and release memory
	api->End();
	delete api;
	delete[] outText;
	pixDestroy(&image);
}

	return 0;
}

        测试图像如下:



        执行结果如下:


        GitHub: https://github.com/fengbingchun/OCR_Test 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值