You are on page 1of 7

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

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:

Anyone knows why the pseudomedian filter is faster than the median filter? I used medfilt2.m for median filtering and I implemented my own pseudomedian filter which is:
b = strel('square',3); psmedIm = (0.5*imclose(noisedIm,b)) + (0.5*imopen(noisedIm,b));

where b is a square flat structuring element and noisedIm is an image noised by a salt and pepper noise. Also I don't understand why the image generated using the pseudomedian filter isn't denoised. Thank you!

In terms of your speed query, I'd propose that your pseudomedian filter is faster because it doesn't involve sorting. The true median filter requires that you sort elements and find the central value, which takes a fair bit of time. The reason why your salt and pepper noise isn't removed is that you're always maintaining their effects because you're always using both the min and max values inside the structuring element when you use imclose and imopen. Because you're just weighting each by half, if there's a white pixel, the 0.5 factor contribution from the max function will bump the pixel value up, and vice versa for black pixels. EDIT: Here's a quick demo I did that helps your pseudomedian behave a little more nicely with salt and pepper noise. The big difference is that it tries to use the 'best parts' of the opened and closed images rather than making them fight it out. I think it works quite well for eliminating the salt and pepper noise you used as an example.
img = imread('cameraman.tif'); img = imnoise(img, 'salt & pepper', 0.01); subplot(2,2,1); imshow(img); b = strel('square', 3); closed = double(imclose(img, b)); opened = double(imopen(img, b)); subplot(2,2,2); imshow(closed,[]); subplot(2,2,3); imshow(opened,[]); img = double(img); img = img + (closed - img) + (opened - img); subplot(2,2,4); imshow(img,[]);

EDIT: Here's the result of running the code:

EDIT 2: Here's the underlying theory (it's not overly mathematical and based entirely on intuition!) Salt and pepper noise exists as pure white and pure black pixels scattered randomly. The idea is that the 'closed' and 'opened' images will each eliminate one of the halves -- either the white salt noise or the black pepper noise -- and the pixel value in that location should be corrected by one of the operations. We just don't know which one. So we know that one of the images out of both 'closed' and 'open' is 'correct' for that pixel because the operation should have effectively 'median-ed' that pixel correctly. Since the one that is 'incorrect' should have exactly the same value at that pixel (white or black) as the original image, subtracting its value doesn't affect the original image. Only the 'correct' one (which differs by the exact amount required to return the image to its supposedly correct value) is right, so we adjust the image at that pixel by the corresponding amount. Thus, taking the noisy original image and adding to it both the differences gives us something with much of the noise reduced.

http://www.scribd.com/doc/38757950/48/Cleaning-salt-and-pepper-noise

You might also like