Python OpenCV - getgaussiankernel() Function
Last Updated :
03 Jan, 2023
Python OpenCV getGaussianKernel() function is used to find the Gaussian filter coefficients. The Gaussian kernel is also used in Gaussian Blurring. Gaussian Blurring is the smoothing technique that uses a low pass filter whose weights are derived from a Gaussian function. In fact, this is the most widely used low pass filter in CV(computer vision) applications. Different properties of the gaussian filter make the algorithm more efficient.
Syntax: cv.getGaussianKernel(ksize, sigma[, ktype])
Parameters:
- Ksize: It is the Aperture size. Ksize value should be odd and positive.
- sigma: Sigma is the Gaussian standard deviation. It is computed from ksize as sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8 if it is non-positive.
- ktype: It is the type of filter coefficients. It can be CV_32F or CV_64F.
Returns:
matrix of Gaussian filter coefficients.
The getGaussianKernel() function computes and returns the matrix of dimension ksize×1 of Gaussian filter coefficients:
Gi=α∗e−(i−(ksize−1)/2)2/(2∗sigma2)
where i=0 to ksize−1 and α is the scale factor chosen so that ∑iGi=1.
Two of such generated kernels can be passed to sepFilter2D. Those passed functions automatically recognize smoothing kernels (symmetrical kernel with the sum of weights equal to 1) and handle them accordingly. You may also use the higher-level GaussianBlur. Here, one thing to remember is that k which is the aperture size should also be odd and positive.
So, you might have a question that what is the Gaussian kernel and why do we need it? The Gaussian kernel is linearly separable. This means we can break any 2 dimension filter into two 1 dimension filters. Because of this, the computational complexity is reduced from O(n2) to O(n). What time is what matters in computer science.
Applying multiple successive Gaussian kernels is the same as applying a single, larger Gaussian blur, whose radius is equal to the square root of the sum of the squares of the multiple kernels radius. Using this characteristic we can approximate a non-separable filter by a mixture of multiple separable filters. The Gaussian kernel weights(1-D) can be obtained quickly using Pascal’s Triangle.
Example 1:
Here, in the below example we will find the Gaussian kernel of one image. We first read the image using cv2. Then we create the Gaussian kernel of size 3×1 using getgaussiankernel() function. ksize which is the Aperture size is odd and positive.
The below image is used in this example:
Python3
# Python OpenCV - getgaussiankernel() Function
# import cv2
import cv2
# read image
img = cv2.imread('gfg2.jpg')
# Creates a 1-D Gaussian kernel
a = cv2.getGaussianKernel(3, 1)
# print Gaussian filter coefficients matrix
print(a)
Output:
[[0.27406862]
[0.45186276]
[0.27406862]]
Example 2:
In this example, we will find the Gaussian kernel of one image, we create the Gaussian kernel of size 7×1 using getgaussiankernel() function.
Python3
# Python OpenCV - getgaussiankernel() Function
# import cv2
import cv2
# read image
img = cv2.imread('gfg_logo.png')
# Creates a 1-D Gaussian kernel
a = cv2.getGaussianKernel(7, 1)
# print Gaussian filter coefficients matrix
print(a)
Output:
[[0.00443305]
[0.05400558]
[0.24203623]
[0.39905028]
[0.24203623]
[0.05400558]
[0.00443305]]
Similar Reads
Python OpenCV - getWindowImageRect() Function
Python OpenCV getWindowImageRect() Function returns the client screen coordinates, along with the width and height of the window containing the picture. Syntax of cv2.getWindowImageRect() Syntax: cv2.getWindowImageRect(window_name) Parameter:Â window_name - Name of the window displaying image/video
3 min read
Python OpenCV - imencode() Function
Python OpenCV imencode() function converts (encodes) image formats into streaming data and stores it in-memory cache. It is mostly used to compress image data formats in order to make network transfer easier. Basic example of imencode() FunctionExample 1: We began by importing the necessary librarie
2 min read
Python OpenCV - haveImageReader() function
In this article, we are going to learn about the haveImageReader() function of the OpenCV library. The haveImageReader() function is used to check whether specified images can be decoded or read successfully by OpenCV or not. Sometimes we need to detect if the specified image file is being correctl
1 min read
Python OpenCV - haveImageWriter() function
In this article, we are going to learn about the haveImageWriter() function of the OpenCV library. haveImageWriter() function Sometimes we need to detect if the specified image file is being correctly written or not before continuing further, In such a case we can use OpenCV which helps us to proces
2 min read
Python OpenCV - getRotationMatrix2D() Function
cv2.getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image.Syntax: cv2.getRotationMatrix2D(center, angle, scale)Parameters: center: Center of rotationangle(θ): The angle of rotation in degrees. A positive value rotates the image anti-clockw
3 min read
Python OpenCV - namedWindow() Function
Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen. Created windows are referred by their names and
3 min read
OpenCV - imdecode() Function in Python
cv2.imdecode() function is used to read image data from a memory cache and convert it into image format. This is generally used for loading the image efficiently from the internet. Example: Decoding and Saving an Image from URL in ColorThis example demonstrates how to download an image from a URL, d
2 min read
Python OpenCV - Canny() Function
Canny edge detection algorithm is used in computer vision for identifying edges within an image. It helps in highlighting boundaries which are important for tasks like object detection and image segmentation. In this article, we will see how OpenCV's built-in Canny() function detects edges in an ima
3 min read
Python OpenCV - Drowsiness Detection
The number of fatalities on the road due to drowsiness is very high. Thus python allows the model of deep learning algorithm via including the use of OpenCV. Drowsiness Detection is the detection of a person to check whether the person is feeling sleepy while performing a significant task. This dete
9 min read
Python OpenCV - waitKey() Function
waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. Examples 1: Disp
1 min read