Python cv2: Filtering Image using GaussianBlur() Method
Image filtering functions are often used to pre-process or adjust an image before performing more complex operations. These operations help reduce noise or unwanted variances of an image or threshold.
There are three filters available in the OpenCV-Python library.
- Gaussian Blur Filter
- Erosion Blur Filter
- Dilation Blur Filter
Image Smoothing techniques help us in reducing the noise in an image. In OpenCV, image smoothing (also called blurring) could be done in many ways. We will see the GaussianBlur() method in detail in this post.
The Gaussian Blur filter smooths the image by averaging pixel values with its neighbors. It’s called the Gaussian Blur because an average has the Gaussian falloff effect.
What that means is that pixels that are closer to a target pixel have a higher influence on the average than pixels that are far away. This is how the smoothing works. It is often used as a decent way to smooth out noise in an image as a precursor to other processing.
Python cv2 GaussianBlur()
OpenCV-Python provides the cv2.GaussianBlur() function to apply Gaussian Smoothing on the input source image.
cv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType)
|src||It is an input image.|
|dst||It is an output image.|
|ksize||It is a Gaussian Kernel Size. [height width]. The height and width should be odd and can have different values. If ksize is set to [0 0], then ksize is computed from the sigma values.|
|sigmaX||It is a kernel standard deviation along X-axis (horizontal direction).|
|sigmaY||It is a kernel standard deviation along Y-axis (vertical direction). If sigmaY=0, then sigmaX value is taken for sigmaY|
|borderType||Specifies image boundaries while the kernel is applied on image borders. Possible values are cv.BORDER_CONSTANT cv.BORDER_REPLICATE cv.BORDER_REFLECT cv.BORDER_WRAP cv.BORDER_REFLECT_101 cv.BORDER_TRANSPARENT cv.BORDER_REFLECT101 cv.BORDER_DEFAULT cv.BORDER_ISOLATED|
The cv2.GaussianBlur() method returns blurred image of n-dimensional array.
Write the following code that demonstrates the gaussianblur() method.
# app.py import numpy as np import cv2 img = cv2.imread('data.png', 1) cv2.imshow('Original', img) blur_image = cv2.GaussianBlur(img, (3, 33), 0) cv2.imshow('Blurred Image', blur_image) cv2.waitKey(0) cv2.destroyAllWindows()
You can see that the left one is an original image, and the right one is a gaussian blurred image.
In GaussianBlur() method, you need to pass the src and ksize values every time, and either one, two, or all parameters value from the remaining sigmaX, sigmaY, and borderType parameter should be passed.
Both sigmaX and sigmaY arguments become optional if you mention a ksize(kernel size) value other than (0,0).
Let’s use the GaussianBlur() method with src, size, and sigmaX parameters.
# app.py import numpy as np import cv2 img = cv2.imread('data.png', 1) cv2.imshow('Original', img) blur_image = cv2.GaussianBlur(img, (5, 5), 5) cv2.imshow('Blurred Image', blur_image) cv2.waitKey(0) cv2.destroyAllWindows()
You can similarly change the values of other parameters of the function and observe the outputs.
In cv2.GaussianBlur() method, instead of a box filter, a Gaussian kernel is used. We have to define the width and height of the kernel, which should be positive and odd, and it will return the blurred image.
That is it for the GaussianBlur() method of the OpenCV-Python library.