Gaussian Blurring

In the previous blog, we discussed smoothing filters. In this article, we will discuss another smoothing technique known as Gaussian Blurring, that uses a low pass filter whose weights are derived from a Gaussian function. This is perhaps the most frequently used low pass filter in computer vision applications. We will also discuss various properties of the Gaussian filter that makes the algorithm more efficient. So, let’s get started with a basic background introduction.

We already know that a digital image is obtained by sampling and quantizing the continuous signal. Thus if we were to interpolate a pixel value, more chances are that it resembles that of the neighborhood pixels and less on the distant pixels. Similarly while smoothing an image, it makes more sense to take the weighted average instead of just averaging the values under the mask (like we did in Averaging).

So, we should look for a distribution/function that assigns more weights to the nearest pixels as compared to the distant pixels. This is the motivation for using Gaussian distribution.

A 2-d Gaussian function is obtained by multiplying two 1-d Gaussian functions (one for each direction) as shown below

2-d Gaussian function with mean=0 and std. deviation= σ

Now, just convolve the 2-d Gaussian function with the image to get the output. But for that, we need to produce a discrete approximation to the Gaussian function. Here comes the problem.

Because the Gaussian function has infinite support (meaning it is non-zero everywhere), the approximation would require an infinitely large convolution kernel. In other words, for each pixel calculation, we will need the entire image. So, we need to truncate or limit the kernel size.

For Gaussian, we know that 99.3% of the distribution falls within 3 standard deviations after which the values are effectively close to zero. So, we limit the kernel size to contain only values within 3σ from the mean. This approximation generally yields a result sufficiently close to that obtained by the entire Gaussian distribution.

Note: The approximated kernel weights would not sum exactly 1 so, normalize the weights by the overall kernel sum. Otherwise, this will cause darkening or brightening of the image.

A normalized 3×3 Gaussian filter is shown below (See the weight distribution)

Later we will see how to obtain different Gaussian kernels. Now, let’s see some interesting properties of the Gaussian filter that makes it efficient.

Properties

  • First, the Gaussian kernel is linearly separable. This means we can break any 2-d filter into two 1-d filters. Because of this, the computational complexity is reduced from O(n2) to O(n). Let’s see an example
  • Applying multiple successive Gaussian kernels is equivalent to applying a single, larger Gaussian blur, whose radius is the square root of the sum of the squares of the multiple kernels radii. Using this property we can approximate a non-separable filter by a combination of multiple separable filters.
  • The Gaussian kernel weights(1-D) can be obtained quickly using the Pascal’s Triangle. See how the third row corresponds to the 3×3 filter we used above.

Because of these properties, Gaussian Blurring is one of the most efficient and widely used algorithm. Now, let’s see some applications

Applications

  • Computer Graphics
  • Before edge detection (Canny Edge Detector)
  • Before down-sampling an image to reduce the ringing effect

Now let’s see how to do this using OpenCV-Python

OpenCV-Python

OpenCV provides an inbuilt function for both creating a Gaussian kernel and applying Gaussian blurring. Let’s see them one by one.

To create a Gaussian kernel of your choice, you can use

To apply Gaussian blurring, use

This first creates a Gaussian kernel and then convolves it with the image.

Now, let’s take an example to implement these two functions. First, use the cv2.getGaussianKernel() to create a 1-D kernel. Then use the cv2.sepFilter() to apply these kernels to the input image.

The second method is quite easy to use. Just one line as shown below

Both these methods produce the same result but the second one is more easy to implement. Try using this for a different type of noises and compare the results with other techniques.

That’s all about Gaussian blurring. Hope you enjoy reading. In the next blog, we will discuss Bilateral filtering, another smoothing technique that preserves edges also.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

2 thoughts on “Gaussian Blurring

  1. tony

    hi i really like your website.
    it’s very helpful!!
    i have some questions i don’t wanna use
    GaussianBlur , how can i make this method to own function .
    thank you for helping!!

    Reply

Leave a Reply