Python OpenCV Gaussian Blur Filtering
In this article, you will learn about image noise, different techniques to filter it, and especially about the Python OpenCV Gaussian Blur filter and how it is useful to filter images.
Python OpenCV has several filtering techniques to perform smoothing operations on images, like Gaussian Filtering, Median Filtering, and Bilateral Filtering. Images can contain different types of noise, especially because of the camera sensor. These smoothing techniques are generally used to reduce noise, reduce detail, and so on.
When individuals see a picture, they see what it represents. In the given image, we see a beautiful natural scene. If we notice it all the more carefully, we will see it contains a lot of colour. However, a computer's appearance differs from that of a human. From a computational point of view, this image is just a large 2-dimensional matrix of numbers. Each number speaks to the colour of a pixel. The computer can deal with these 2-D matrices by applying different functions to them to adjust their qualities.
Here, we have used the Gaussian filter. The Gaussian Filter is a low pass filter. The Gaussian smoothing (or blur) of an image removes the outlier pixels or the high-frequency components to reduce noise. It is likewise utilised as a preprocessing stage prior to applying our AI or deep learning models.
The OpenCV Gaussian filtering library provides the cv2.GaussianBlur() method to blur an image by using the Gaussian Kernel. Each pixel in an image gets multiplied by the Gaussian Kernel. It means, a Gaussian Kernel is a square array of pixels.
Syntax of GaussianBlur()
cv2.GaussianBlur(src, ksize, sigma_x, dst, sigma_y, border_type)
- src- the input image,
- ksize- Gaussian kernel size (width and height), where the width and height can have different values and must be positive and odd,
- sigma_x- Gaussian kernel standard deviation along the X-axis,
- dst- output image,
- sigma_y- Gaussian kernel standard deviation along the Y-axis,
- border_type- image boundaries. Possible values are- BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101, BORDER_TRANSPARENT, BORDER_REFLECT101, BORDER_DEFAULT, BORDER_ISOLATED
The size of the sigma specifies how wide the curve should be inside the kernel. If only sigma_x is specified, sigma_y is automatically taken as equal to sigma_x. If both are defined as zeros, they are calculated from the kernel size.
Example1 of Gaussian Blur
The given code blurs the image using the Gaussian kernel of size (5,5) and BORDER_DEFAULT as the border type.
import cv2
import numpy
# image path
path = r'nature.jpg'
# using imread()
img = cv2.imread(path)
dst = cv2.GaussianBlur(img,(5,5),cv2.BORDER_DEFAULT)
cv2.imshow('image', numpy.hstack((img, dst)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)
The above code returns blurred image of n-dimensional array in output.
Example2 of Gaussian Blur
The following line of code blurs the image using the Gaussian kernel of size (9,9) and BORDER_REFLECT_101 as the border type.
import cv2
import numpy
# image path
path = r'nature.jpg'
# using imread()
img = cv2.imread(path)
dst = cv2.GaussianBlur(img,(9,9),cv2.BORDER_REFLECT_101)
cv2.imshow('image', numpy.hstack((img, dst)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)
Output of the above code -
Related Articles
Python OpenCV Overview and ExamplesDraw different shapes on image using Python OpenCV
OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV Overlaying or Blending Two Images
Python OpenCV Histogram of Color Image
Arithmetic Operations on Images using Python OpenCV
Detect Specific Color From Image using Python OpenCV
Capture a video in Python OpenCV and save
Contour Detection using Python OpenCV
Python OpenCV ColorMap
Adaptive Thresholding in Python OpenCV
Geometric Transformation OpenCV Python
Find the stop words in nltk Python
Install NLTK for Python on Windows 64 bit
Eye Detection Program in Python OpenCV
Python Turtle Graphics Overview