Spatial filtering is performed by convolving the image with a mask so that the new image g(x,y) is evaluatedas the following convolution formula.
In order to do that a matlab code called SpatialFilter is implemented. First, we need to do zero padding. I started to fill the output image with full of zeros as the following.
%C is the output image %A is the original image %B is the filter function [C] =SpatialFilter(A,B) for k = 1: row + 2 for l=1 : cols +2 C(k,l) = 0; end end
Notice that 2 is added to end points and the new image’s size became row+2 X cols+2 where the row and cols is the input images’ row and column values. After that,the pixel values of the input image is saved on the output image C with the desired places which starts from the point (2,2) to (row,cols). Also notice that it isthe size of the input image.
for k = 1 : row for l = 1 : cols C(k+1,l+1) = A(k,l); end endSo, our input image A is zero padded and saved in the output image C.
%here, the actual work is done
for k = 2 : row +1
for l = 2 : cols +1
value = Filter(C,B,k,l) / sumx;
Temp(k,l) = value;
end
end
Here, I used another function that I wrote called Filter. This function only does the convolution for the given index values that is a 3x3 matrix.
After this partial convolution, the result is saved in a temporary matrix. The reason why I used a temporary matrix is that if I saved the new values in C then the old values of C would change and it would affect the result of the convolution.
The explained producere is repeated for all the pixels in the zero padded input array A. See that I didn’t convolve the zero padded values in order to that for loops strats from 2.
Finally, the Temp is saved in the output matrix C.
for k = 2 : row + 1
for l = 2 : cols + 1
C(k,l) = Temp(k,l);
end
end
Now, lets have look at Filter function. (The functionality of this function is explained above.)% m an n is the neighboured indices of %the given index so I traverse all % the neighbours of the given %index through the below loop % k and l is used for indices of the filter for m = x-1: x + 1 k = k +1; for n = y-1: y + 1 l = l + 1; filter = filter +(A(m,n) * B(k,l)); end l = 0; endThe for loops does the convolution of the wanted index with the filter by multiplying the given index and neighbour values with the filter and then summing them up. For filtering the dollor image is used. Results of using bluring and spatial filters are shown below.
| Original 1 Dollar Image |
| Blurred 1 Dollar |
| After Laplacian Filter is Applied |
%A is the image
%B is the filter
%x and y is the the indices that will convolve with the filter
%multiplies 3 x 3 neighbours of the given index of the image with 3 x 3
%filter with corresponding indices and sum them
function [filter] = Filter(A,B,x,y)
filter = 0;
%used to take indices of the filter
k = 0;
l = 0;
% m an n is the neighboured indices of the given index so I traverse all
% the neighbours of the given index through the below loop
% k and l is used for indices of the filter
for m = x-1: x + 1
k = k +1;
for n = y-1: y + 1
l = l + 1;
filter = filter +(A(m,n) * B(k,l));
end
l = 0;
end
end
%c is the output image
%A is the original image
%B is the filter
function [C] =SpatialFilter(A,B)
%input is converted to double
A = double(A);
%Initialize the row & columns
[row,cols] = size (A);
%sumx is the total sum of the filter
sumx = 0;
%zero padding
%first, new matrix is filled with 0
for k = 1: row + 2
for l=1 : cols +2
C(k,l) = 0;
end
end
%then values of the input is saved to output
%with the desired place to satisfy zero padding
for k = 1 : row
for l = 1 : cols
C(k+1,l+1) = A(k,l);
end
end
%sum of the filter index values is evaluated
for i = 1:3
for j = 1:3
sumx = sumx + B(i,j);
end
end
%here, the actual work is done
%I wrote another function called "Filter" that carries out convolution for
%only 3x3 part of the input matrix here the 2 for loops make this filtering
%for all the possible non zero values of the C
for k = 2 : row +1
for l = 2 : cols +1
value = Filter(C,B,k,l) / sumx;
%!!!don't divide sumx when evaluating laplacian!!!
%value = Filter(C,B,k,l);
%a temp matrix is used not to override C and change its values
%after a 3x3 convolution of it
Temp(k,l) = value;
end
end
%temp is saved in C
for k = 2 : row + 1
for l = 2 : cols + 1
C(k,l) = Temp(k,l);
end
end
end