2) This project aims to reduce image noise using mask operation. a) Use imnoise MATLAB function to add different noises to the input image. b) Investigate the usefulness of each of the mean, median and Gaussian filtering for removing different types and levels of image noise (Hint: use fspecial and imfilter commands in MATLAB). c) Investigate the effect of different 3x3 averaging masks. d) Investigate the effect of filter size on the computation time. Consider Gaussian filter and change its size and record the corresponding operation time. Does the timing increase linearly or not? Why is this? (Hint: to measure the time use tic and toc MATLAB comments).
ANSWER:
CODE TEXT
% Reading sample.JPG image
img = imread("sample.JPG");
% Grayscaling the image
input_img = rgb2gray(img);
%% a. Adding different types of noises using imnoise
figure(1);
% Showing original image
subplot(2,2,1);
imshow(input_img);
title('Original Image');
% Adding gaussian noise to image
gauss_noise = imnoise(input_img,'gaussian');
% Showing gauss_noise image
subplot(2,2,2);
imshow(gauss_noise);
title('gaussian noise image');
% Adding poisson noise to image
poisson_noise = imnoise(input_img,'poisson');
% Showing poisson noise image
subplot(2,2,3);
imshow(poisson_noise);
title('poisson noise image');
% Adding salt & pepper noise to image
snp_noise = imnoise(input_img,'salt & pepper');
% Showing salt & pepper noise image
subplot(2,2,4);
imshow(snp_noise);
title('salt & pepper noise image');
%% b. Investigating usefullness of different filters
figure(2);
% Creating a cell of different noised images
noise_images = {gauss_noise, poisson_noise, snp_noise};
noise_images_labels=["gaussian", "poisson", "salt & pepper"];
% defining empty vector to store snr values for different noise and
% filterings
filter_snrs=[];
% looping for each type of noise image and applying different filters
for i=1:length(noise_images)
% defining empty vector to store snr values for different filtering
filter_snr=[];
% fetching single noised image from cell
noise_image = noise_images{i};
% ploting noise_image
subplot(3,4,(i-1)*4+1);
imshow(noise_image);
title(sprintf('%s noise Image',noise_images_labels(i)));
% a. applying mean/average filter
mean_filter = fspecial('average');
mean_image = imfilter(noise_image, mean_filter);
% finding snr
snr_val = snr(double(input_img),double(mean_image));
filter_snr = [filter_snr snr_val];
% ploting
subplot(3,4,(i-1)*4+2);
imshow(mean_image);
title('Mean filter');
% b. applying median filter, using medfilt2
median_image = medfilt2(noise_image);
% finding snr
snr_val = snr(double(input_img),double(median_image));
filter_snr = [filter_snr snr_val];
% ploting
subplot(3,4,(i-1)*4+3);
imshow(median_image);
title('Median filter');
% c. applying gaussian filter
gaussian_filter = fspecial('gaussian');
gaussian_image = imfilter(noise_image, gaussian_filter);
% finding snr
snr_val = snr(double(input_img),double(gaussian_image));
filter_snr = [filter_snr snr_val];
% ploting
subplot(3,4,(i-1)*4+4);
imshow(gaussian_image);
title('Gaussian filter');
% appending the snr_values to complete list
filter_snrs=[filter_snrs; filter_snr];
end
%% b&c. ploting snr to investigate the effect of different filters on
% different types of noise
figure(3);
for i=1:size(filter_snrs,1)
subplot(3,1,i);
plot([1],filter_snrs(i,1),'*');
hold on;
plot([2],filter_snrs(i,2),'*');
hold on;
plot([3],filter_snrs(i,3),'*');
legend('Mean filter','Median filter','Gaussian filter');
title(sprintf('SNR Values for Different filters on %s noise Image', ...
noise_images_labels(i)));
ylabel('SNR');
end
%% d. Investigating effect of kernel size on computational time
kernel_sizes=3:2:30;
% defining vector to store time
time_taken=[]
for k=kernel_sizes
% fetching map of size k
gaussian_filter = fspecial('gaussian',k);
% initializing tic
tic;
% applying kernel
gaussian_image = imfilter(noise_image, gaussian_filter);
% checking time
time_taken = [time_taken, toc];
end
% ploting graph Kernel size vs time
figure(4);
plot(kernel_sizes,time_taken);
xlabel('Kernel Size');
ylabel('Computational Time');
title('Kernel Size vs Computational Time');
CODE IMAGE
OUTPUT IMAGE
a.
b .
c.
d.
Get Answers For Free
Most questions answered within 1 hours.