/*************************************************************************************
=>THE FOLLOWING PROGRAM CREATES AN IMAGE OF 128 X 64 RESOLUTION PNG IMAGE AND
=>THEN CREATES A TWO DIMENSIONAL PIXEL ARRAY BY READING FROM THE IMAGE FILE.
=>IT THEN APPLIES A LOW PASS FILTER USING A 3 X 3 KERNEL.
=>FINALLY IT APPLIES NOISE TO THE FILTERED IMAGE USING RANDOM NUMBERS
In ImageFilter.h header file, there are functions to create pixel array and filter image
using a low pass filter.
FUNCTION NAMES: void CreatePixelArray();
void LowPassFilter(int kernel[K][K];
*************************************************************************************/
#include "ImageFilter.h"
/*int kernel[K][K]={ {1, 1, 1},
{1, 1, 1},
{1, 1, 1}
};*/
int kernel[K][K]={ {1, 4, 1},
{4, 8, 4},
{1, 4, 1}
};
int main(){
int num=0;
/*IMAGE CREATION*/
CreateImage();
/*READING FROM IMAGE FILE AND CREATION OF ORIGINAL PIXEL ARRAY*/
CreatePixelArray();
/*LOW PASS FILTER USING 3 X 3 KERNEL (SPATIAL OPERATION)*/
LowPassFilter(kernel);
/*APPLYING NOISE TO EACH PIXEL RANDOMLY USING GENERATED RANDOM NUMBERS WITHIN 1 TO 10*/
/*AddNoise(num);*/
AddNoise();
return 0;
}
/*HEADER FILE FOR LOW PASS FILTER OPERATION USING 3 X 3 KERNEL AND CREATE PIXEL ARRAY*/
#include
#include
#include
#include
#define HEADER "P2\r\n128 64\r\n255\r\n"
#define pi 3.1416
#define M 128
#define N 64
#define MAX 10
#define K 3
#define MAX1 10
#define MAX2 100
int pixel_array[N][M],sum=0;
char char_array[MAX];
char ch[]=HEADER;
int array[K][K];
int unique_rand_int[MAX1];
/*CREATE IMAGE*/
void CreateImage()
{
int num=0;
double f=0.00;
int m=0,n=0;
char str[6];
FILE *S_STREAM;
S_STREAM=fopen("out2.pgm", "w");
fprintf(S_STREAM,"%s",ch);
for(n=0;n
for(m=0;m
f=(float)((127.0*sin(2*pi*m/128.0)*sin(pi*n/64.0))+127.0);
sprintf(str,"%.0f",f);
num=atoi(str);
fprintf(S_STREAM,"%d",num);
fprintf(S_STREAM," ");
}
fprintf(S_STREAM,"\n");
}
fclose(S_STREAM);
}
/*CREATE PIXEL ARRAY*/
void CreatePixelArray()
{
FILE *R_STREAM;
char c='a';
int newline=0,x=0;
int flag=0,num=0,i=0,j=0;
R_STREAM=fopen("out2.pgm","a+");
while(!feof(R_STREAM)){
c=fgetc(R_STREAM);
if(c=='\n')
{
newline++;
if(newline>3)
{
j++;
i=0;
}
}
if( (newline==3) && (flag==0) )
{
flag=1;
}
if(flag==1)
{
if(isdigit(c))
{
char_array[x]=c;
x++;
}
if(c==' ')
{
char_array[x]='\0';
num=atoi(char_array);
pixel_array[j][i]=num;
i++;
for(x=0;x
char_array[x]='\0';
}
x=0;
}
}
}
fclose(R_STREAM);
}
/*APPLYING LOW PASS FILTER USING 3 X 3 KERNEL*/
void LowPassFilter( int kernel[K][K])
{
int i=0, j=0, m=0, n=0,pixel=0;
FILE *fp;
/*CREATING NEW FILE FOR FILTERED IMAGE*/
fp=fopen("filter.pgm","w");
fprintf(fp,"%s",ch);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+kernel[i][j];
}
}
for(n=0;n
for(m=0;m
if( ((n-1)<0) && ((m-1)<0) ) /*UPPER LEFT CORNER*/
{
array[0][0]=0;
array[0][1]=0;
array[0][2]=0;
array[1][0]=0;
array[1][1]=pixel_array[n][m];
array[1][2]=pixel_array[n][m+1];
array[2][0]=0;
array[2][1]=pixel_array[n+1][m];
array[2][2]=pixel_array[n+1][m+1];
}
if( ((n-1)<0)>128) ) /*UPPER RIGHT CORNER*/
{
array[0][0]=0;
array[0][1]=0;
array[0][2]=0;
array[1][0]=pixel_array[n][m-1];
array[1][1]=pixel_array[n][m];
array[1][2]=0;
array[2][0]=pixel_array[n+1][m-1];
array[2][1]=pixel_array[n+1][m];
array[2][2]=0;
}
if( ((n+1)>63) && ((m-1)<0) ) /*LOWER LEFT CORNER*/
{
array[0][0]=0;
array[0][1]=pixel_array[n-1][m];
array[0][2]=pixel_array[n-1][m+1];
array[1][0]=0;
array[1][1]=pixel_array[n][m];
array[1][2]=pixel_array[n][m+1];
array[2][0]=0;
array[2][1]=0;
array[2][2]=0;
}
if( ((n+1)>63) && ((m-1)<0) ) /*LOWER RIGHT CORNER*/
{
array[0][0]=pixel_array[n-1][m-1];
array[0][1]=pixel_array[n-1][m];
array[0][2]=0;
array[1][0]=pixel_array[n][m-1];
array[1][1]=pixel_array[n][m];
array[1][2]=0;
array[2][0]=0;
array[2][1]=0;
array[2][2]=0;
}
if( (n==0) && ((m-1)>=0) && ((m+1)<=64) ) /*UPPER BORDER*/
{
array[0][0]=0;
array[0][1]=0;
array[0][2]=0;
array[1][0]=pixel_array[n][m-1];
array[1][1]=pixel_array[n][m];
array[1][2]=pixel_array[n][m+1];
array[2][0]=pixel_array[n+1][m-1];
array[2][1]=pixel_array[n+1][m];
array[2][2]=pixel_array[n+1][m+1];
}
if( (n==63) && ((m-1)>=0) && ((m+1)<=64) ) /*LOWER BORDER*/
{
array[0][0]=pixel_array[n-1][m-1];
array[0][1]=pixel_array[n-1][m];
array[0][2]=pixel_array[n-1][m+1];
array[1][0]=pixel_array[n][m-1];
array[1][1]=pixel_array[n][m];
array[1][2]=pixel_array[n][m+1];
array[2][0]=0;
array[2][1]=0;
array[2][2]=0;
}
if( (m==0) && ((n-1)>=0) && ((n+1)<=64) ) /*LEFT BORDER*/
{
array[0][0]=0;
array[0][1]=pixel_array[n-1][m];
array[0][2]=pixel_array[n-1][m+1];
array[1][0]=0;
array[1][1]=pixel_array[n][m];
array[1][2]=pixel_array[n][m+1];
array[2][0]=0;
array[2][1]=pixel_array[n+1][m];
array[2][2]=pixel_array[n+1][m+1];
}
if( (m==127) && ((n-1)>=0) && ((n+1)<=64) ) /*RIGHT BORDER*/
{
array[0][0]=pixel_array[n-1][m-1];
array[0][1]=pixel_array[n-1][m];
array[0][2]=0;
array[1][0]=pixel_array[n][m-1];
array[1][1]=pixel_array[n][m];
array[1][2]=0;
array[2][0]=pixel_array[n+1][m-1];
array[2][1]=pixel_array[n+1][m];
array[2][2]=0;
}
if( ( (n-1) >0 ) && ( (n+1) <>0 ) && ( (m+1) <128 ) ) /*ALL PIXELS INSIDE BOUNDARY*/
{
array[0][0]=pixel_array[n-1][m-1];
array[0][1]=pixel_array[n-1][m];
array[0][2]=pixel_array[n-1][m+1];
array[1][0]=pixel_array[n][m-1];
array[1][1]=pixel_array[n][m];
array[1][2]=pixel_array[n][m+1];
array[2][0]=pixel_array[n+1][m-1];
array[2][1]=pixel_array[n+1][m];
array[2][2]=pixel_array[n+1][m+1];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
pixel=pixel+( array[i][j] * kernel[i][j] );
}
}
pixel=(int)(pixel/sum);
printf("%d ", pixel);
fprintf(fp,"%d",pixel);
fprintf(fp," ");
}
fprintf(fp,"\n");
}
fclose(fp);
}
/*RANDOM NUMBERS*/
int RandomNumbers()
{
int index=0,num=0, limit=0,flag=-1;
int random_integer[MAX2];
int lowest=1, highest=MAX1;
int range=(highest-lowest)+1;
int count=0;
int index2=0;
srand((unsigned)time(0));
for(index=0; index
}
unique_rand_int[count]=random_integer[count];
limit++;
for(index=0;index
for(index2=0; index2
flag=1;
break;
}
else flag=0;
}
if(flag==0)
{
limit++;
count++;
unique_rand_int[count]=random_integer[index];
}
}
return count;
}
/*NOISE ADDITION BY RANDOM NUMBERS*/
void AddNoise()
{
int num=0;
FILE *F_N;
int m=0, n=0,pixel=0, count=0;
F_N=fopen("AddNoisetoFilter.pgm", "w");
fprintf(F_N,"%s",ch);
num=RandomNumbers();
for(n=0;n
for(m=0;m
if(count
if(pixel_array[n][m]<=127)
pixel=pixel_array[n][m]+unique_rand_int[count];
if(pixel_array[n][m]>127)
pixel=pixel_array[n][m]-unique_rand_int[count];
count++;
}
if(count>=num)
{
count=0;
}
printf("%d ", pixel);
fprintf(F_N,"%d",pixel);
fprintf(F_N," ");
}
fprintf(F_N,"\n");
}
}
No comments:
Post a Comment