Thursday, October 25, 2012

C code for determining the statistical Properties of Image


/*Statistical Properties of Image*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>



struct fheader{
int filetype;
long filesize;
int res1, res2;
long offset;
}fh;

struct infoheader{
long bioffset, width, height;
int planes, bitcnt;
long com_type, img_size, xres, yres, clr, clrimg;
}ih;

float mean();
float std_dev(float);
float energy();
void print_attr();

FILE *fp;
char fname[20];

void main()
{
int i,j;
float m,sigma, ms,e;
clrscr();

printf("\n Enter the file name(.bmp):");
scanf("%s", fname);
fp=fopen(fname, "rb");
if(fp==NULL)
{
printf("\nError in opening in file");
getch();
}

fread(&fh, sizeof(fh), 1, fp);
fread(&ih, sizeof(ih),1 ,fp);
print_attr();

printf("\n\n The statistical properties are: ");
m=mean();
printf("\n\n Mean\t%f: ",m);
e=energy();
printf("\n\n Energy:\t%f ",e);
sigma=std_dev(m);
printf("\n\n The standard deviation:\t %f", sigma);

ms=e/(float)(ih.width * ih.height);
sigma=sqrt(ms - m*m);
printf("\n Variance from other method:\t %f",sigma);
fclose(fp);
getch();
}

void print_attr()
{
printf("\n\nThe file attribute are:  ");
printf("\nFile Type:%d",fh.filetype);
printf("\nFile Size:%d", fh.filesize);
printf("\nSize of offset:%ld", fh.offset);
printf("\nheader size:%ld", ih.bioffset);
printf("\nNo of rows:%ld", ih.height);
printf("\nNo of columns:%ld", ih.width);
printf("\nNO of planes:%d", ih.planes);
printf("\nNo of bits/pixel:%d", ih.bitcnt);
printf("\nCommpression Type:%ld",ih.com_type);
printf("\nHorizontal resolution:%ld", ih.xres);
printf("\nVertical Resolution:%ld",ih.yres);
printf("\nNo of colors used:%ld", ih.clr);
printf("\nNo of important color used:%ld", ih.clrimg);
}

float mean()
{
int x,y;
int ch;
float mean=0;
fseek(fp, fh.offset,SEEK_SET);
for(x=0; x<ih.width; x++)
for(y=0;y<ih.height;y++)
{
ch=fgetc(fp);
mean+=ch;
}
mean/=(float)(ih.width*ih.height);
fclose(fp);
return(mean);
}

float std_dev(float m)
{
int x,y,ch;
float stddev=0;
fp=fopen(fname, "rb");
fseek(fp, fh.offset, SEEK_SET);
for(x=0;x<ih.height; x++)
for(y=0; y<ih.width;y++)
{
ch=fgetc(fp);
stddev +=(float)((ch-m)*(ch-m));
}
stddev /=(float)(ih.width * ih.height);
stddev=sqrt(stddev);
fclose(fp);
return (stddev);
}

float energy()
{
int x,y,ch;
float energy=0;
fp=fopen(fname, "rb");
fseek(fp, fh.offset, SEEK_SET);
for(x=0;x<ih.height; x++)
for(y=0; y<ih.width;y++)
{
ch=fgetc(fp);
energy += (float)ch * (float)ch;
}
return(energy);
}



INPUT



OUTPUT:

The file attribute are:
File Type:19778
File Size:30078
Size of offset:1078
header size:40
No of rows:145
No of columns:200
NO of planes:1
No of bits/pixel:8
Commpression Type:0
Horizontal resolution:2834
Vertical Resolution:2834
No of colors used:0
No of important color used:0

 The statistical properties are:
 Mean   49.898621:
 Energy:        170469184.000000
 The standard deviation:         58.208115
 Variance from other method:     58.209751


No comments:

Post a Comment