OpencV getGaborKernel() Method
Last Updated : 28 Apr, 2025
In this article, we will learn about cv2.getGaborKernel() for various rotations in Python.
Gabor filters are one of the most essential tools in Computer Vision. Gabor filters help in texture analysis, edge detection, and feature extraction. A Gabor kernel is a type of bandpass filter that is used to analyze images in relation to the frequency of content. The Gabor kernel consists of a Gaussian envelope multiplied by a sinusoidal wave. The Gaussian envelope controls the size and shape of the kernel. Sinusoidal wave controls their orientation and frequency.
Cv2.getGaborKernel()
The cv2.getGaborKernel() function in the OpenCV library provides a straightforward way to generate Gabor kernels with a variety of parameters. This function takes various parameters such as kernel size, sigma, frequency, orientation, and phase.
Syntax of Cv2.getGaborKernel() function:
cv2.getGaborKernel(ksize, sigma, theta, lambd, gamma, psi=0, ktype=cv2.CV_32F)
Parameters:
- ksize: It is the size of the filter kernel (width, height) in pixels. This can be a tuple or a single integer value.
- sigma: Standard deviation of the Gaussian envelope.
- theta: Orientation of the filter in degrees.
- lambd: Wavelength of the sinusoidal factor.
- gamma: Spatial aspect ratio (ellipticity) of the filter.
- psi: Phase offset of the filter in degrees. This is an optional parameter and its default value is 0.
- ktype: Data type of the filter coefficients. This is an optional parameter and its default value is cv2.CV_32F, which represents a 32-bit floating-point data type.
Generating various rotation using cv2.getGaborKernel() in Python,
Use the orientation parameter to generate Gabor kernels with different rotations. The orientation parameter in the cv2.getGaborKernel() function rotates the Gabor kernel. By changing the orientation parameter, we get various orientations. Its values are in radians, from 0 to π. A loop with varying orientation values can create Gabor kernels with different orientations. We can then use the cv2.getGaborKernel() function to generate Gabor kernels for each orientation value.
Example:
Firstly we import the required libraries cv2, NumPy, and matplotlib.pyplot. Defines all the values of parameters in Cv2.getGaborKernel() function. The loop defined in the code is looping through each orientation value in theta_range and generates a corresponding Gabor kernel using the cv2.getGaborKernel() function. The generated kernel is displayed using matplotlib.pyplot.imshow().
Colormap is set to grayscale with cmap='gray', and the title of the displayed image includes orientation value using plt.title(). The image is shown in a separate window by calling plt.show(). This will output different Gabor kernels with various orientations.
Python3 # Import required libraies import cv2 import numpy as np import matplotlib.pyplot as plt # kernel size ksize = 31 # sigma for Gaussian envelope sigma = 5 # range of orientation values theta_range = np.arange(0, np.pi, np.pi / 4) # frequency of sinusoidal wave frequency = 0.3 # phase of sinusoidal wave phase = 0 # for loop for generating different rotation for theta in theta_range: kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, frequency, phase) plt.imshow(kernel, cmap='gray') plt.title("Kernel with orientation = " + str(theta)) plt.show()
Output:
In the above code, we can see how the orientation parameter of the cv2.getGaborKernel() function affects the rotation angle of the Gabor kernel. We can use this approach to generate Gabor kernels with different orientations for our various image-processing applications.
Conclusion
The cv2.getGaborKernel() function is a powerful tool in computer vision for generating Gabor kernels. By varying the orientation parameter, we can generate Gabor kernels with different rotations, which can be used in various image processing applications.
Technical Vocabulary
- Bandpass filters block the things which we do not want to see or hear. It helps us see only specific things in a picture or a sound.
- Gaussian envelope gives a soft to our image blur that helps to make things look smoother and less pixelated. It is used for smoothing out edges in pictures or making text easier to read.
- Sinusoidal wave is a wavy line that goes up and down in a regular pattern. It is used in many signal-processing applications, like encoding and decoding information, or creating patterns in images.
- A kernel is like a small matrix that helps to modify an image or a signal. In the Gabor kernel, a kernel is a special matrix that combines a Gaussian envelope with a sinusoidal wave. It is used in image processing applications to detect edges and highlight texture features in an image.
Similar Reads
Python OpenCV | cv2.copyMakeBorder() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.copyMakeBorder() method is used to create a border around the image like a photo frame.  Syntax: cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value) Parameters: src: It is the source image
2 min read
Python OpenCV | cv2.blur() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.blur() method is used to blur an image using the normalized box filter. The function smooths an image using the kernel which is represented as: Syntax: cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) Pa
2 min read
Python OpenCV | cv2.cvtColor() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV. We will use some of color space conversion codes be
4 min read
cv2.imread() method - Python OpenCV OpenCV-Python is a Python library used to solve computer vision tasks. cv2.imread() method loads an image from the specified file. If the image cannot be read because of missing file, improper permissions or an unsupported/invalid format then it returns an empty matrix.Example:Pythonimport cv2 image
2 min read
Python OpenCV - getgaussiankernel() Function 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 w
3 min read