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;
- %loading 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);
- an = mean + (randn(size(original_image))*sigma);
- noisy_image = original_image + an;
- [m,n]=size(noisy_image);
- %usng a mask of 3-by-3
- mask_matrix = [1 2 1; 2 4 2; 1 2 1];
- %z=ones(3);
- [p,q] = size(mask_matrix);
- w=1:p;
- x = round(median(w));
- %{
- below, we are making noisy matrix's corner's element all zero so that we
- can use 3-by-3 kernel with ease for corner elements
- %}
- noisy_image_with_extra_zeros = zeros(m+2*(x-1),n+2*(x-1));
- for i=x:(m+(x-1))
- for j=x:(n+(x-1))
- noisy_image_with_extra_zeros(i,j) = uint8(noisy_image(i-(x-1),j-(x-1)));
- end
- end
- figure, imshow(uint8(noisy_image_with_extra_zeros))
- title('noisy-image with extra zeros around');
- x=0;
- y=0;
- %these loops map the noisy_image with zeros to the kernel so that summation
- %can occur after multiplication between them
- for i=1:m
- for j=1:n
- for k=1:p
- for l=1:q
- summation(k,l)= noisy_image_with_extra_zeros(i+x,j+y)*mask_matrix(k,l);
- y=y+1;
- end
- y=0;
- x=x+1;
- end
- x=0;
- %here we are finding the closest pixel to consider for our outcome
- %image
- to_be_sorted= abs(summation - double(noisy_image(i,j)));
- sorted_vector = sort(to_be_sorted(:));
- filtered_image(i,j)=(sum(sorted_vector(1:5)));
- summation=[];
- end
- end
- figure, imshow(uint8(filtered_image))
- title('Filtered Image by taking 5 closest point');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement