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

Recent Posts

MATLAB PROGRAM : 2D MEDIAN FILTERING FOR SALT AND PEPPER NOISE WITHOUT USING medfilt2 FUNCTION

MEDIAN FILTER:
In digital Image processing, removing the noise is one of the preprocessing techniques.
The image noise may be termed as random variation of brightness or color information.
There are various types of image noise. Here a matlab program to remove 'salt and pepper noise' using median filtering is given.
  The random occurrence of black and white pixels is ‘salt and pepper noise’.
Learn how to add 'salt and pepper noise to an image'.
Median filtering preserves the image without getting blurred. Median filtering is done on an image matrix by   finding the median of the neighborhood pixels by using a window that slides pixel by pixel.

The procedural steps for 2D median filtering
Learn how to pad with zeros using MATLAB built_in function padarray.

FLOW CHART:


MATLAB CODE:

%READ AN 2D IMAGE
A=imread('zebra.jpg');
title('IMAGE WITH SALT AND PEPPER NOISE');
figure,imshow(A);

%PAD THE MATRIX WITH ZEROS ON ALL SIDES
modifyA=zeros(size(A)+2);
B=zeros(size(A));

%COPY THE ORIGINAL IMAGE MATRIX TO THE PADDED MATRIX
        for x=1:size(A,1)
            for y=1:size(A,2)
                modifyA(x+1,y+1)=A(x,y);
            end
        end
      %LET THE WINDOW BE AN ARRAY
      %STORE THE 3-by-3 NEIGHBOUR VALUES IN THE ARRAY
      %SORT AND FIND THE MIDDLE ELEMENT
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        window=zeros(9,1);
        inc=1;
        for x=1:3
            for y=1:3
                window(inc)=modifyA(i+x-1,j+y-1);
                inc=inc+1;
            end
        end
       
        med=sort(window);
        %PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
        B(i,j)=med(5);
       
    end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
title('IMAGE AFTER MEDIAN FILTERING');
figure,imshow(B);







  

like button Like "IMAGE PROCESSING" page

9 comments:

Movies Gallery 2011 said... Reply to comment

Excellent pieces. Keep posting such kind of information on your blog. I really impressed by your blog.
Vee Eee Technologies| Vee Eee Technologies

Phongthorn.jant said... Reply to comment

thank you so much for project lol

jeena said... Reply to comment

thanks a lot for this code

punya said... Reply to comment

great, thankx a lot, wht can i say, amazing

Pooja Dixit said... Reply to comment

thanks for this helpful code...

annah said... Reply to comment

thank you..it helps alot.. :)

HANEE said... Reply to comment

great job but why is my image become red after the MEDIAN FILTERING???

Aaron Angel said... Reply to comment

@HANEE

Kindly check whether the image is 2D or RGB image. If the image is RGB then convert it into grayscale and proceed.

HANEE said... Reply to comment

it worked perfectly with Gaussian Filter but not with Median Filter(image becomes red), i'm now trying with other images to see whether it is a image problem or what..

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Related Posts Plugin for WordPress, Blogger...
Twitter Bird Gadget Google ping Hypersmash.com