You are on page 1of 17

Contrast stretching (often called normalization) is a simple image

enhancement technique that attempts to improve the contrast in an


image by `stretching' the range of intensity values it contains to
span a desired range of values, e.g. the full range of pixel
values that the image type concerned allows. It differs from the
more sophisticated histogram equalization in that it can only apply
a linear scaling function to the image pixel values. As a result the
`enhancement' is less harsh.

Transformation function is shown in the graph. Locations of the points (r1, s1) and
(r2, s2) used to control the shape of the transformation function. If r1=s1 and r2=s2
transformation is linear function and produces no changes in the gray level. If
r1=r2, s1=0 and s2=L-1, transformation becomes a thresholding function that
results a binary image. Intermediate values of (r1, s1) and (r2, s2) produce various
degrees of spread in the gray levels of the output image, thus affecting its contrast.
Most implementations accept a graylevel image as input and
produce another graylevel image as output.
1- Contrast stretching.

1. Open image file.


2. Extract the image matrix from the opened image file.
3. Accept parameters (R1, S1) and (R2, S2) from user.
4. Find slope of line joining (0, 0) and (R1, S1) let it be M1.
5. Find slope of line joining (R1, S1) and (R2, S2) let it be M2.
6. Find slope of line joining (R2, S2) and (255,255) let it be M3.
7. For all pixels having Gray value less than R1, modify their gray value
as below
Pixel = Pixel*M1,
8. For all pixels having Gray value between R1 & R2, modify their gray
value as below
Pixel = (Pixel - R1) * M2 + S1,
9. For all pixels having Gray value greater than R2, modify their gray
value as below
Pixel = (Pixel – R2) * M3 + S2.
10.Display the image.
11.Create new image file for storing resultant image.

#include <dos.h>

#include <graphics.h>

#include <conio.h>

#include <stdlib.h>

#include <stdio.h>

#include <math.h>

#define WIDTH_OFF 18

#define HEIGHT_OFF 22

#define PIXEL_OFF 1078

unsigned char imghead[PIXEL_OFF]; //Image header including color palette

unsigned char huge pixel[640][480]; //Image pixel data

int width,height;

void printscreen(void);

void dispimage(char pos); //pos indicates left or right of screen


void init_graphics(void); //initializes graphics system

void get_pixelmat(FILE *fp); //Extracts pixel data from image file

void contrast_stretching(void);

void bitplane(void);

void power_log(void);

void filecreate(void); //Creates file with help of 'pixel'matrix

//and uses imghead as image header+colorpal

void main()

FILE *fp;

char filename[15];

int i,j,opt,r1,r2,s1,s2,opt1;

init_graphics();

printf("Enter source image file : ");

gets(filename); //Getting desired image file from user

fp=fopen(filename,"rb"); //Source BMP File is Opened in read binary mode

if(fp==NULL) //On failure in file open, program terminates.

printf("Cannot open source file \n");

getch();

exit(0);
}

get_pixelmat(fp); //Getting pixel matrix,header and palette from image

printf("\nEnter desired operation on image");

printf("\n1 : For Image negative \n2 : For power transform");

printf("\n3 : For contrast stretching ");

printf("\n4 : For Gray level slicing\n5 : Bit plane\n6 : To exit : ");

scanf("%d",&opt);

switch(opt)

case 1:

clrscr();

cleardevice();

dispimage(0);

for(i=0;i<height;i++) //image negative operation

for(j=0;j<width;j++)

pixel[i][j]=255-pixel[i][j];

dispimage(1);

break;

case 2:

clrscr();

cleardevice();
dispimage(0);

power_log();

dispimage(1);

break;

case 3:

clrscr();

cleardevice();

dispimage(0);

contrast_stretching();

dispimage(1);

break;

case 4:

clrscr();

cleardevice();

printf("\n\n\t1 : Displaying high value in region of interest(binary)");

printf("\n\t2 : Preserving original levels other than interest : ");

scanf("%d",&opt1);

printf("\n\nEnter the range of interest : ");

scanf("%d%d",&r1,&r2);

printf("\n\nEnter the high output level : ");

scanf("%d",&s1);

switch(opt1)

