Skip to content

Commit

Permalink
Check in icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Haoest authored and Haoest committed Jul 27, 2011
1 parent f47b8ef commit 2ee7e2c
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 0 deletions.
Binary file added Sudokubot/basicOCR/.DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions Sudokubot/basicOCR/basicOCR.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* preprocessing.h
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/

#include <opencv/cv.h>
//#include <opencv/highgui.h>
#include <opencv/ml.h>
#include <stdio.h>
#include <ctype.h>


class basicOCR{
public:
float classify(IplImage* img,int showResult);
basicOCR ();
void test();
~basicOCR();
private:
int train_samples;
int classes;
CvMat* trainData;
CvMat* trainClasses;
int size;
int OCRTemplateCharacterSize;
int K;
CvKNearest *knn;
void getData(IplImage* OCRTemplate);
void train();
};
126 changes: 126 additions & 0 deletions Sudokubot/basicOCR/basicOCR.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* basicOCR.c
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 Damiles. GPL License
*
*/

#include <opencv/cv.h>
#include <opencv/ml.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "preprocessing.h"
#include "basicOCR.hpp"
#import "cvutil.hpp"


void basicOCR::getData(IplImage* OCRTemplate)
{
IplImage *prs_image;
CvMat row,data;
int i,j;
for(i =0; i<classes; i++){
for( j = 0; j< train_samples; j++){
/*
trainData = cvCreateMat(train_samples*classes, size*size, CV_32FC1);
trainClasses = cvCreateMat(train_samples*classes, 1, CV_32FC1);
*/
//process file
CvRect roi = cvRect(i * OCRTemplateCharacterSize, j * OCRTemplateCharacterSize, OCRTemplateCharacterSize, OCRTemplateCharacterSize);
cvSetImageROI(OCRTemplate, roi);
IplImage *src_image = cvCreateImage(cvGetSize(OCRTemplate), 8, 1);
cvCopy(OCRTemplate, src_image);
cvThreshold(src_image, src_image, 255/2, 255, CV_THRESH_BINARY);
//showImage(src_image, "");
prs_image = preprocessing(src_image, size, size);
cvReleaseImage(&src_image);
//Set class label
cvGetRow(trainClasses, &row, i*train_samples + j);
cvSet(&row, cvRealScalar(i));
//Set data
cvGetRow(trainData, &row, i*train_samples + j);
IplImage* img = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 );
//convert 8 bits image to 32 float image
cvConvertScale(prs_image, img, 1.0/255.0, 0);
cvGetSubRect(img, &data, cvRect(0,0, size,size));
CvMat row_header, *row1;
//convert data matrix sizexsize to vecor
row1 = cvReshape( &data, &row_header, 0, 1 );
cvCopy(row1, &row, NULL);
cvReleaseImage(&prs_image);
cvReleaseImage(&img);
}
}
}

void basicOCR::train()
{
knn=new CvKNearest( trainData, trainClasses, 0, false, K );
}

float basicOCR::classify(IplImage* img, int showResult)
{
IplImage *prs_image;
CvMat data;
CvMat* nearest=cvCreateMat(1,K,CV_32FC1);
float result;
//process file
prs_image = preprocessing(img, size, size);
if (prs_image==0) return 0;
//Set data
IplImage* img32 = cvCreateImage( cvSize( size, size ), IPL_DEPTH_32F, 1 );
cvConvertScale(prs_image, img32, 0.0039215, 0);
cvGetSubRect(img32, &data, cvRect(0,0, size,size));
//showImage(img32, "classify");
CvMat row_header, *row1;
row1 = cvReshape( &data, &row_header, 0, 1 );
result = knn->find_nearest(row1,K,0,0,nearest,0);
int accuracy=0;
for(int i=0;i<K;i++){
if( nearest->data.fl[i] == result)
accuracy++;
}
float pre=100*((float)accuracy/(float)K);
if(showResult==1){
printf("|\t%.0f\t| \t%.2f%% \t| \t%d of %d \t| \n",result,pre,accuracy,K);
printf(" ---------------------------------------------------------------\n");
}
cvReleaseImage(&prs_image);
cvReleaseImage(&img32);
cvReleaseMat(&nearest);
return result;
}

basicOCR::basicOCR()
{
OCRTemplateCharacterSize = 50;
// IplImage *OCRTemplate = cvLoadImage("OCRTemplateSet3.png", 0);
IplImage *OCRTemplate = [cvutil LoadUIImageAsIplImage:@"OCRTemplateSet3.png" asGrayscale:YES ignoreOrientation:YES];
train_samples = OCRTemplate->height / OCRTemplateCharacterSize;
classes= 10;
size=40;
K = 1;

trainData = cvCreateMat(train_samples*classes, size*size, CV_32FC1);
trainClasses = cvCreateMat(train_samples*classes, 1, CV_32FC1);

//Get data (get images and process it)
getData(OCRTemplate);
//train
train();
cvReleaseImage(&OCRTemplate);
// printf(" ---------------------------------------------------------------\n");
// printf("|\tClass\t|\tPrecision\t|\tAccuracy\t|\n");
// printf(" ---------------------------------------------------------------\n");

}

basicOCR::~basicOCR(){
cvReleaseMat(&trainData);
cvReleaseMat(&trainClasses);
knn->clear();
delete knn;
}
112 changes: 112 additions & 0 deletions Sudokubot/basicOCR/preprocessing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* preprocessing.c
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/

#include "preprocessing.h"
#include <opencv/cv.h>
//#include <opencv/highgui.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>


/*****************************************************************
*
* Find the min box. The min box respect original aspect ratio image
* The image is a binary data and background is white.
*
*******************************************************************/
void findX(IplImage* imgSrc,int* min, int* max){
int i;
int minFound=0;
CvMat data;
CvScalar maxVal=cvRealScalar(imgSrc->height * 255);
CvScalar val=cvRealScalar(0);
//For each col sum, if sum < width*255 then we find the min
//then continue to end to search the max, if sum< width*255 then is new max
for (i=0; i< imgSrc->width; i++){
cvGetCol(imgSrc, &data, i);
val= cvSum(&data);
if(val.val[0] < maxVal.val[0]){
*max= i;
if(!minFound){
*min= i;
minFound= 1;
}
}
}
}

void findY(IplImage* imgSrc,int* min, int* max){
int i;
int minFound=0;

CvScalar maxVal=cvRealScalar(imgSrc->width * 255);
CvScalar val=cvRealScalar(0);
//For each col sum, if sum < width*255 then we find the min
//then continue to end to search the max, if sum< width*255 then is new max
for (i=0; i< imgSrc->height; i++){
CvMat data;
cvGetRow(imgSrc, &data, i);
val= cvSum(&data);
if(val.val[0] < maxVal.val[0]){
*max=i;
if(!minFound){
*min= i;
minFound= 1;
}
}
}
}

CvRect findBB(IplImage* imgSrc){
CvRect aux;
int xmin, xmax, ymin, ymax;
xmin=xmax=ymin=ymax=0;

findX(imgSrc, &xmin, &xmax);
findY(imgSrc, &ymin, &ymax);

aux=cvRect(xmin, ymin, xmax-xmin, ymax-ymin);

return aux;

}

IplImage* preprocessing(IplImage* imgSrc,int new_width, int new_height){
IplImage* result;
IplImage* scaledResult;
CvMat data;
CvMat dataA;
CvRect bb;//bounding box
//Find bounding box
bb=findBB(imgSrc);
if (bb.width == 0 || bb.height ==0){
return 0;
}
//Get bounding box data and no with aspect ratio, the x and y can be corrupted
cvGetSubRect(imgSrc, &data, cvRect(bb.x, bb.y, bb.width, bb.height));
//Create image with this data with width and height with aspect ratio 1
//then we get highest size betwen width and height of our bounding box
int size=(bb.width>bb.height)?bb.width:bb.height;
result=cvCreateImage( cvSize( size, size ), 8, 1 );
cvSet(result,CV_RGB(255,255,255),NULL);
//Copy de data in center of image
int x=(int)floor((float)(size-bb.width)/2.0f);
int y=(int)floor((float)(size-bb.height)/2.0f);
cvGetSubRect(result, &dataA, cvRect(x,y,bb.width, bb.height));
cvCopy(&data, &dataA, NULL);
//Scale result
scaledResult=cvCreateImage( cvSize( new_width, new_height ), 8, 1 );
cvResize(result, scaledResult, CV_INTER_NN);
cvReleaseImage(&result);
//Return processed data
return scaledResult;
}


18 changes: 18 additions & 0 deletions Sudokubot/basicOCR/preprocessing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* preprocessing.h
*
*
* Created by damiles on 18/11/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/

#include <opencv/cv.h>
//#include <opencv/highgui.h>
#include <stdio.h>
#include <ctype.h>


IplImage *preprocessing(IplImage* imgSrc,int new_width, int new_height);
CvRect findBB(IplImage* imgSrc);

Binary file added SudokubotTests/testData/book_lowlight_1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon-72.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ss-archive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ss-home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ss-preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2ee7e2c

Please sign in to comment.