Python OpenCV - getgaussiankernel() Function
Last Updated :
20 Dec, 2025
The cv2.getGaussianKernel() function generates a 1-D Gaussian kernel, which is commonly used for smoothing and blurring operations in image processing. The kernel contains weights based on the Gaussian distribution, and these weights are used by filtering functions like GaussianBlur() or sepFilter2D() to soften image details.
Example: This example shows how to generate a Gaussian kernel of size 3 and print its coefficient values.
Python
import cv2
k = cv2.getGaussianKernel(3, 1)
print(k)
Output
[[0.27406862]
[0.45186276]
[0.27406862]]
Explanation: cv2.getGaussianKernel(3, 1) creates a 3×1 Gaussian kernel with σ=1.
Syntax
cv2.getGaussianKernel(ksize, sigma, ktype=cv2.CV_64F)
Parameters:
- ksize: Kernel size (must be odd & positive).
- sigma: Standard deviation of Gaussian.
- ktype: Output data type (CV_32F or CV_64F).
Examples
Example 1: This example generates a Gaussian kernel of size 5×1 and uses a custom sigma value to control the spread of the curve.
Python
import cv2
k = cv2.getGaussianKernel(5, 2.0)
print(k)
Output
[[0.15246914]
[0.2218413 ]
[0.25137912]
[0.2218413 ]
[0.15246914]]
Explanation: cv2.getGaussianKernel(5, 2.0) Wider, smoother kernel because of larger sigma.
Example 2: This example generates a slightly larger 7×1 Gaussian kernel to show how weights spread more smoothly.
Python
import cv2
k = cv2.getGaussianKernel(7, 1)
print(k)
Output
[[0.00443305]
[0.05400558]
[0.24203623]
[0.39905028]
[0.24203623]
[0.05400558]
[0.00443305]]
Explanation: getGaussianKernel(7, 1) produces a wider kernel that provides stronger smoothing.
Example 3: This example creates a Gaussian kernel with automatic sigma selection by passing sigma=0.
Python
import cv2
k = cv2.getGaussianKernel(5, 0)
print(k)
Output
[[0.0625]
[0.25 ]
[0.375 ]
[0.25 ]
[0.0625]]
Explanation: getGaussianKernel(5, 0) automatically computes sigma based on kernel size.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice