(Blanked the page)
Line 1: Line 1:
<big>'''Finding the Optimal Filter Size for Median Filtering'''</big>
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-<br />
1) Mean Squared Error of filtered image compared to the original image<br />
2) Peak Signal to Noise Ratio of the filtered image and the original image<br />
3) Computation time to generate filtered image<br />
4) Blur comparison of filtered image and original image<br />
5) Visual Representation to compare filtered image and original image<br />
'''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.
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 <br />
function Y = medianFilter2(X, N)<br />
height = length(X(:,1));<br />
width = length(X(1,:));<br />
Y = zeros(height, width);<br />
for row_idx = floor(N/2)+1:height-floor(N/2)-1<br />
    for col_idx = floor(N/2)+1:width-floor(N/2)-1<br />
        chop = X(row_idx-floor(N/2):row_idx+floor(N/2), col_idx-floor(N/2):col_idx+floor(N/2));<br />
        Y(row_idx, col_idx) = median(chop(:,:), 'all');<br />
    end<br />
end    <br />
end<br />
%Read image to Matlab<br />
G = imread('yacht.tif');<br />
G = im2double(G);<br />
height = length(G(:,1));<br />
width = length(G(1,:));<br />
%Add noise with noise density of 0.1 to image<br />
C = imnoise(G, 'salt & pepper', 0.1);<br />
%Check initial distribution of pixel intensites<br />
figure(100)<br />
x = reshape(G, 1, height*width);<br />
hist(im2uint8(x), 0:255);<br />
title('Histogram of original yacht.tif')<br />
xlabel('Pixel Intensity')<br />
ylabel('Number of Pixels')<br />
%Allocate space and initialize filtered image, mse and peak_snr to 0<br />
I = zeros(height, width);<br />
mse = zeros(1,10);<br />
peak_snr = zeros(1,10);<br />
median_time = zeros(1,10);<br />
max_window_size = 21;<br />
y = zeros(height*width, 10);<br />
idx = 1;<br />
for win_size = 3:2:21<br />
    %Pass noisy image through median filter and calculate computation time<br />
    tic;<br />
    I = medianFilter2(C, win_size);<br />
    median_time(idx) = toc;<br />
    %Calculate MSE and PSNR<br />
    squaredErrorImage = (G - I) .^ 2;<br />
    mse(idx) = sum(sum(squaredErrorImage))/(height*width);<br />
    peak_snr(idx) = psnr(I, G);<br />
    %Reshape filtered image for generating distribution of pixel intensities<br />
    y(:, idx) = reshape(im2uint8(I), 1, height*width);<br />
    idx = idx + 1;<br />
end<br />
%Create plots of MSE, PSNR and Computation time<br />
figure(1)<br />
scatter(3:2:max_window_size, mse)<br />
xlabel('Window Size')<br />
ylabel('MSE')<br />
title('Mean Squared Error with different window size')<br />
figure(2)<br />
scatter(3:2:max_window_size, peak_snr)<br />
xlabel('Window Size')<br />
ylabel('PSNR(dB)')<br />
title('PSNR with different window size')<br />
figure(3)<br />
scatter(3:2:max_window_size, median_time)<br />
xlabel('Window Size')<br />
ylabel('Median Filter Computation Time')<br />
title('Computation Time with different window size')<br />
%Check filtered distribution of pixel intensities<br />
figure(101)<br />
temp = y(:,1);<br />
hist(temp(:,1), 0:255);<br />
title('Histogram of filtered house.tif with filter size = 3x3')<br />
xlabel('Pixel Intensity')<br />
ylabel('Number of Pixels')<br />
figure(102)<br />
temp2 = y(:,3);<br />
hist(temp2(:,1), 0:255);<br />
title('Histogram of filtered house.tif with filter size = 7x7')<br />
xlabel('Pixel Intensity')<br />
ylabel('Number of Pixels')<br />
figure(103)<br />
temp3 = y(:,10);<br />
hist(temp3(:,1), 0:255);<br />
title('Histogram of filtered house.tif with filter size = 21x21')<br />
xlabel('Pixel Intensity')<br />
ylabel('Number of Pixels')<br />
* 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.<br />
Following are the visual representations of the filtered images-

Latest revision as of 23:18, 30 November 2019

Alumni Liaison

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett