Line 1: | Line 1: | ||

<big>'''Finding the Optimal Filter Size for Median Filtering'''</big> | <big>'''Finding the Optimal Filter Size for Median Filtering'''</big> | ||

+ | |||

'''Overview''' | '''Overview''' | ||

Line 16: | Line 17: | ||

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- | 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- | ||

− | %Read image to Matlab | + | %Read image to Matlab<br /> |

− | G = imread('yacht.tif'); | + | |

− | G = im2double(G); | + | G = imread('yacht.tif');<br /> |

− | height = length(G(:,1)); | + | |

− | width = length(G(1,:)); | + | 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; | tic; | ||

I = medianFilter2(C, win_size); | I = medianFilter2(C, win_size); |

## Revision as of 21:40, 30 November 2019

**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-

%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 to compare the tradeoff between SNR and 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 intensites
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