A C++ program for Histogram.


#include<iostream>
#include<fstream>
#include<conio.h>
#include<string.h>
#include<graphics.h>
using namespace std;
struct pix
{
unsigned char b,g,r;
}pixel;
char Header[54];
inthist[256]={0};
intrmin=30, rmax=200;
ints,smin=0,smax=255;
ifstream in;
ofstream out,out1;
int main()
{
charinfile[]=”f:\\bf.bmp”;
charoutfile[]=”f:\\bf-histogram-linear.bmp”;
charimdata[]=”f:\\imdata.dat”;
int intensity;
intgd=DETECT, gm;
initgraph(&gd,&gm,”C:\\Dev-Cpp\\”);
line(10,10,10,450);
line(10,450, 600,450);
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));
//Image 24-bit to gray scale conversion
intensity=(pixel.r+pixel.g+pixel.b)/3;
pixel.r=pixel.g=pixel.b=intensity;
out1<<“ORIGINAL : ” <<(int)pixel.r<<” , “<<(int)pixel.g<<” , “<<(int)pixel.b<<endl;
for (int k=0;k<255;k++)
if (intensity == k)
hist[k]+= 1;
int s = (smax-smin)/(float)(rmax-rmin) *(intensity-rmin)+smin;
if(s <= rmin)
pixel.r=pixel.g=pixel.b=0;
else
pixel.r=pixel.g=pixel.b=s;
out.write((char *)(&pixel),sizeof(pixel));
out1<<“MODIFIFED : ” <<(int)pixel.r<<” , “<<(int)pixel.g<<” ,
“<<(int)pixel.b<<endl;
}
for(int k=0;k<255;k++)
line(20+k,450,20+k,450-hist[k]/2);
in.close();
out.close();
out1.close();
getch();
}