Here, we apply the median filter to the corrupted image by salt and peper. To get salt and paper I used matlab function I = imnoise(A,'salt & pepper',0.3) for 30% and J = imnoise(A,'salt & pepper',0.5) for 50% corruption. Again, the dollar image is used as the original image.
Implementation of the median filter is pretty much the same with spatial filtering. The changes are in zero padding, start and end points of the for loops because now we made our matrix size (row*4 X cols*4) instead of multiplying by 2 and in the helper “Filter” function. “Filter” is now named as “MyMedian” which actually takes the original image and the desired index that will be needed for finding the median of the 5X5 matrix. This function takes 5X5 neighboured indices, holds them in array and evaluate the median.
Let’s have look at the MedianFilter function. The actual work done is implemented as below after zero padding. Note that the values in the for loops are re arranged.
%zero padding
%first, new matrix is filled with 0
for k = 1: row + 4
for l=1 : cols + 4
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+2,l+2) = A(k,l);
end
end
%here, the actual work is done
%I wrote another function called
%"MyMedian" that carries out median for
%only 5x5 part of the input matrix here
%the 2 for loops make median filtering
%for all the possible non zero values of the C
for k = 3 : row + 2
for l = 3 : cols + 2
value = MyMedian(C,k,l);
%a temp matrix is used not to override C and change its values
%after a 5x5 convolution of it
Temp(k,l) = value;
end
end
Instead of equating value to Filter function now MyMedian function that evaluates the median is used. Theb the Temp is equated to C.
%temp is saved in C
for k = 3 : row + 2
for l = 3 : cols + 2
C(k,l) = Temp(k,l);
end
end
Now, we can look at the MyMedian function.
for m = x-2: x + 2
for n = y-2: y + 2
k = k + 1;
%the value of A saved in a matrix B
B(k) = A(m,n);
end
end
As said before it finds the median of any 5X5 matrix. To do this this matrix is saved in B then the median function of the matlab is used.
| 30 % Salt & Pepper Noised Image |
| Applying the Median Filter |