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-

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

## Alumni Liaison

Correspondence Chess Grandmaster and Purdue Alumni

Prof. Dan Fleetwood