The median filter is a well-known image processing filter. It provides a very nice way to smooth an image while preserving edges. The median filter replaces each pixel in the image with the median value of its neighboring pixels. A similar non-linear filter with slightly different properties is the mode filter which replaces each pixel with the mode of its neighboring pixels. I additionally make a slight modification so that “bad” pixels are ignored entirely in the computation of the mode.

This idea arose when I was trying to de-noise some images as well as do some in-painting of “bad” pixels (that have no value). Consider the image below. The darkest-blue areas are bad pixels. We have no information for those pixels. The other pixels are colored to show how far that pixel is from the camera. (see the post on Stereo Vision) However, some of the good pixels still have the wrong value. These are the noise pixels.


original data
[Initial Image]

In the rest this post I talk about how we use a selective mode filter to convert the above image into the one below. (There’s also download-able Matlab/C++ code)


selective modefilt
[Final Result of Selective Mode Filter]

First Try

First, we compare a standard median filter along-side a standard mode filter. We see that the two results are very similar. I deem the mode filter to be slightly better, but this is pretty subjective. As you can see, both methods do a good job of eliminating noise although neither filter removes the bad pixels.


median filter  mode filter
[Standard Median Filter, Standard Mode Filter]

Being Selective

The solution is to ignore bad pixels so that they can not contribute to the computation of the median or mode (depending on which filter you use). In my example, bad pixels are value ‘0’ while all the other pixels are positive and non-zero. Therefore, I re-wrote the mode filter to ignore pixels under a threshold when computing the local mode around each pixel. The result is very nice. Some of the fine-scale features are lost, but overall, the number of noise pixels and bad pixels are greatly reduced.

Conclusion & Download

This is a simple idea, but it had very nice results. A good next step would be to create a selective median filter. I implemented this in mex so the operation is very quick. In the demo below I include binaries for most platforms, but if doesn’t work on your computer you may need to re-compile.

modefilt.zip

3 thoughts on “Selective Mode filter in Matlab

  1. in my view “your” first implementation was much better than this ;-)

    Did you try Belief Propagation? in MEX it works just slightly slower than WTA+Median, but results are more reliable
    http://people.cs.uchicago.edu/~pff/bp/
    (and then Median filter adds a bit more dB in PSNR)

    How do you think to incorporate temporal redundancy between video frames to improve estimate?

  2. Hello Yawn,
    Great work, thanks for shared it.
    I am asking if you have used the belief propagation at the final step of your code.

Leave a Reply

Your email address will not be published.