How Image Filtering is used to Improve Picture Quality

Image Filtering is used everywhere nowadays. Right from social media applications and news articles to various training models to improve image data. It can be used to remove noise from images as well as smoothen edges.

To understand image filtering, let us first understand image blurring. Starting with Gaussian Blur

Gaussian Blur

In image processing, a Gaussian blur (also known as Gaussian smoothing) is the result of blurring an image by a Gaussian function. The name is derived from the mathematician and scientist Carl Friedrich Gauss and is also known as Gaussian Blurring.

As I stated before, the Gaussian blur is a type of image-blurring filter that uses a Gaussian function (which also expresses the normal distribution in statistics) for calculating the transformation to apply to each pixel in the image. The formula of a Gaussian function in one dimension is

In two dimensions, it is the product of two such Gaussian functions, one in each dimension:

where x is the distance from the origin in the horizontal axis, y is the distance from the origin in the vertical axis, and σ is the standard deviation of the Gaussian distribution. When applied in two dimensions, this formula produces a surface whose contours are concentric circles with a Gaussian distribution from the centre point.

In order to save computational time and power while applying gaussian blur on 2-D images, instead of applying the gaussian matrix directly on the 2-D matrix, it is first applied in one dimension horizontally and then a series of 1-D gaussian matrices are applied vertically. In computational terms, this is a useful property, since the calculation can be performed in O(w(kernel)*w(Image)*h(Image)) + O(h(kernel)*w(Image)*h(Image)) time, as opposed to O(w(kernel)*h(kernel) * w(Image)*h(Image)) for a non-separable kernel. (h is height and w is width)

Gaussian blurring is commonly used when reducing the size of an image.

Box Blur

Box blur is also known as box linear filter. Box blurs are frequently used to approximate Gaussian blur.

A box blur is generally implemented as an image effect that affects the whole screen. The blurred colour of the current pixel is the average of the current pixel’s colour and its 8 neighbouring pixels. Here, the weight is constant and lies within a square (box).

It can be derived from this formula:

br denotes the box radius, which is half the size of the square.

So, if we had to show the box blur visually it would look like this :

In order to implement Box Blur we can use the openCV function boxFilter()

The above code yields the following results:

Min and Max Filters

The Minimum filter enhances ‘minimum’ values in the image by increasing its area. Similar to a dilate function each 3x3 (or other window size) is processed for the darkest surrounding pixel. That darkest pixel then becomes the new pixel value at the centre of the window.

Here is a visualisation:

We can implement it is using the PIL library as follows

Similarly, when we enhance the ‘highest’ or ‘maximum’ values of the image, it is called maximum filtering.

An interesting use case of minimum filtering is in astrophotographical images. Many wide field deep sky images have so many, so small, and so bright stars, that non-stellar objects as diffuse nebulae and Milky Way condensations, can be greatly obscured by them. A modulated minimum filter (Amount parameter less than 1) can be applied in these cases to decrease the visual impact of stars, giving non-stellar objects their deserved importance in the scene.

Median and Mode Filtering

Median and Mode filtering are also very useful tools for noise removal. As you probably guessed, it takes the median and mode respectively of the square sections of the images taken.

The code above yields the following results:

Similarly, we can implement a mode Filter using the PIL library:

Use Case: Pepper and Salt Noise Removal

Now that I have explained various types of filtering/blurring, let us try to understand which one is the best to use in various situations.

Let’s take a use case of removing salt and pepper noise.

The code above will take the original image and add salt and pepper noise to it. After adding the noise, we have used various filtering techniques to discover which one achieves best results.

Now using matplotlib.pyplot() , we have been able to see the results of the various images as follows:

Hence, we can see that the best results are achieved from median filtered image.

This is one of many ways in which filtering is used for noise removal and to improve image quality. Various filtering techniques can be used to remove various kinds of noise from images.

References:

Data Science and Machine Learning Enthusiast