A C++ program to show image contrast stretching.


#include<iostream>
#include<fstream>
#include<conio.h>
#include<string.h>
using namespace std;
struct pix
{
unsigned char b,g,r;
}pixel;
intut=150, lt=80; // upper and lower threshold values
char Header[54];
ifstream in;
ofstream out,out1;
int main()
{
charinfile[]=”e:\\bf-gray.bmp”;
charoutfile[]=”e:\\bf-contraststreching.bmp”;
charimdata[]=”e:\\imdata.dat”;
in.open(infile,ios::in|ios::binary);
in.read(( char*)(&Header),sizeof(Header));
out.open(outfile,ios::out| ios::binary);
out.write(( char*)(&Header),sizeof(Header));
out1.open(imdata, ios::out);
while(!in.eof())
{
in.read((char *)(&pixel),sizeof(pixel));
out1<<“ORIGINAL : ” <<(int)pixel.r<<” , “<<(int)pixel.g<<” ,
“<<(int)pixel.b<<endl;
int intensity= (pixel.r+pixel.g+pixel.b)/3; //gray scale
//Image contrast streching
if (intensity <= lt)
pixel.r=pixel.g=pixel.b = 0.5*intensity;
else if( intensity <=ut)
pixel.r=pixel.g=pixel.b = 2*(intensity-lt)+0.5*lt;
else
pixel.r=pixel.g=pixel.b = 0.5*(intensity-ut)+0.5*lt + 2*(ut-lt);
out.write((char *)(&pixel),sizeof(pixel));
out1<<“MODIFIFED (Contrast Streching) : ” <<(int)pixel.r<<” ,
“<<(int)pixel.g<<” , “<<(int)pixel.b<<endl;
}
in.close();
out.close();
getch();
}