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’.

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);












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.


  


    ALSO MEDIAN FILTER FOR RGB IMAGE

43 comments:

  1. thank you so much for project lol

    ReplyDelete
  2. great, thankx a lot, wht can i say, amazing

    ReplyDelete
  3. thanks for this helpful code...

    ReplyDelete
  4. thank you..it helps alot.. :)

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

    ReplyDelete
  6. @HANEE

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

    ReplyDelete
  7. 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..

    ReplyDelete
  8. thanks for this usefull code........
    can we selection direction in a window ? if yes how is it possible ? actually i m thinking to preserve edges of image.......

    ReplyDelete
  9. I ve a problem with the Median Filter. The image becomes red.
    Somebody can help me with this problem?

    ReplyDelete
  10. I ve also the problem that the picture becomes red with the Median Filter . Can somebody help? Thank you a lot

    ReplyDelete

  11. Do you have also an Matlab Code for the Gauss filter?

    I didn t find / see one
    Thanks

    regrads,

    CHristian

    ReplyDelete
  12. whether any functions will change wen v change the version of matlab

    ReplyDelete
  13. hi can you give the same code for fuzzy median filter pa

    ReplyDelete
  14. Can someone code for Wiener Filter please for gray scale image

    ReplyDelete
  15. I ve also the problem that the picture becomes red with the Median Filter . Can somebody help? Thank you a lot

    ReplyDelete
  16. @Janardhan Mopada

    Is your input Image RGB? if so, convert it to grayscale and proceed.

    ReplyDelete
  17. do you have a code for contour detection with many algorithm?

    ReplyDelete
  18. Anybody can provide me the code of the 'Noise Reduction By Fuzzy Image Filtering''.

    ReplyDelete
  19. Sir, Let me know the code of Tri colour Filter(Image) / RGB only

    ReplyDelete
    Replies
    1. For rgb you need to separate 3 plates red, green, blue and aply median filtering on each plane.
      And atlast combine them using cat function.

      Delete
  20. median filtering will be work on syntax "medfilt2(gray scale image)"

    ReplyDelete
  21. Sir how to Mean filtering can apply on the DN (digital no.) values
    of each pixel vector so as to smoothen out the SRC (spectral response curve) of a pixel for hyperspectral data?

    ReplyDelete
  22. Use rgb2gray and it will work fine!

    ReplyDelete
  23. how to adapt thıs to 9*9 or 13*13 medıan fılter ? how ı do ıt?

    ReplyDelete
  24. how ı adapt matlab code to 9*9 or 13*13 medıan fılter ?

    ReplyDelete
  25. Awesome. Do you have the code for Average filtering also?

    ReplyDelete
  26. awesome. do you have the code for average filtering also?

    ReplyDelete
  27. the image turned red. there is some problem kindly rectify.

    ReplyDelete
  28. @AASTHA SINGH
    The code is for grayscale image. Kindly check whether it is rgb or grayscale.

    ReplyDelete
  29. can i use this median filter code to remove noise from ultrasound image????? plsss reply

    ReplyDelete
  30. How would i change this for a 7x7 median filter?

    ReplyDelete
  31. What is the meaning of med and med(5). Idk what that means and ive been scratchig my head for the past 1 hr

    ReplyDelete
  32. How to change this for a 7x7 or 5x5 neighborhood ?

    ReplyDelete