{ case 1:
printf("\n\nEnter the low output level : ");

scanf("%d",&s2);

clrscr();

cleardevice();

dispimage(0);

for(i=0;i<height;i++)

for(j=0;j<width;j++)

if(pixel[i][j]<=r1 || pixel[i][j]>=r2)

pixel[i][j]=s2;

else

pixel[i][j]=s1;

dispimage(1);

break;

case 2:

clrscr();

cleardevice();

dispimage(0);

for(i=0;i<height;i++)

for(j=0;j<width;j++)
{

if(pixel[i][j]>=r1 && pixel[i][j]<=r2 )

pixel[i][j]=s1;

dispimage(1);

break;

break;

case 5:

clrscr();

cleardevice();

dispimage(0);

bitplane();

dispimage(1);

break;

case 6:

exit(0);

break;

default:

printf("\nCase error : ");

getch();

exit(0);

};
getch();

clrscr();

cleardevice();

filecreate();

getch();

fcloseall(); //closing all open streams before exiting

void dispimage(char pos)

int i,j;

for(i=0;i<height;i++)

for(j=0;j<width;j++)

if(pos==0)

putpixel(j+0,480-i,(pixel[i][j])/16); //Displaying image in 16 colors

else

putpixel(j+320,480-i,(pixel[i][j])/16);

void init_graphics()
{

int i;

int gd=DETECT,gm,errorcode; //Default graphics mode (640*480) 16 colors.

struct palettetype pal; //Palette structure for declaring colors

initgraph(&gd,&gm,"c:\\tc\\"); //initialize graphics mode

errorcode = graphresult();

if (errorcode != grOk)

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

getpalette(&pal); //Getting currrent palette properties

for(i=0;i<pal.size;i++)

setrgbpalette(pal.colors[i],i*4,i*4,i*4);

//(0=Black && 15=White)

settextstyle(1,0,1); //Setting appropriate text style //Setting up RGB PALETTE

//to 16 Grey levels

void get_pixelmat(FILE *fp)


{

int i,j,k;

unsigned long img_width,img_height;

fseek(fp,0,SEEK_SET); // Setting file pointer to file start

fread(&imghead,PIXEL_OFF,1,fp); // Reading header+palette information

fseek(fp,WIDTH_OFF,SEEK_SET); // Setting file pointer to point'width'field

fread(&img_width,4,1,fp); // Getting width

fread(&img_height,4,1,fp); // Getting height

fseek(fp,PIXEL_OFF,SEEK_SET); // Setting file pointer to pixel data area

width=(int)img_width;

height=(int)img_height;

for(i=0;i<height;i++)

for(j=0;j<width;j++)

pixel[i][j]=fgetc(fp);

if((width%4)!=0) //Ensuring that dword padding bytes at end of scan

{ //line are not (displayed/captured).

for(k=0;k<(4-(width%4));k++)

fgetc(fp);

}
}

void contrast_stretching()

int r1,r2,s1,s2,i,j;

float m1,m2,m3;

printf("\nNote: r1<r2<255 and s1<s2<255 ");

printf("\nEnter (r1,s1) (e.g. 100 60 ) : ");

scanf("%d %d",&r1,&s1);

printf("Enter (r2,s2) (e.g. 160 240) : ");

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<height;i++)

for(j=0;j<width;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) * m2) + s1;

else

pixel[i][j]=(int)( (float)(pixel[i][j]-r2) * m3) + s2;


}

void power_log()

int i,j;

double c,t,val;

printf("Enter the power factor (E.g (0<pf<1) lighter (pf>1) darker : ");

scanf("%lf",&val);

c=((double)255.0)/pow((double)255.0,val);

for(i=0;i<height;i++) //image negative operation

for(j=0;j<width;j++)

t=pow((double)pixel[i][j],val);

pixel[i][j]=(unsigned char)(c*t);

void bitplane()

{
int i,j,

bit;

char mask;

printf("Enter the bit no (0-7) : ");

scanf("%d",&bit);

mask=pow(2,(bit));

for(i=0;i<height;i++) //image negative operation

for(j=0;j<width;j++)

if(pixel[i][j]&mask)

pixel[i][j]=mask;

else

pixel[i][j]=0;

void filecreate()

int i,j,k;

FILE *fp;

unsigned char filename[15];


printf("Enter the destination filename :");

fflush(stdin);

gets(filename);

fp=fopen(filename,"wb"); //Source BMP File is Opened in read binary mode

if(fp==NULL) //On failure in file open, program terminates.

printf("Cannot create destination file \n");

fcloseall();

getch();

exit(0);

fseek(fp,0,SEEK_SET);

fwrite(&imghead,PIXEL_OFF,1,fp);

for(i=0;i<height;i++)

for(j=0;j<width;j++)

fputc(pixel[i][j],fp);

if((width%4)!=0) //Ensuring that each scan line ends on dword bndry.

{
for(k=0;k<(4-(width%4));k++)

fputc(0XFF,fp); //Adding padding data to scan line

fclose(fp);

printf("File '%s' created sucessfully ",filename);

}4- Contrast Stretching

Low
contrast Image

Higher Contrast
Image (r1=100, s1=60; r2=180,s2=240)

You might also like