Finding the Optimal Filter Size for Median Filtering

Overview

Median filtering is a non-linear digital filtering technique which is used to reduce impulsive noise from a signal. It has several applications in image processing. In the image processing lab (Week 13: Lab 10a), a median filter with a filter size of 3x3 was applied to an image of a race car that contained salt and pepper noise. This page will demonstrate how the median filter size affects the filtered image in terms of-

1) Mean Squared Error of filtered image compared to the original image

2) Peak Signal to Noise Ratio of the filtered image and the original image

3) Computation time to generate filtered image

4) Blur comparison of filtered image and original image

5) Visual Representation to compare filtered image and original image


Experimental Setup

The yacht image from the image processing lab(Week 13: Lab 10a) was used to conduct the experiments. Noise with a noise density of 0.1 was added to the image. https://drive.google.com/file/d/16ArG1osinq7UCnkrVAbzvvwzD8zib8wJ/view?usp=sharing

Then the image was passed through the median filter of various sizes and the computation time was calculated. Additionally, the mean squared error and the peak signal-to-noise ratio between the filtered image and the original image were calculated and plotted for several window sizes. Finally, a histogram of the filtered image was generated to check the number of pixels at each intensity and the filtered image itself was plotted. Following is the Matlab code for the experiment-

%Median Filter
function Y = medianFilter2(X, N)

height = length(X(:,1));

width = length(X(1,:));

Y = zeros(height, width);

for row_idx = floor(N/2)+1:height-floor(N/2)-1

   for col_idx = floor(N/2)+1:width-floor(N/2)-1
       chop = X(row_idx-floor(N/2):row_idx+floor(N/2), col_idx-floor(N/2):col_idx+floor(N/2));
       Y(row_idx, col_idx) = median(chop(:,:), 'all');
   end

end

end


%Read image to Matlab

G = imread('yacht.tif');

G = im2double(G);

height = length(G(:,1));

width = length(G(1,:));


%Add noise with noise density of 0.1 to image

C = imnoise(G, 'salt & pepper', 0.1);


%Check initial distribution of pixel intensites

figure(100)

x = reshape(G, 1, height*width);

hist(im2uint8(x), 0:255);

title('Histogram of original yacht.tif')

xlabel('Pixel Intensity')

ylabel('Number of Pixels')

%Allocate space and initialize filtered image, mse and peak_snr to 0

I = zeros(height, width);

mse = zeros(1,10);

peak_snr = zeros(1,10);

median_time = zeros(1,10);

max_window_size = 21;

y = zeros(height*width, 10);

idx = 1;

for win_size = 3:2:21

   %Pass noisy image through median filter and calculate computation time
   tic;
   I = medianFilter2(C, win_size);
   median_time(idx) = toc;
   %Calculate MSE and PSNR
   squaredErrorImage = (G - I) .^ 2;
   mse(idx) = sum(sum(squaredErrorImage))/(height*width);
   peak_snr(idx) = psnr(I, G);
   %Reshape filtered image for generating distribution of pixel intensities
   y(:, idx) = reshape(im2uint8(I), 1, height*width);
idx = idx + 1;

end

%Create plots of MSE, PSNR and Computation time

figure(1)

scatter(3:2:max_window_size, mse)

xlabel('Window Size')

ylabel('MSE')

title('Mean Squared Error with different window size')

figure(2)

scatter(3:2:max_window_size, peak_snr)

xlabel('Window Size')

ylabel('PSNR(dB)')

title('PSNR with different window size')

figure(3)

scatter(3:2:max_window_size, median_time)

xlabel('Window Size')

ylabel('Median Filter Computation Time')

title('Computation Time with different window size')

%Check filtered distribution of pixel intensities

figure(101)

temp = y(:,1);

hist(temp(:,1), 0:255);

title('Histogram of filtered house.tif with filter size = 3x3')

xlabel('Pixel Intensity')

ylabel('Number of Pixels')

figure(102)

temp2 = y(:,3);

hist(temp2(:,1), 0:255);

title('Histogram of filtered house.tif with filter size = 7x7')

xlabel('Pixel Intensity')

ylabel('Number of Pixels')

figure(103)

temp3 = y(:,10);

hist(temp3(:,1), 0:255);

title('Histogram of filtered house.tif with filter size = 21x21')

xlabel('Pixel Intensity')

ylabel('Number of Pixels')


Results

https://drive.google.com/file/d/1vYwHYtDJKahHdO56Uk2IpIefdo6Eu8c3/view?usp=sharing

https://drive.google.com/file/d/1Z_PVPX3JMKCbQk7Fm8lqqnJFxflrqDPd/view?usp=sharing

https://drive.google.com/file/d/1fyRpQT5MuVZn2RNiHTZvrqRZ0je6jT4S/view?usp=sharing

https://drive.google.com/file/d/1ObZaNl-W0kQ7CLVZ28pbvYbrwAcNzP_c/view?usp=sharing

https://drive.google.com/file/d/1BuJkPX2pkGlq5wKfj58eRVyYnRuTkS8s/view?usp=sharing

https://drive.google.com/file/d/1ihw1quSqhVRTPLv15wNUmlMWmT8FFQNE/view?usp=sharing

Conclusion

  • The mean squared error of the filtered image increases linearly with the increase in the filter size suggesting that a median filter size of 3x3 is optimal.
  • The peak signal to ratio of the filtered image decreases with the increase in filter size again suggesting that a median filter size of 3x3 is optimal.
  • The computation time increases with the increase in filter size. This is not surprising as most of the time is taken while computing the median and it is faster to compute the median of a 3x3 matrix than a 21x21 matrix.
  • The results of the distribution of the pixel intensities shows that there are more 0 intensities as filter size increases. This means that there are less number of pixels representing the actual image which leads to blurring as can be seen from the visual representations of the filtered images.

Following are the visual representations of the filtered images-

https://drive.google.com/file/d/1zZq8cJ2-W1899w68tMsGtowLJIQ2Vt7g/view?usp=sharing

https://drive.google.com/file/d/1DNH_CkbO0tcTYGZecDmb7wK-CRJYfEUr/view?usp=sharing

https://drive.google.com/file/d/1kVVQ8kP3KRRkN3SAF2uF0PhGHAwL_i8m/view?usp=sharing

https://drive.google.com/file/d/1Kj7TEa_Q5BX0oVBQ512TfOJZPikAu2FY/view?usp=sharing

Alumni Liaison

ECE462 Survivor

Seraj Dosenbach