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
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
1 2 3 4 |
cv2.getGaussianKernel(ksize, sigma[, ktype]) # ksize - kernel size, should be odd and positive (3,5,...) # sigma - Gaussian standard deviation. If it is non-positive, it is computed from ksize as sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8 # ktype - Type of filter coefficients (Optional) |
To apply Gaussian blurring, use
1 2 3 |
cv2.GaussianBlur(input_img, ksize, sigmaX[, sigmaY[, borderType]]]) # sigmaX - standard deviation in X direction # sigmaY - Optional,if sigmaY is zero, it is set to be equal to sigmaX |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import cv2 img = cv2.imread('D:/downloads/opencv_logo.PNG') # Creates a 1-D Gaussian kernel a = cv2.getGaussianKernel(5,1) # Apply the above Gaussian kernel. Here, I # have used the same kernel for both X and Y b = cv2.sepFilter2D(img,-1,a,a) # Display the Image cv2.imshow('a',b) cv2.waitKey(0) |
The second method is quite easy to use. Just one line as shown below
1 2 3 4 5 |
import cv2 img = cv2.imread('D:/downloads/opencv_logo.PNG') # Apply the Gaussian blur c = cv2.GaussianBlur(img,(5,5),1) |
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.