Advertisement
subasah

problem1.1-assignmentI

Feb 9th, 2020
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.76 KB | None | 0 0
  1. %clearing command window, workspace and image windows
  2. clc;
  3. clear all;
  4. close all;
  5. % we are only here working with grayscale image
  6. original_image = imread('grayscale_cat.jpg');
  7. %
  8. %adding noise, using gaussian noise equation
  9. %of mean 10 and variance 20
  10. mean = 10;
  11. sigma = sqrt(20);
  12. original_image= double(original_image);
  13. noisy_image = original_image + mean +(randn(size(original_image))*sigma);
  14.  
  15. % making a gaussian kernel
  16. sigma = 1;        
  17. % we will normalize this mask and hence only exponential part will play a
  18. % role, here mask_matrix is our kernel of 5-by-5 size
  19. mask_matrix = zeros(5,5);      
  20. W = 0;                    % used this sum for normalization
  21. for i = 1:5
  22.     for j=1:5
  23.         sq_dist = (i-3)^2+ (j-3)^2;
  24.         mask_matrix(i,j) = exp(-1*(sq_dist)/(2*sigma*sigma));
  25.         W = W + mask_matrix(i,j);
  26.     end
  27. end
  28. mask_matrix = mask_matrix/W;
  29.  
  30. % we can use the above kernel to filter the noise added
  31. [m,n] = size(noisy_image);
  32. gausian_filtered_image = zeros(m,n);
  33. %adding zeros around the noisy matrix so that we can calculate the pixel
  34. %values of the corner element with the use of the mask above
  35. noisy_image_with_zeros = padarray(noisy_image,[2 2]);
  36. for i=1:m
  37.     for j=1:n
  38.         %temporary variable, var_temp used for calculation
  39.         var_temp = noisy_image_with_zeros(i:i+4 , j:j+4);
  40.         var_temp = double(var_temp);
  41.         conv = var_temp.*mask_matrix;
  42.         gausian_filtered_image(i,j) = sum(conv(:));
  43.     end
  44. end
  45.  
  46. gausian_filtered_image = uint8(gausian_filtered_image);
  47.  
  48. % showing here, original and filtered image
  49. figure(1);
  50. set(gcf,'Position',get(0,'Screensize'));
  51. subplot(121),imshow(uint8(original_image)),title('Intact Image');
  52. subplot(122),imshow(gausian_filtered_image),title(' gaussian filtered Image');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement