A C++ program to show image averaging filter


#include<iostream>
#include<fstream>
#include<conio.h>
#include<string.h>
using namespace std;
structpix
{
unsigned char b,g,r;
}pixel;
char Header[54];
ifstream in;
ofstream out,out1;
int main()
{
charinfile[]=”e:\\p\\bf.bmp”;
charoutfile[]=”e:\\p\\bf-filtered.bmp”;
charimdata[]=”e:\\p\\imdata.dat”;
inti,j,k,count=0;
in.open(infile,ios::in|ios::binary);
in.read(( char*)(&Header),sizeof(Header));
int height=*(int*)&Header[18];
int width=*(int*)&Header[22];
int filter[9]={1,1,1,1,1,1,1,1,1};
structpix a[height][width];
structpix a1[height][width];
cout<< “Height = ” << height <<” Width= ” << width;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
a[i][j].r=0; a1[i][j].r=0;
a[i][j].g=0; a1[i][j].g=0;
a[i][j].b=0; a1[i][j].b=0;
}
out1.open(imdata, ios::out);
i=0;j=0;
while(!in.eof())
{
in.read((char *)(&pixel),sizeof(pixel));
if(j==width)
{
j=0;
i++;
}
a[i][j].r=pixel.r;
a[i][j].g=pixel.g;
a[i][j].b=pixel.b;
out1<<“PIXEL “<<count++<<” : “<<(int)a[i][j].r<<” , “<<(int)a[i][j].g<<” ,
“<<(int)a[i][j].b<<endl;
j++;
}
out.open(outfile,ios::out| ios::binary);
out.write(( char*)(&Header),sizeof(Header));
count=0;
for(k=0;k<width;k++)
{
pixel.r=a[0][k].r;
pixel.g=a[0][k].g;
pixel.b=a[0][k].b;
out1<<“PIXEL WRITTEN “<<count++ <<” : “<<(int)pixel.r<<” , “<<(int)pixel.g<<” ,
“<<(int)pixel.b<<endl;
out.write((char*)(&pixel),sizeof(pixel));
}
for(i=1; i<height-1; i++)
{
for(j=1; j<(width-1); j++)
{
a1[i][j].r = (1/9.0)*((int)a[i-1][j-1].r*filter[0]+(int)a[i][j-1].r*filter[1]+(int)a[i+1][j-
1].r*filter[2]+(int)a[i-1][j].r*filter[3]+(int)a[i][j].r*filter[4]+ (int)a[i+1][j].r*filter[5] + (int)a[i-
1][j+1].r*filter[6] + (int)a[i][j+1].r*filter[7] + (int)a[i+1][j+1].r*filter[8]);
a1[i][j].g = (1/9.0)*((int)a[i-1][j-1].g*filter[0]+(int)a[i][j-1].g*filter[1]+(int)a[i+1][j-
1].g*filter[2]+(int)a[i-1][j].g*filter[3]+(int)a[i][j].g*filter[4]+ (int)a[i+1][j].g*filter[5] + (int)a[i-
1][j+1].g*filter[6] + (int)a[i][j+1].g*filter[7] + (int)a[i+1][j+1].g*filter[8]);
a1[i][j].b = (1/9.0)*((int)a[i-1][j-1].b*filter[0]+(int)a[i][j-1].b*filter[1]+(int)a[i+1][j-
1].b*filter[2]+(int)a[i-1][j].b*filter[3]+(int)a[i][j].b*filter[4]+ (int)a[i+1][j].b*filter[5] + (int)a[i-
1][j+1].b*filter[6] + (int)a[i][j+1].b*filter[7] + (int)a[i+1][j+1].b*filter[8]);
out1<<“PIXEL MODIFIED ” <<” : “<<(int)a1[i][j].r<<” , “<<(int)a1[i][j].g<<” ,
“<<(int)a1[i][j].b<<endl;
}
for(k=0;k<width;k++)
{
if(k==0|| k==width-1)
{
pixel.r=a[i][k].r;
pixel.g=a[i][k].g;
pixel.b=a[i][k].b;
}
else {
pixel.r=a1[i][k].r;
pixel.g=a1[i][k].g;
pixel.b=a1[i][k].b;
}
out1<<“PIXEL WRITTEN “<<count++ <<” : “<<(int)pixel.r<<” , “<<(int)pixel.g<<” ,
“<<(int)pixel.b<<endl;
out.write((char*)(&pixel),sizeof(pixel));
}
}
for(k=0;k<width;k++)
{
pixel.r=a[height-1][k].r;
pixel.g=a[height-1][k].g;
pixel.b=a[height-1][k].b;
out1<<“PIXEL WRITTEN “<<count++ <<” : “<<(int)pixel.r<<” , “<<(int)pixel.g<<” ,
“<<(int)pixel.b<<endl;
out.write((char*)(&pixel),sizeof(pixel));
}
in.close();
out.close();
out1.close();
getch(); }