if (waitKey(10) >= 0){
// leer imagen
Mat imagen = imread("/home/adrian/workspace/OCR/matricula2.jpg"/*,CV_LOAD_IMAGE_GRAYSCALE*/);
imshow("imagen",imagen);
//procesamos imagen redimensionada: (filtramos, pasamos a escala grises, binarizamos)
medianBlur(imagen,imagen, 3);
cvtColor(imagen,imagen,CV_BGR2GRAY);
threshold(imagen,imagen,umbral, umbral_max,3);
// inicializamos motor OCR tesseract
putenv("TESSDATA_PREFIX=/usr/local/share/");
setlocale(LC_NUMERIC, "C");
tesseract::TessBaseAPI api;
printf("\nTesseract-ocr version: %s--------\t",api.Version()); //version de tesseract
printf("Leptonica version: %s\n", getLeptonicaVersion()); //version de leptonica
printf("___________________________________________________________________________\n");
if (api.Init(NULL, "spa")) { //idioma spanish
fprintf( stderr, " ¡No se pudo inicializar tesseract! \n" );
exit(1);
}
api.SetPageSegMode(tesseract::PSM_AUTO);
api.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789");
api.SetImage(imagen.data, imagen.size().width,imagen.size().height, imagen.channels(), imagen.step1());
// region de interes (ROI), p.ej. regiones que contengan texto
Rect textROI(0,0,imagen.cols,imagen.rows);//imagen completa
// recognize text
api.TesseractRect( imagen.data, 0,imagen.step1(), textROI.x, textROI.y,textROI.width, textROI.height);
char *texto = new char[200];
texto = api.GetUTF8Text();
// remove "newline"
string t1(texto);
t1.erase( remove(t1.begin(), t1.end(), '\n'), t1.end() );
// print found text
printf("TEXTO LEIDO: \n");
printf( "%s",t1.c_str() );
// draw rectangle image
rectangle(imagen, textROI, Scalar(0, 0, 255), 2, 8, 0);
imwrite("/home/adrian/workspace/OCR/procesadas/binaria.jpg",imagen);
imshow("binarizada",imagen);
delete [] texto;
// destroy tesseract OCR engine
api.Clear();
api.End();
}