C++及OpenCV中调用Tesseract
tesseract
tesseract-ocr/tesseract: 是一个开源的光学字符识别(OCR)引擎,适用于从图像中提取和识别文本。特点是可以识别多种语言,具有较高的识别准确率,并且支持命令行和API调用。
项目地址:https://gitcode.com/gh_mirrors/te/tesseract
免费下载资源
·
How to Use Tesseract with c++ or OpenCV and some code trace
1.Tesseract 在C++中直接使用
// tess.cpp:
// Recognize text on an image using Tesseract API and print it to the screen
// Usage: ./tess image.png
#include <tesseract/baseapi.h>
#include <tesseract/strngs.h>
#include <iostream>
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Please specify the input image!" << std::endl;
return -1;
}
const char* lang = "eng";
const char* filename = argv[1];
//新建tess基类
tesseract::TessBaseAPI tess;
//初始化
tess.Init(NULL, lang, tesseract::OEM_DEFAULT);
//设置识别模式
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
FILE* fin = fopen(filename, "rb");
if (fin == NULL)
{
std::cout << "Cannot open " << filename << std::endl;
return -1;
}
fclose(fin);
STRING text;
//进行识别
if (!tess.ProcessPages(filename, NULL, 0, &text))
{
std::cout << "Error during processing." << std::endl;
return -1;
}
else
std::cout << text.string() << std::endl;
return 0;
}
ubuntu下编译命令
g++ -I/usr/local/include `pkg-config --cflags --libs tesseract` tess.cpp -o tess
进行OCR识别的主体函数为:
tesseract::TessBaseAPI::
bool TessBaseAPI::ProcessPage(Pix* pix, int page_index, const char* filename,
const char* retry_config, int timeout_millisec,
STRING* text_out)
在分析流程时,专注此API,位置在baseapi.cpp;
后续分析;
2. Tesseract同OpenCV结合使用
可以选择ROI然后进行OCR;
为了识别cv::Mat矩阵,将其传递至TessBaseAPI::SetImage();
使用TessBaseAPI::GetUTF8Text()得到识别后的文字;
// tesscv.cpp:
// Using Tesseract API with OpenCV
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <tesseract/baseapi.h>
#include <iostream>
int main(int argc, char** argv)
{
// Usage: tesscv image.png
if (argc != 2)
{
std::cout << "Please specify the input image!" << std::endl;
return -1;
}
// Load image
cv::Mat im = cv::imread(argv[1]);
if (im.empty())
{
std::cout << "Cannot open source image!" << std::endl;
return -1;
}
cv::Mat gray;
cv::cvtColor(im, gray, CV_BGR2GRAY);
// ...other image pre-processing here...
// Pass it to Tesseract API
tesseract::TessBaseAPI tess;
tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
tess.SetImage((uchar*)gray.data, gray.cols, gray.rows, 1, gray.cols);
// Get the text
char* out = tess.GetUTF8Text();
std::cout << out << std::endl;
return 0;
}
ubuntu 编译命令
g++ -I/usr/local/include `pkg-config --cflags --libs opencv tesseract` tesscv.cpp -o tesscv
over!
GitHub 加速计划 / te / tesseract
60.1 K
9.29 K
下载
tesseract-ocr/tesseract: 是一个开源的光学字符识别(OCR)引擎,适用于从图像中提取和识别文本。特点是可以识别多种语言,具有较高的识别准确率,并且支持命令行和API调用。
最近提交(Master分支:2 个月前 )
bc490ea7
Don't check for a directory, because a symbolic link is also allowed.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
4 个月前
2991d36a - 4 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)