Thursday, October 25, 2012

C code for Grey Level Transformation


/*Gray Level Transformation*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
#include<math.h>


typedef struct bmp_header {
unsigned char ft[2];
unsigned long fsz;
unsigned int res1, res2;
unsigned long ofb, sz, wd, ht;
unsigned int npi, bpp;
unsigned long cps, isz, xres, yres;
unsigned long clru, clrimp;
 }TH;
TH *ihdr;
void rgb2gray();
void inverse();
void power();
void contrast_stretching();
void result(char []);

int length , width, temp;
unsigned char ch, pixel[200][200];
int i,j,k;
char file_name[14];

void main()
{
int n,m;
long int i,j;
FILE *imgf;
clrscr();
printf("\n\t GRAY LEVEL TRANSFORMATION\n");
printf("\n Enter the input file name---->");
scanf("%s", file_name);
fflush(stdin);
imgf= fopen(file_name, "rb");
if(imgf== NULL)
{
printf("\n \a error");
exit(0);
}
fread(ihdr, sizeof(TH), 1, imgf);
length= ihdr->ht;
width= ihdr->wd;
for(i=0; i<length; i++)
for(j=0; j<width; j++)
{
temp=0;
for(k=0; k<3; k++)
{
ch=fgetc(imgf);
temp=temp+ch;
}
temp=temp/3;
pixel[i][j]=temp;
}
fclose(imgf);
rgb2gray();
result("gray1.bmp");
inverse();
result("contrast.bmp");
inverse();
power();
contrast_stretching();
result("inverse.bmp");
printf("\n\nTo observe gray scale image open GRAY1.BMP in c:\tc\bin\ ");
printf("\n\n To observe inverse image open INVERSE.bmp in ");
printf("\n\n To observe power transformed image open POWER.bmp in  ");
printf("\n\n To observe contarst stretched image open CONTRAST.bmp in c:\tc\bin");
getch();
}

  void rgb2gray()
{
FILE *imgf1;
imgf1=fopen("gray1.bmp"," wb");
fwrite(ihdr, sizeof(TH), 1, imgf1);
for(i=0; i< length; i++)
for(j=0; j<width; j++)
{
for(k=0; k<3;k++)
{
fputc(pixel[i][j], imgf1);
}
}

fclose(imgf1);

}

void inverse()
{
for(i=0; i<length; i++)
for(j=0; j<width; j++)
{
pixel[i][j]=255-pixel[i][j];
}
}

void result(char file_name[14])
{
FILE *imgf1;
imgf1=fopen(file_name, "wb");
fwrite(ihdr, sizeof(TH),1, imgf1);
for(i=0; i<length; i++)
for(j=0; j<width; j++)
{
for(k=0; k<3; k++)
{
fputc((int)pixel[i][j], imgf1);
}
}
fclose(imgf1);
}

  void power()
{
double c, t;
double val;
printf("\n\n POWER LAW TRANSFORMATION\n");
printf("\n Enter the power factor----->");
scanf("%lf", &val);
c=((double)255.0)/pow((double)255.0, val);
printf("\n The constant value is %lf", c);
for(i=0; i<(ihdr->ht); i++)
for(j=0; j<(ihdr->wd); j++)
{
t=pow((double)pixel[i][j], val);
pixel[i][j] =(int) (t* c);
}
result("power.bmp");
//restoring the pixel values
val=(1.0/val);
c=((double)255.0)/ pow((double)255.0, val);
for(i=0;  i<(ihdr->ht);i++)
for(j=0; j<(ihdr->wd); j++)
{
t=pow((double)pixel[i][j], val);
pixel[i][j]=(int)(t*c);
}
}

void contrast_stretching()
{
int r1, r2, s1, s2, i, j;
float m1,m2,m3;
printf("\n\n\t CONTRAST STRETCHING\n");
printf("\n NOTE: r1<r2<255 and s1<s2<255\n");
printf("\n Enter (r1, s1) -");
scanf("%d, %d", &r1, &s1 );
printf("\n Enter (r2, s2) -");
scanf("%d,%d", &r2 , &s2);
m1=(float)s1/ (float)r1;
m2=(float)(s2-s1)/(float)(r2-r1);
m3=(float)(255-s2)/ (float)(255-r2);
for(i=0; i<(ihdr->ht); i++)
for(j=0; j<(ihdr->wd);j++)
{
if(pixel[i][j]< r1)
pixel[i][j]=(int)(((float)(pixel[i][j]))*m1);
else
{
if(pixel[i][j]> r1 && pixel[i][j]<r2)
pixel[i][j]=(int)(((float)(pixel[i][j]-r1))*m1)+s1;
else
pixel[i][j]=(int)(((float)(pixel[i][j]-r2))*m1)+s2;
}
}
}

No comments:

Post a Comment