Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %clearing command window, workspace and image windows
- clc;
- clear all;
- close all;
- % we are only here working with grayscale image
- original_image = imread('grayscale_cat.jpg');
- %
- %adding noise, using gaussian noise equation
- %of mean 10 and variance 20
- mean = 10;
- sigma = sqrt(20);
- original_image= double(original_image);
- noisy_image = original_image + mean +(randn(size(original_image))*sigma);
- % making a gaussian kernel
- sigma = 1;
- % we will normalize this mask and hence only exponential part will play a
- % role, here mask_matrix is our kernel of 5-by-5 size
- mask_matrix = zeros(5,5);
- W = 0; % used this sum for normalization
- for i = 1:5
- for j=1:5
- sq_dist = (i-3)^2+ (j-3)^2;
- mask_matrix(i,j) = exp(-1*(sq_dist)/(2*sigma*sigma));
- W = W + mask_matrix(i,j);
- end
- end
- mask_matrix = mask_matrix/W;
- % we can use the above kernel to filter the noise added
- [m,n] = size(noisy_image);
- gausian_filtered_image = zeros(m,n);
- %adding zeros around the noisy matrix so that we can calculate the pixel
- %values of the corner element with the use of the mask above
- noisy_image_with_zeros = padarray(noisy_image,[2 2]);
- for i=1:m
- for j=1:n
- %temporary variable, var_temp used for calculation
- var_temp = noisy_image_with_zeros(i:i+4 , j:j+4);
- var_temp = double(var_temp);
- conv = var_temp.*mask_matrix;
- gausian_filtered_image(i,j) = sum(conv(:));
- end
- end
- gausian_filtered_image = uint8(gausian_filtered_image);
- % showing here, original and filtered image
- figure(1);
- set(gcf,'Position',get(0,'Screensize'));
- subplot(121),imshow(uint8(original_image)),title('Intact Image');
- subplot(122),imshow(gausian_filtered_image),title(' gaussian filtered Image');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement