Luke jf Schlather home
A median filter is a noise reduction algorithm useful in image processing. The basic idea is to smooth out the colors in the image, which can be very useful, for example, if you want to find 'edges' in the image, (otherwise known as Image Segmentation.) To illustrate, it lets you take an image that looks like this:

And make it look like this:

Framesize of 5, threshold of 100.

For a computer, this is a lot more palatable to do analysis. Our eyes look at the first image and see very clear regions, while a computer just sees a jumble of pixels - and hardly a single pair of pixels are the same color. The median filter takes similar pixels and makes the actual values smaller.

For my implementation, I wrote a basic image class in C++ that is capable of reading and writing PPM (portable pixmap) files. PPM is an uncompressed format that is extremely easy to read and write, much like Microsoft's .bmp format. My implementation handles PPMs with 24-bit pixels. To get input for the program, I used the ImageMagick toolkit to make jpegs into ppms (then convert them back to post here.) Doing this is as simple as typing this command into the Linux console:

convert stall.jpg stall.ppm

Now, as for the algorithm itself:

The algorithm relies on two parameters which will affect how much detail it removes. The first is the framesize. The algorithm examines each pixel in turn, and compares it to all of the pixels in a box around it. The framesize defines how far away the pixels it is compared to should be. Ideally, it would be a circular region, but so I don't have to mess with iterating over a roughly circular area, I chose to use this 'radius' to define a square instead. The results are good, and I don't really see a need to work out a way to do it with a circle. To illustrate:

Framesize picture

The algorithm goes through each pixel, and for each of the three color channels (red, blue, and green) it finds the median value for that color channel within the bounding box defined by the framesize. Here's where the threshold comes in. If all three values differ from the median value by greater than the threshold, the algorithm replaces the current pixel with the median color channel values over the frame.

Now, this is my implementation, there are some other ways it could be done. I tried it instead replacing each color channel independently, regardless of the other channels; this resulted in splotches of single colors that didn't seem to give quite the effect I wanted. I also played around with using the average; this definitely wasn't as effective (though I didn't use the average to decide if I wanted to replace a pixel, only in replacing a pixel.)

If you're curious, source is code available at Tested under Ubuntu 9+, and several varieties of Fedora Linux.