Line 67: | Line 67: | ||

%Pass noisy image through median filter and calculate computation time<br /> | %Pass noisy image through median filter and calculate computation time<br /> | ||

− | tic; | + | tic;<br /> |

− | + | ||

− | + | ||

− | %Calculate MSE and PSNR | + | I = medianFilter2(C, win_size);<br /> |

− | squaredErrorImage = (G - I) .^ 2; | + | |

− | mse(idx) = sum(sum(squaredErrorImage))/(height*width); | + | median_time(idx) = toc;<br /> |

− | peak_snr(idx) = psnr(I, G); | + | |

+ | %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 /> | |

− | figure( | + | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | 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 /> | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | xlabel('Pixel Intensity') | + | |

− | + | ||

− | + | ylabel('Number of Pixels')<br /> | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | ylabel('Number of Pixels') | + | |

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

'''Results''' | '''Results''' |

## Revision as of 22:45, 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 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