Gaussian Blurring

As the name suggest, this type of blurring uses a Gaussian kernel to smooth the input image. OpenCV implements Gaussian filtering with the function GaussianBlur(). The signature of this method is shown as below:

cv2.GaussianBlur(src, ksize, sigmaX[, sigmaY[, borderType]]])

The parameters of the above function are described as below:

Parameter Description

src

input image; it can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.

ksize

Neighborhood size. It means how many pixels around the central pixels will be used to perform blurring of central pixel.

sigmaX

Gaussian kernel standard deviation in X direction.

sigmaY

Gaussian kernel standard deviation in Y direction.

borderType

Border mode used to extrapolate pixels outside of the image.

Just like the blur() function in OpenCV, we also need to specify the kernel size as parameter. As mentioned earlier, the kernel size should be an odd value. We also should specify the standard deviation in the X and Y directions, sigmaX and sigmaY respectively. If only sigmaX is specified, sigmaY is taken as the same as sigmaX. If both are given as zeros, they are calculated from the kernel size. Gaussian blurring is highly effective in removing Gaussian noise from an image.

In OpenCV, we can create a Gaussian kernel using getGaussianKernel() function which can be used in the GaussianBlur() function to blur the input image. Code 1-2 shows application of Gaussian blurring to smooth the input image.

Let us look at an example that blurs an image using Gaussian blurring. Code 1-1 shows an example of using GaussianBlur() function to smooth (blur) an input image.

Code 1-1 Gaussia Blur
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import cv2
import numpy as np
try:
    path = r'F:\img\lena.jpg'
    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) 
    imgBlur =  cv2.GaussianBlur(img,(9,9),0)
    cv2.imshow('Original Image',(img))
    cv2.imshow('Gaussian Blur',imgBlur)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
except Exception as e:
    print(str(e))

Line 1 and 2: We start by importing OpenCV and numpy libraries.
Line 3: This line defines a try block. The exception block (Line 11-12) is associated with this block. If any exception occurs, it throws it and code in exception block is executed.
Line 4: Define path of the image to read.
Line 5: The image is read. Note that we are reading a grayscale image (see 2nd parameter).
Line 6: Using OpenCV’s GaussianBlur() function the input image is blurred. The result is stored in imgBlur variable. We used the kernel size of (9,9) and SigmaX=0. For the rest of the parameters, their default values are used as they are optional. You can tweak around the parameters to see the impact on the output.
Line 7-8: It displays both blurred and original image in a separate window.
Line 9: waitKey(0) function makes the window wait until user presses any keyboard button.
Line 10: Object of all the windows opened will be destroyed and closed.

The output obtained for the code shown above is shown in Figure 1-1.

No image
Figure 1-1: Original image(left), Gaussian blurred image (right).

OpenCV Tutorials