原文地址:http://blog.damiles.com/2008/11/basic-ocr-in-opencv/
代码下载:https://github.com/damiles/basicOCR
In this tutorial we go to create a basic number OCR. It consist to classify a handwrite number into his class.
To do it, we go to use all we learn in before tutorials, we go to use a simple basic painter and the basic pattern recognition and classification with openCV tutorial.
In a typical pattern recognition classifier consist in three modules:
Preprocessing: in this module we go to process our input image, for example size normalize, convert color to BN…
Feature extraction: in this module we convert our image processed to a characteristic vector of features to classify, it can be the pixels matrix convert to vector or get contour chain codes data representation
Classification module get the feature vectors and train our system or classify an input feature vector with a classify method as knn.
In this basic OCR we go to use this graph:
Where we get a train set and test set of image to train and test our classifier method (knn)
We have a 1000 handwrite images, 100 images of each number. We get 50 images of each number (class) to train and other 50 to test our system.
Then the first work we do is pre-process all train image, to do it we create a preprocessing function. In this function we get a image and a new width and height we want as result of preprocessing, then the function return a normalized size with bounding box image. You can see more clear the process in this graph:
Pre-processing code:
01 |
void findX(IplImage* imgSrc, int * min, int * max){ |
05 |
CvScalar maxVal=cvRealScalar(imgSrc->width * 255); |
06 |
CvScalar val=cvRealScalar(0); |
09 |
for (i=0; i< imgSrc->width; i++){ |
10 |
cvGetCol(imgSrc, &data, i); |
12 |
if (val.val[0] < maxVal.val[0]){ |
22 |
void findY(IplImage* imgSrc, int * min, int * max){ |
26 |
CvScalar maxVal=cvRealScalar(imgSrc->width * 255); |
27 |
CvScalar val=cvRealScalar(0); |
30 |
for (i=0; i< imgSrc->height; i++){ |
31 |
cvGetRow(imgSrc, &data, i); |
33 |
if (val.val[0] < maxVal.val[0]){ |
42 |
CvRect findBB(IplImage* imgSrc){ |
44 |
int xmin, xmax, ymin, ymax; |
45 |
xmin=xmax=ymin=ymax=0; |
47 |
findX(imgSrc, &xmin, &xmax); |
48 |
findY(imgSrc, &ymin, &ymax); |
50 |
aux=cvRect(xmin, ymin, xmax-xmin, ymax-ymin); |
58 |
IplImage preprocessing(IplImage* imgSrc, int new_width, int new_height){ |
60 |
IplImage* scaledResult; |
71 |
cvGetSubRect(imgSrc, &data, cvRect(bb.x, bb.y, bb.width, bb.height)); |
74 |
int size=(bb.width>bb.height)?bb.width:bb.height; |
75 |
result=cvCreateImage( cvSize( size, size ), 8, 1 ); |
76 |
cvSet(result,CV_RGB(255,255,255),NULL); |
78 |
int x=( int ) floor (( float )(size-bb.width)/2.0f); |
79 |
int y=( int ) floor (( float )(size-bb.height)/2.0f); |
80 |
cvGetSubRect(result, &dataA, cvRect(x,y,bb.width, bb.height)); |
81 |
cvCopy(&data, &dataA, NULL); |
83 |
scaledResult=cvCreateImage( cvSize( new_width, new_height ), 8, 1 ); |
84 |
cvResize(result, scaledResult, CV_INTER_NN); |
We use the function getData of basicOCR class to create the train data and train classes, this function get all images under OCR folder to create this train data, the OCR forlder is structured with 1 folder to each class and each file have are pbm files with this name cnn.pbm where c is the class {0..9} and nn is the number of image {00..99}
Each image we get is pre-processed and then convert the data in a feature vector we use.
basicOCR.cpp getData code:
01 |
void basicOCR::getData() |
08 |
for (i =0; i<classes; i++){ |
09 |
for ( j = 0; j< train_samples; j++){ |
13 |
sprintf (file, "%s%d/%d0%d.pbm" ,file_path, i, i , j); |
15 |
sprintf (file, "%s%d/%d%d.pbm" ,file_path, i, i , j); |
16 |
src_image = cvLoadImage(file,0); |
18 |
printf ( "Error: Cant load image %s\n" , file); |
22 |
prs_image = preprocessing(src_image, size, size); |
25 |
cvGetRow(trainClasses, &row, i*train_samples + j); |
26 |
cvSet(&row, cvRealScalar(i)); |
28 |
cvGetRow(trainData, &row, i*train_samples + j); |
30 |
IplImage* img = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 ); |
32 |
cvConvertScale(&prs_image, img, 0.0039215, 0); |
34 |
cvGetSubRect(img, &data, cvRect(0,0, size,size)); |
36 |
CvMat row_header, *row1; |
38 |
row1 = cvReshape( &data, &row_header, 0, 1 ); |
39 |
cvCopy(row1, &row, NULL); |
After processed and get train data and classes whe then train our model with this data, in our sample we use knn method then:
1 |
knn= new CvKNearest( trainData, trainClasses, 0, false , K ); |
Then we now can test our model, and we can use the test result to compare to another methods we can use, or if we reduce the image scale or similar. There are a function to create the test in our basicOCR class, test function.
This function get the other 500 samples and classify this in our selected method and check the obtained result.
01 |
void basicOCR::test(){ |
09 |
for (i =0; i<classes; i++){ |
10 |
for ( j = 50; j< 50+train_samples; j++){ |
12 |
sprintf (file, "%s%d/%d%d.pbm" ,file_path, i, i , j); |
13 |
src_image = cvLoadImage(file,0); |
15 |
printf ( "Error: Cant load image %s\n" , file); |
19 |
prs_image = preprocessing(src_image, size, size); |
20 |
float r=classify(&prs_image,0); |
27 |
float totalerror=100*( float )error/( float )testCount; |
28 |
printf ( "System Error: %.2f%%\n" , totalerror); |
Test use the classify function that get image to classify, process image, get feature vector and classify it with a find_nearest of knn class. This function we use to classify the input user images:
01 |
float basicOCR::classify(IplImage* img, int showResult) |
05 |
CvMat* nearest=cvCreateMat(1,K,CV_32FC1); |
08 |
prs_image = preprocessing(img, size, size); |
11 |
IplImage* img32 = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 ); |
12 |
cvConvertScale(&prs_image, img32, 0.0039215, 0); |
13 |
cvGetSubRect(img32, &data, cvRect(0,0, size,size)); |
14 |
CvMat row_header, *row1; |
15 |
row1 = cvReshape( &data, &row_header, 0, 1 ); |
17 |
result=knn->find_nearest(row1,K,0,0,nearest,0); |
21 |
if ( nearest->data.fl[i] == result) |
24 |
float pre=100*(( float )accuracy/( float )K); |
26 |
printf ( "|\t%.0f\t| \t%.2f%% \t| \t%d of %d \t| \n" ,result,pre,accuracy,K); |
27 |
printf ( " ---------------------------------------------------------------\n" ); |
All work or training and test is in basicOCR class, when we create a basicOCR instance then only we need call to classify function to classify our input image. Then we go to use basic Painter we create before in other tutorial to user interactivity to draw a image and classify it.