/*********************************/ /** Solution to ComputingII **/ /** Exercise 1(b) PJM 08/02/00 **/ /*********************************/ #pragma windows #include #include #include <\DBOS\graphics.h> // These libraries must be included #include // to use graphics functions void Plot_2D_Array(int *); // Function to plot grey-scale graph void filter(int *); // Function to filter image (1D) int main(void) { FILE *fp; int data[128][128]; //array to hold image data int *pointer; int row,col,ctrl; fp=fopen("noisy.dat","r"); for (row=0;row<128;row++) // load data into 2D array { for (col=0;col<128;col++) fscanf(fp,"%d",&data[row][col]); } fclose(fp); winio("%gr[black]%ww%lw",128,128,&ctrl); // OPEN GRAPHICS WINDOW pointer=&data[0][0]; // the variable "pointer" points to the data array filter(pointer); // FILTER DATA Plot_2D_Array(pointer); // pass the filtered data array to the Plot_2D_Array function } void Plot_2D_Array(int *array) { short i,rows,cols; // SET UP GREY SCALE for (i=0;i<256;i++) set_video_dac(i,i,i,i); // DISPLAY IMAGE for (rows=0;rows<128;rows++) { for (cols=0;cols<128;cols++) set_pixel(cols,rows,*(array+cols+(128*rows))); } } void filter(int *data_array) { int i,j,sum,average,xcoord,ycoord,check; int filter_array[128][128]; // Calculate average values // Note that loop limits in the following two lines // are set so that edge pixels are not filtered. // That is, for pixels along the left edge of the image // it is not possible to use the neighbour to the left // in the averaging process. Just omitting these pixels // is slightly unsatisfactory. Usually, a "wrap-around" method is // employed where the neighbours of the pixels along the left edge // are taken to be the pixels along the right edge. Similarly, // the pixels along the top of the image have the bottom edge pixels as // neighbours. However, to keep the code simple I have not used this wrap-around // method, simply ignoring the edge pixels. for (i=1;i<127;i++) { for(j=1;j<127;j++) { sum=0; // set sum=0 // The following lines sum up the pixels in a 3x3 box // surrounding (and including) the pixel at coordinate (i,j). // To use a 5x5 filter, change the loop limits to -2 and 2. // To use a 7x7 filter, change the loop limits to -3 and 3. etc... // However, as we are neglecting the pixels at the edge of the // image, the limits of the loops for i and j will also need to be // changed (Eg. (i=2;i<126;i++) for a 5x5 filter). for (xcoord=-1;xcoord<=1;xcoord++) { for (ycoord=-1;ycoord<=1;ycoord++) sum=sum+*(data_array+j+xcoord+(128*(i+ycoord))); } average=sum/9; // calculate average // 3x3 = 9 pixels filter_array[i][j]=average; // set values in filtered array } } for (i=1;i<127;i++) // copy filtered array values for (j=1;j<127;j++) // into original array *(data_array+j+(128*i))=filter_array[i][j]; }