1 | initial version |
The easiest path is to integrate tesseract library in your Opencv project, then use its API to recognize your characters. It's so possible to retrain your charset, if you suppose that your letters are special.
to install tesseract : see this tuto
this is an example of one of my projects:
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/features2d.hpp"
int main( int argc, char** argv )
{
cout << "OCR: starts" << endl;
Mat scene_plate = imread("plate.jpg", CV_LOAD_IMAGE_COLOR );
read_image(scene_plate.cols, scene_plate.rows, (char*)scene_plate.ptr());
}
void read_image(int width, int height, char *image)
{
cv::Mat Image(height, width, CV_8UC3, image);
// you may need to define the area of interest, where the test is found
// initializing Tesseract API
char *outText;
tesseract::TessBaseAPI *tess_api = new tesseract::TessBaseAPI();
if (tess_api->Init(NULL, "eng")) // eng is a flag of which trained language you use, if you just train your own language, you gave "XYZ" as a falge, you have to use it here
{
cout<<"Could not initialize tesseract.\n";
exit(1);
}
tess_api->SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345789.-");
tess_api->SetImage((uchar*)plate_image.data, plate_image.size().width, plate_image.size().height, plate_image.channels(), plate_image.step1());
tess_api->Recognize(0);
char* out =tess_api->GetUTF8Text();
double confidence =ocr_plate.confidence = tess_api->MeanTextConf();
cout<<"OCR output:"<< out<< " with confidence "<<confidence<<endl;
}
2 | No.2 Revision |
The easiest path is to integrate tesseract library in your Opencv project, then use its API to recognize your characters. It's so possible to retrain your charset, if you suppose that your letters are special.
to install tesseract : see this tuto
this is an example of one of my projects:
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/features2d.hpp"
int main( int argc, char** argv )
{
cout << "OCR: starts" << endl;
Mat scene_plate = imread("plate.jpg", CV_LOAD_IMAGE_COLOR );
read_image(scene_plate.cols, scene_plate.rows, (char*)scene_plate.ptr());
}
void read_image(int width, int height, char *image)
{
cv::Mat Image(height, width, CV_8UC3, image);
// you may need to define the area of interest, where the test is found
// initializing Tesseract API
char *outText;
tesseract::TessBaseAPI *tess_api = new tesseract::TessBaseAPI();
if (tess_api->Init(NULL, "eng")) // eng is a flag of which trained language you use, if you just train your own language, you gave "XYZ" as a falge, you have to use it here
{
cout<<"Could not initialize tesseract.\n";
exit(1);
}
tess_api->SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345789.-");
tess_api->SetImage((uchar*)plate_image.data, plate_image.size().width, plate_image.size().height, plate_image.channels(), plate_image.step1());
tess_api->Recognize(0);
char* out =tess_api->GetUTF8Text();
double confidence =ocr_plate.confidence = tess_api->MeanTextConf();
cout<<"OCR output:"<< out<< " with confidence "<<confidence<<endl;
}