Python OpenCV - getgaussiankernel() Function - GeeksforGeeks

19 Dec, 2021
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 cv2import cv2  # read imageimg = cv2.imread('gfg2.jpg')  # Creates a 1-D Gaussian kernela = cv2.getGaussianKernel(3, 1)  # print Gaussian filter coefficients matrixprint(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 cv2import cv2  # read imageimg = cv2.imread('gfg_logo.png')  # Creates a 1-D Gaussian kernela = cv2.getGaussianKernel(7, 1)  # print Gaussian filter coefficients matrixprint(a)
Output:
[[0.00443305]
 [0.05400558]
 [0.24203623]
 [0.39905028]
 [0.24203623]
 [0.05400558]
 [0.00443305]]
Picked
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()