Search code examples
matlabfilteringmedian

median filter vs. pseudomedian filter in matlab


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!


Solution

  • 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:

    Salt and pepper noise reduction

    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.