# I – Salt & Pepper Noise – Non-Linear Filtering

Anyone who has ever used an old television set will likely have encountered salt and pepper noise before. It is sometimes referred to casually as a “fuzzy” or “snowy” screen. Salt and pepper noise is simply the random scattering of black and white pixels throughout an image, which looks like a picture with black and white specs (ie: salt and pepper) all over the image. Consider the sample image and its respective histogram, shown below.

Sample image and histogram

Now, observe the effects randomly making 25% of the pixels in this image either black or white.

Image with noise

Notice the item in the far right of the above image, the spectrum of just the noise: It’s all either black or white. This noise spectrum gives us an idea of how (from a statistics standpoint) salt and pepper noise affects an image. Please note: when I refer to the “noise spectrum”, simply imagine a blank image (ie: every pixel value is zero), and then noise is applied to it.

So, now that we have a rough idea of what this kind of noise is, we need to figure out how to deal with it. One simple approach would be to apply a smoothing/blurring filter, which is covered in the previous section on convolution masks. Let’s try this out, and see what happens.

Salt and pepper noise removal attempted via blurring

Well, the results above are less than encouraging. The output image almost looks worse. Since blurring effectively calculates an output pixel’s value based on the values of the neighboring pixels in the source image, salt and pepper noise will not be affected much, if at all, by this filtering method. From a statistics standpoint, the introduction of extreme outliers into a data set will significantly affect the average/mean value of the data set.

What we need is an approach that considers the neighboring pixels in the source image, and ignores outliers. If we think back to simple statistics, one method should stick out above the others: calculating the median. For those who don’t recall what the median is, consider a a set of numbers. Now sort the numbers (ascending or descending, either way works). Now find the value in the exact middle of this sorted list. There is your median value. When introducing extreme values to a data set, the median is rarely affected at all, as the extreme values will remain at either of the two extrema of the sorted data set.

So, let’s try filtering this image again. We will use a median mask, which is a non-linear filter. Think of it like a convolution mask from the previous section, but instead of calculating a sum of products for each overlapping section of the input image and the convolution mask, we will instead calculate the median value. So, for a 3×3 median filter mask, we would have nine (9) pixel values from the source image which we would arrange into a 1-dimensional list, sort numerically, then pick the value in the middle of this list, and assign its value to the appropriate pixel in the output image buffer. A simple recap with respect to convolution masks is shown below.

Now, let’s try the median filter on the previous example:

Noise removal of 25% salt and pepper noise via a 3×3 median filter mask

The results are far better than when we attempted noise removal via blurring. Now let’s increase the noise density to 50%.

Noise removal of 50% salt and pepper noise via a 3×3 median filter mask

Well, the result isn’t half bad, considering that half of all the pixels in the source image have been randomly been set to a value of black or white. We can still do better though. Let’s increase the size of the median filter mask to 5×5, so that each pixel in the output image is calculated based on 25 pixels from the input image.

Noise removal of 50% salt and pepper noise via a 5×5 median filter mask

Now nearly all of the salt and pepper noise has been removed, but the output image has been distorted considerably. This is an important consideration to take into place when using median filters for this purpose: you will have to find a trade-off between how much noise you want removed, and how much distortion is acceptable in the final result. The functions I wrote to create salt and pepper noise, as well as the median filtering function, are included below. You can use them to apply varying amounts of noise, and to test out median filters of arbitrary sizes (3×3, 5×5, 11×11, etc).

Salt and Pepper Noise Generation:

Median Filter:

## 3 thoughts on “I – Salt & Pepper Noise – Non-Linear Filtering”

1. this is the sex.
very concise.
Im guessing you can create some plugins for Paint.NET with this kind of knowledge

• Absolutely. I’ve made every effort to break down the details to the point where the MATLAB code could be easily ported to C, even assembly if need be.