In the past two parts of this section, we’ve covered two types of filtering so far: linear, and non-linear. In the case of linear filtering, we applied convolution masks to an image to blur it. This type of filter basically calculates a sum-of-products using a filter mask and a source image, which is a linear operation. In the case of non-linear filtering, we used median filtering, which takes a neighborhood of pixels, calculates the median value, and uses that value in the final result. This is obviously a non-linear operation.

Now we will cover a sort of hybrid model: adaptive filtering. One problem we have encountered with attempting to filter out salt & pepper noise and Gaussian noise is that when we are dealing with very “noisy” images, we tend to require larger filter masks to remove such large quantities of image noise. Although we can use arbitrarily large masks, the end result is that the larger the mask we use, the poorer the quality of the output image. What we would like is a means to determine if a pixel in a noisy image has been affected by the introduction of noise, and performing filtering on a conditional basis.

Although adaptive filters are a subject all of their own, I will present a simple adaptive filter I have designed for the removal of salt & pepper noise from an image. First, consider the example below, where I remove a considerable amount of salt & pepper noise from an image (20% added noise) using a 3×3 median filter.

20% Salt & pepper noise removal – 3×3 static median mask

The error vector/image above makes it clear that the filtered image is distorted compared to the original image. Most notably, it appears blurry, with sharp edges and fine details having been removed by some degree. Now, let’s make a simple modification to the code for a simple median filter. For each pixel in the source image, it will assess whether or not the pixel is considered to be a noisy pixel according to a simple contrast checking algorithm.

This algorithm will attempt to determine if the pixel under consideration is a statistical outlier compared to its surrounding/neighboring pixels. If it is considered to be a noisy pixel, then a median filter is applied to the neighborhood of this pixel, and the median value is used for the result to be stored in the output image buffer. If the pixel is not considered to be noisy, it is simply copied directly into the output image buffer, with no median mask applied at all.

The images below will demonstrate this algorithm in action, and compare the quality of its output compared to that of a simple median filter.

As we can see above, the adaptive method removes a significant amount of noise, without blurring the image. The error value (as shown in the title for the error vector/image) also decreases significantly (by nearly three-fourths) by using an adaptive filter over a static one. For completeness, I will perform this experiment again, but with 50% salt & pepper noise, and filtering using static/adaptive 7×7 median filters.

50% Salt & pepper noise removal – 7×7 static median mask

Notice how blurry the de-noised/cleaned image is after applying the static 7×7 median mask. All the fine lines, edges, and details have been lost.