Python OpenCV Gaussian Blur Filtering
In this article, you will learn about the image noises, different techniques to filter them, especially about the Python OpenCV Gaussian Blur filter and how this is useful to filter image.
Python OpenCV has several filtering techniques to perform smoothing operations on images, like - Gaussian Filtering, Median Filtering, Bilateral Filtering. Images can contain different types of noise, especially because of camera sensor. These smoothing techniques are generally used to reduce noise, reduce detail, and so on.
At the point when individuals see a picture, we see what it represents. In the given image, we see a beautiful nature scene. If we notice it all the more carefully, we will see it contain lot of color. But, a computer looks this differently than people does. From a computational point of view, this image is just a large 2-dimensional matrix of numbers. Each number speaks to the color of a pixel. The computer can deal with these 2-D matrices by applying different functions to them to adjust the qualities.
Here, we have used 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 utilized as a preprocessing stage prior to applying our AI or deep learning models.
The OpenCV Gaussian filtering provides cv2.GaussianBlur() method to blur an image by using Gaussian Kernel. Each pixel in an image gets multiplied by 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), the width and height can have different values and must be positive and odd,
- sigma_x - Gaussian kernel standard deviation along X-axis,
- dst - output image,
- sigma_y - Gaussian kernel standard deviation along 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 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 ArticlesPython OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
How to convert MySQL query result to JSON in Python
How to display data from MongoDB in HTML table using Python
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python get visitor information by IP address
Python Tkinter Overview and Examples
Python Turtle Graphics Overview