A C++ program for image Sobelfilter.


#include<iostream>
#include<fstream>
#include<conio.h>
#include<string.h>
using namespace std;
struct pix
{
unsigned char b,g,r;
}pixel;
char Header[54];
ifstream in;
ofstream out,out1;
int main()
{
charinfile[]=”e:\\bf.bmp”;
charoutfile[]=”e:\\bf-filtered-sobel.bmp”;
charimdata[]=”e:\\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 filter1[9]={-1,-2,-1,0,0,0,1,2,1};
int filter2[9]={-1,0,1,-2,0,2,-1,0,1};
struct pix a[height][width];
struct pix a1[height][width];
struct pix a2[height][width];
struct pix a3[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;
// cout<<“PIXEL : “<<(int)a[i][j].r<<” , “<<(int)a[i][j].g<<” , “<<(int)a[i][j].b<<endl;
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;
// to write first line of original image to modified image file (border1)
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++)
{
//cout<< (int)a[i-1][j-1].r<<” , ” << filter[0]<<endl;
a1[i][j].r = ((int)a[i-1][j-1].r*filter1[0]+(int)a[i][j-1].r*filter1[1]+(int)a[i+1][j-
1].r*filter1[2]+(int)a[i-1][j].r*filter1[3]+(int)a[i][j].r*filter1[4]+ (int)a[i+1][j].r*filter1[5] +
(int)a[i-1][j+1].r*filter1[6] + (int)a[i][j+1].r*filter1[7] + (int)a[i+1][j+1].r*filter1[8]);
// a1[i][j].g = ((int)a[i-1][j-1].g*filter1[0]+(int)a[i][j-1].g*filter1[1]+(int)a[i+1][j-
1].g*filter1[2]+(int)a[i-1][j].g*filter1[3]+(int)a[i][j].g*filter1[4]+ (int)a[i+1][j].g*filter1[5] +
(int)a[i-1][j+1].g*filter1[6] + (int)a[i][j+1].g*filter1[7] + (int)a[i+1][j+1].g*filter1[8]);
// a1[i][j].b = ((int)a[i-1][j-1].b*filter1[0]+(int)a[i][j-1].b*filter1[1]+(int)a[i+1][j-
1].b*filter1[2]+(int)a[i-1][j].b*filter1[3]+(int)a[i][j].b*filter1[4]+ (int)a[i+1][j].b*filter1[5] +
(int)a[i-1][j+1].b*filter1[6] + (int)a[i][j+1].b*filter1[7] + (int)a[i+1][j+1].b*filter1[8]);
a1[i][j].g=a[i][j].g;
a1[i][j].b= a[i][j].b;
a2[i][j].r = ((int)a[i-1][j-1].r*filter2[0]+(int)a[i][j-1].r*filter2[1]+(int)a[i+1][j-
1].r*filter2[2]+(int)a[i-1][j].r*filter2[3]+(int)a[i][j].r*filter2[4]+ (int)a[i+1][j].r*filter2[5] +
(int)a[i-1][j+1].r*filter2[6] + (int)a[i][j+1].r*filter2[7] + (int)a[i+1][j+1].r*filter2[8]);
// a2[i][j].g = ((int)a[i-1][j-1].g*filter2[0]+(int)a[i][j-1].g*filter2[1]+(int)a[i+1][j-
1].g*filter2[2]+(int)a[i-1][j].g*filter2[3]+(int)a[i][j].g*filter2[4]+ (int)a[i+1][j].g*filter2[5] +
(int)a[i-1][j+1].g*filter2[6] + (int)a[i][j+1].g*filter2[7] + (int)a[i+1][j+1].g*filter2[8]);
// a2[i][j].b = ((int)a[i-1][j-1].b*filter2[0]+(int)a[i][j-1].b*filter2[1]+(int)a[i+1][j-
1].b*filter2[2]+(int)a[i-1][j].b*filter2[3]+(int)a[i][j].b*filter2[4]+ (int)a[i+1][j].b*filter2[5] +
(int)a[i-1][j+1].b*filter2[6] + (int)a[i][j+1].b*filter2[7] + (int)a[i+1][j+1].b*filter2[8]);
a2[i][j].g=a[i][j].g;
a2[i][j].b= a[i][j].b;
out1<<“PIXEL MODIFIED ” <<” : “<<(int)a1[i][j].r<<” , “<<(int)a1[i][j].g<<” ,
“<<(int)a1[i][j].b<<endl;
a3[i][j].r=a1[i][j].r + a2[i][j].r;
a3[i][j].g=a1[i][j].g + a2[i][j].g;
a3[i][j].b=a1[i][j].b + a2[i][j].b;
}
for(k=0;k<width;k++)
{
// To write left and write border
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 //to write middle modified contents
{
pixel.r=a3[i][k].r;
pixel.g=a3[i][k].g;
pixel.b=a3[i][k].b;
}
out1<<“PIXEL WRITTEN “<<count++ <<” : “<<(int)pixel.r<<” , “<<(int)pixel.g<<” ,
“<<(int)pixel.b<<endl;
out.write((char*)(&pixel),sizeof(pixel));
}}
// to write bottom border
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();
}