说明:在使用Tesseract 之前,一定要安装好Tesseract ,并在Project中配置好Tesseract 路径。
在自己的工程属性中VC++目录下增加包含目录和库目录,以便VS2010查找文件。例如:
包含目录:新增C:\Program Files\Tesseract-OCR\include\tesseract
库目录:新增C:\Program Files\Tesseract-OCR\include\lib
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"; //简体中文chi_sim 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; }
进行OCR识别的主题函数为tesseract::TessBaseAPI:: bool TessBaseAPI::ProcessPage(Pix* pix, int page_index, const char* filename, const char* retry_config, int timeout_millisec,STRING* textout); 在分析流程时,专注此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; }