Reference address
Tesseract with C + + or OpenCV and some code trace 1.Tesseract Direct use in C + +
Tess.cpp:
//Recognize text on a image using Tesseract API and print it to the screen
//Usage:./tess image.p ng
#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];
New Tess base class
Tesseract::tessbaseapi Tess;
Initialize
Tess. Init (NULL, Lang, tesseract::oem_default);
Set the recognition mode
Tess. Setpagesegmode (tesseract::P sm_single_block);
file* fin = fopen (filename, "RB");
if (fin = = NULL)
{
std::cout << "Cannot open" << filename << std::endl;
return-1;
}
Fclose (Fin);
STRING text;
To identify
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 Compile command
g++-i/usr/local/include ' pkg-config--cflags--libs tesseract ' Tess.cpp-o Tess
The principal functions for OCR recognition are:
TESSERACT::TESSBASEAPI::
bool Tessbaseapi::P rocesspage (pix* Pix, int page_index, const char* filename,
const char* retry_config, int timeout_millisec,
string* text_out)
In the analysis process, focus on this API, location in Baseapi.cpp;
follow-up analysis; 2. Tesseract used in conjunction with OPENCV
You can select ROI and then OCR;
In order to identify the Cv::mat matrix, it is passed to Tessbaseapi::setimage ();
Use Tessbaseapi::getutf8text () to get the identified text;
Tesscv.cpp://Using tesseract API with OpenCV #include <opencv2/highgui/highgui.hpp> #include <OPENCV2/IMGPR oc/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!" << s
Td::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 ...//pass it to tesseract API Tesseract::tessbaseapi Tess; Tess.
Init (NULL, "Eng", Tesseract::oem_default); Tess.
Setpagesegmode (tesseract::P sm_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 Compile command
g++-i/usr/local/include ' pkg-config--cflags--libs opencv tesseract ' Tesscv.cpp-o TESSCV
over!