FE
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#define pi 3.1415
void setpixel(int x1,int y1);
void empty();
void m_myMap();
void m_mapping();
double m_fd();
int m_feature();
void m_centroid();
struct show
{
int myx;
int myy;
}s[300];
int sumx=0,sumy=0,map=0,xavg=0,yavg=0;
double theta,temp;
int direction=0,ix,iy,count=0,count1=0;
int i,j,map2=0,flag=0;;
double re2;
int x,y,xl,xh,yh,yl;
int t2,t3,t4,t5;
FILE *fp;
int img[120][120],store[120][120];
void main()
{
int gd = DETECT, gm = VGAHI;
initgraph(&gd,&gm,"c:\tc\bgi");
// Open the image
fp =fopen("c:\\rec6.bmp","r");
fseek(fp,1078,0L);
outtextxy(100,80,"ORGINAL");
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
img[j][i]=(int)fgetc(fp);
putpixel(j+100,i+100,img[j][i]);
}
}
// Find the Seed Point
for(i=0;i<100 && !flag; i++)
{
for(j=0;j<100 && !flag; j++)
{
if(img[j][i]==255)
{
ix=i;iy=j;
flag=1;
}
}
}
x=iy+1;
y=ix;
//putpixel(iy+300,ix+300,img[iy][ix]);
//printf(" seed %d %d" ,iy,ix);
//Chain coding For Boundary Extraction
do
{
switch(direction)
{
case 0:
{
if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
else if(img[x][y-1]==0)
{y=y-1; direction= 6;}
else if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction= 7;}
else if(img[x+1][y]==0)
{x=x+1; direction= 0;}
else if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction= 1;}
else if(img[x][y+1]==0)
{y=y+1; direction= 2;}
else if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
else if(img[x-1][y]==0)
{x=x-1; direction= 4;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
case 1:
{
if(img[x][y-1]==0)
{y=y-1; direction= 6;}
else if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction= 7;}
else if(img[x+1][y]==0)
{x=x+1; direction= 0;}
else if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction= 1;}
else if(img[x][y+1]==0)
{y=y+1; direction= 2;}
else if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
else if(img[x-1][y]==0)
{x=x-1; direction= 4;}
else if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
case 2:
{
if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction= 7;}
else if(img[x+1][y]==0)
{x=x+1; direction= 0;}
else if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction= 1;}
else if(img[x][y+1]==0)
{y=y+1; direction= 2;}
else if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
else if(img[x-1][y]==0)
{x=x-1; direction= 4;}
else if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
else if(img[x][y-1]==0)
{y=y-1; direction= 6;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
case 3:
{
if(img[x+1][y]==0)
{x=x+1; direction= 0;}
else if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction=1;}
else if(img[x][y+1]==0)
{y=y+1; direction= 2;}
else if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
else if(img[x-1][y]==0)
{x=x-1; direction= 4;}
else if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
else if(img[x][y-1]==0)
{y=y-1; direction= 6;}
else if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction= 7;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
case 4:
{
if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction= 1;}
else if(img[x][y+1]==0)
{y=y+1; direction= 2;}
else if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
else if(img[x-1][y]==0)
{x=x-1; direction= 4;}
else if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
else if(img[x][y-1]==0)
{y=y-1; direction= 6;}
else if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction= 7;}
else if(img[x+1][y]==0)
{x=x+1; direction= 0;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
case 5:
{
if(img[x][y+1]==0)
{y=y+1; direction= 2;}
else if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
else if(img[x-1][y]==0)
{x=x-1; direction= 4;}
else if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
else if(img[x][y-1]==0)
{y=y-1; direction= 6;}
else if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction= 7;}
else if(img[x+1][y]==0)
{x=x+1; direction= 0;}
else if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction= 1;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
case 6:
{
if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
else if(img[x-1][y]==0)
{x=x-1; direction= 4;}
else if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
else if(img[x][y-1]==0)
{y=y-1; direction= 6;}
else if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction =7;}
else if(img[x+1][y]==0)
{x=x+1; direction= 0;}
else if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction= 1;}
else if(img[x][y+1]==0)
{y=y+1; direction= 2;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
case 7:
{
if(img[x-1][y]==0)
{x=x-1; direction= 4;}
else if(img[x-1][y-1]==0)
{x=x-1;y=y-1; direction=5;}
else if(img[x][y-1]==0)
{y=y-1; direction= 6;}
else if(img[x+1][y-1]==0)
{x=x+1;y=y-1; direction= 7;}
else if(img[x+1][y]==0)
{x=x+1; direction= 0;}
else if(img[x+1][y+1]==0)
{x=x+1; y=y+1; direction= 1;}
else if(img[x][y+1]==0)
{y=y+1; direction= 2;}
else if(img[x-1][y+1]==0)
{x=x-1; y=y+1;direction= 3;}
setpixel(x,y);
s[count].myx=x;
s[count].myy=y;
count++;
break;
}
}// switch
}// while
// while((x!=87)||(y!=40));
while(((x!=iy+1)||(y!=ix-1)));
empty();
m_mapping();
//m_myMap();
m_centroid();
m_feature();
m_fd();
fclose(fp);
getch();
}
// Assign array with 0
void empty()
{
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
store[i][j]=0;
}
}
}
//mapping of structure value to a two dim array
void m_mapping()
{
for(i=0;i<300;i++)
{
store[s[i].myx][s[i].myy]=2;
}
}
// Dispaly that store image
void m_myMap()
{
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
if(store[j][i]==2)
putpixel(j+300,i+190,RED);
}
}
}
// Dispaly the boundary
void setpixel(int x1,int y1)
{
outtextxy(300,80,"Image Boundary");
// printf(" %d ",x1);
putpixel(x1+300,y1+100,RED);
delay(10);
map++;
sumx+=x1;
sumy+=y1;
}
// Find the centroid
void m_centroid()
{
xavg=sumx/count;
yavg=sumy/count;
printf("\nCentroid Point (%d,%d) ",xavg,yavg);
putpixel(xavg+300,yavg+100,15);
}
// Finding Angle
int m_feature()
{
double asum=0,bsum=0,csum=0,y,a,b,c,ang,alpha,dia;
for(i=0;i<=100;i++)
for(j=0;j<100;j++)
{
if(store[j][i]==2)
{
asum=asum+(i-xavg)*(i-xavg);
bsum=asum+(i-xavg)*(j-yavg);
csum=csum+(j-yavg)*(j-yavg);
}
}
a=asum;
b=bsum;
c=csum;
temp=a-b;
ang=b/temp;
alpha= (0.5*atan(ang));
dia = 2.0*(a-c)*cos(2.0*alpha)+2.0*b*sin(2.0*alpha);
if(dia<0.0)
theta=alpha+(3.1428/2.0);
else
theta=alpha;
printf("\nTheta %f ",theta);
return(theta);
}
//Axis of least inertia
double m_fd()
{
double x,y,temp;
int sign;
for(i=0;i<=200;i++)
{
x= (yavg-s[i].myy+s[i].myx*tan(pi/2+3)-
xavg*tan(3))/(tan(pi/2+3));
y= ( yavg+(x-xavg)*tan(3));
sign=((i*x-xavg)>0)? 1:-1;
putpixel(x+300,y+100,GREEN);
// printf("\n x=%f y= %f" ,x,y);
// printf(" Sign %d",sign);
// getch();
}
temp=(sign*sqrt((xavg-x)*(xavg-x)+(yavg-y)*(yavg-y)));
printf("\nLeast Inertia=%f",temp);
return temp;
}