Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Part 2: 2D Median Filter With Different Window Size

MATLAB CODE:

clear all

%READ AN IMAGE
I = imread('zebra.jpg');
display(size(I));

%CONVERT RGB IMAGE INTO GRAYSCALE
A = rgb2gray(I);

%ADD SALT AND PEPPER NOISE TO THE GRAYSCALE IMAGE
A = imnoise(A,'Salt & pepper',0.1);

figure,imshow(A);title('IMAGE WITH SALT AND PEPPER NOISE');

%DEFINE THE WINDOW SIZE MXN
M=5;
N=5;

%PAD THE MATRIX WITH ZEROS ON ALL SIDES
modifyA=padarray(A,[floor(M/2),floor(N/2)]);
figure,imshow(uint8(modifyA)); title('PADDED WITH ZEROS');

B = zeros([size(A,1) size(A,2)]);
med_indx = round((M*N)/2); %MEDIAN INDEX

for i=1:size(modifyA,1)-(M-1)
    for j=1:size(modifyA,2)-(N-1)
       
       
        temp=modifyA(i:i+(M-1),j:j+(N-1),:);
        tmp_sort = sort(temp(:));%tmp(:) converts 2D matrix to 1D matrix
        B(i,j) = tmp_sort(med_indx);
      
      
       
    end
end


 %CONVERT THE IMAGE TO UINT8 FORMAT.
B=uint8(B);
figure,imshow(B);
title('IMAGE AFTER MEDIAN FILTERING');

EXPLANATION:
·        The image is read into the matrix A.  
·        The image is color matrix that contains Red, Green and Blue channels so ‘rgb2gray()’ command is used to convert RGB image to grayscale.
·        Salt and pepper noise is added to the image.  Learn how to add 'salt and pepper noise to an image'.
·        Define the window size MxN example: 3x3, 7x7, 5x3
·        Pad the image with zeros on all sides. This is done to perform the filtering on the border pixels. Learn howto pad with zeros using MATLAB built_in function padarray.
·        Median is the middle point of the series. The index that is obtained by dividing the total number of elements in a window by 2 gives the position.
·        A sliding window of size M x N is used and the elements in the window are sorted and the middle element from the sorted array is chosen.





EXPLANATION:

·        The image filtered with 3x3 window still contains salt and pepper noise but the edges are still sharp.
·        The image filtered with 5x5 window  contains less noise compared to 3x3 window filter and edges are smoothed a bit
·        The image filtered with 7x7 window is free of noise but the edges are smoothed to certain extent.
like button Like "IMAGE PROCESSING" page

0 comments:

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com