Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

///////////////////////////// Project in Microsoft Visual Studio with OpenCV ///

/////////////////////////
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace cv;
using namespace std;
void
void
void
void
void
void
void

testing();
test0();
test479();
test5();
test3();
test2();
test68();

bool a[800][2000];
int b[8][8];
int check[10]={0};
int r[600],c[600];
int interesting_regions[4][20];
int _tmain(int argc, _TCHAR* argv[])
{
int i,j,k,x,y,intensity,row,col,row8,col8,low,high,thresh;
int num_cnt,mode,rlow,rhigh,clow,chigh;
Mat img;
/////////////////////////////////////// Loading Image ////////////////////////
/////
img = imread("C:\\Users\\Ankit\\Documents\\Visual Studio 2010\\Projects\
\myNewOpenCV\\myNewOpenCV\\26.jpg",0);
row=img.rows;
col=img.cols;
//////////////////////////////////// Converting into 0 and 1 Matrix ////////////
////
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
intensity=img.at<uchar>(i,j);
if(intensity>128)
a[i][j]=0;
else
a[i][j]=1;
}
}
////////////////////////////// Calculating HISTROGRAMS ////////////////////////
/////
// For Each Column

for(i=0;i<col;i++)
{
c[i]=0;
for(j=1;j<row;j++)
{
if(a[j-1][i]!=a[j][i])
c[i]++;
}
if(c[i]>8)
c[i]=0;
}
/////////////// calculating interesting regions////////////////
// For Columns
num_cnt=0;
mode=0;
for(i=0;i<col;i++)
{
if(c[i]>0 && mode==0)
{
interesting_regions[0][num_cnt]=i;
//cout<<i<<endl;
mode=1;
}
else if(c[i]==0 && mode==1)
{
interesting_regions[1][num_cnt++]=i;
//cout<<i<<endl;
mode=0;
}
}
cout<<"NUMBER OF DIGITS FOUND : "<<num_cnt<<endl;
// For Rows
for(i=0;i<num_cnt;++i)
{
low=interesting_regions[0][i];
high=interesting_regions[1][i];
for(k=0;k<row;k++)
{
r[k]=0;
for(j=low;j<=high;j++)
{
if(a[k][j-1]!=a[k][j])
r[k]++;
}
if(r[k]>8)
r[k]=0;
}
for(j=0;j<=row;++j)
{
if(r[j]!=r[j+1])
{

interesting_regions[2][i]=j;
break;
}
}
for(j=row;j>=0;--j)
{
if(r[j]!=r[j-1])
{
interesting_regions[3][i]=j;
break;
}
}
}
////////////////////////// Digitwise Recognization /////////////////////
/////
for(i=0; i<num_cnt; ++i)
{
/////// Making Regions of Intrest Multiple of 8 ////////
rlow=interesting_regions[2][i];
rhigh=interesting_regions[3][i];
clow=interesting_regions[0][i];
chigh=interesting_regions[1][i];
//cout<<rlow<<" "<<rhigh<<" "<<clow<<" "<<chigh<<endl;
/// For Rows
row=rhigh-rlow+1;
j=8-(row%8);
if(j%2==0)
rlow=rlow-(j/2);
else
rlow=rlow-(j/2)-1;
rhigh=rhigh+(j/2);
row=rhigh-rlow+1;
/// For Cols
col=chigh-clow+1;
j=8-(col%8);
if(j%2==0)
clow=clow-(j/2);
else
clow=clow-(j/2)-1;
chigh=chigh+(j/2);
col=chigh-clow+1;
///////////////// Mapping 8x8 Matrix /////////////////
row8=row/8;
col8=col/8;

memset(b,0,sizeof(b));
for(k=rlow;k<rhigh;k++)
{
for(j=clow;j<chigh;j++)
{
x=(k-rlow)/row8;
y=(j-clow)/col8;
b[x][y]+=a[k][j];
}
}
////////////////////// 0 1 Bitmap ///////////////////////
thresh=(0.05)*row8*col8;
for(k=0;k<8;k++)
{
for(j=0;j<8;j++)
{
if(b[k][j]>thresh)
b[k][j]=1;
else
b[k][j]=0;
}
}
/////////////////// Displaying 8x8 Matrix //////////////////
cout<<"\n\nDigit Captured in 8x8 Bit Matrix \n\n\n";
for(k=0;k<8;k++)
{
for(j=0;j<8;j++)
{
if(b[k][j])
cout<<"X";
else
cout<<".";
}
cout<<endl;
}
memset(check,0,sizeof(check));
if(thresh<15)
{
check[1]=1;
}
else
{
testing();
}
cout<<"\nDigit Recognised : ";
int maxm=0;
for(k=0;k<10;k++)
maxm = (check[k] > check[maxm]) ? k : maxm ;

cout<<maxm<<endl;
}
getch();
return 0;
}
///////////////////////////////// Function Definations ////////////////////////
///////////////
void testing()
{
test0();
test479();
test5();
test3();
test2();
test68();
}
void test0()
{
int count=0,i,j;
for(i=2;i<=5;i++)
for(j=2;j<=5;j++)
if(b[i][j])
count++;
if(!count)
{
count=0;
for(i=4;i<=7;i++)
for(j=0;j<=3;j++)
if(b[i][j])
count++;
if(!count)
check[7]++;
else
check[0]++;
}
else
check[0]=0;
}
void test479()
{
int count=0,i,j;
for(i=5;i<=7;i++)
for(j=0;j<=4;j++)
if(b[i][j])
count++;
if(!count)
{
count=0;
for(i=2;i<=5;i++)
for(j=0;j<=4;j++)
if(b[i][j])
count++;
if(!count)

check[7]++;
else
{
count=0;
for(i=0;i<=1;i++)
for(j=2;j<=5;j++)
if(b[i][j])
count++;
if(!count)
check[4]++;
else
check[9]++;
}
}
}
void test5()
{
int count=0,j;
for(j=0;j<=4;j++)
if(b[5][j])
count++;
for(j=3;j<=7;j++)
if(b[1][j])
count++;
if(!count)
check[5]++;
}
void test3()
{
int count[4]={0},j;
for(j=0;j<=3;j++)
{
if(b[2][j])
count[0]++;
if(b[1][j])
count[0]++;
if(b[5][j])
count[1]++;
}
for(j=5;j<=7;j++)
{
if(b[1][j])
count[2]++;
if(b[5][j])
count[3]++;
}
if(count[0]>1)
count[0]=0;
else
count[0]=1;
if(count[1])
count[1]=0;
else

count[1]=1;
if(count[0] && count[1] && count[2] && count[3])
check[3]++;
}
void test2()
{
int count=0,j;
for(j=0;j<=3;j++)
if(b[2][j])
count++;
for(j=5;j<=7;j++)
if(b[5][j])
count++;
if(!count)
check[2]++;
}
void test68()
{
int count[4]={0},j,i;
for(j=0;j<=3;j++)
if(b[5][j])
count[0]++;
for(j=5;j<=7;j++)
{
if(b[6][j])
count[1]++;
if(b[1][j])
count[2]++;
}
for(i=3;i<=4;i++)
for(j=3;j<=4;j++)
if(b[i][j])
count[3]++;
if(count[0] && count[1] && count[2])
{
if(count[3])
check[8]++;
}
else if(count[0] && count[1])
check[6]++;
}

You might also like