OpencV getGaborKernel() Method
Last Updated :
24 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 cv2
import numpy as np
import matplotlib.pyplot as plt
ksize = 31
sigma = 5
theta_range = np.arange( 0 , np.pi, np.pi / 4 )
frequency = 0.3
phase = 0
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 | os.getenvb() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.getenvb() method in Python is bytes version of os.getenv() method. This method
3 min read
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
Feature Matching in OpenCV
OpenCV feature matching is a super cool technology in computer vision that's changing how machines understand the visual world. It's super important in things like image search, object recognition, image stitching, and making pictures look better. If you want to level up your image analysis, classif
8 min read
Python OpenCV | cv2.arrowedLine() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.arrowedLine() method is used to draw arrow segment pointing from the start point to the end point. Syntax: cv2.arrowedLine(image, start_point, end_point, color, thickness, line_type, shift, tipLength)Parame
3 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
Python PIL | getpixel() Method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The PixelAccess class provides read and write access to PIL.Image data at a pixel level. Accessing individual pixels is fairly slow. If you are looping over all of the pixels in an image, there i
2 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: [GFGTABS] Python imp
2 min read
Python OpenCV | cv2.circle() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.circle() method is used to draw a circle on any image. The syntax of cv2.circle() method is: Syntax: cv2.circle(image, center_coordinates, radius, color, thickness) Parameters: image: It is the image on whi
3 min read
Python OpenCV â cv2.flip() method
OpenCV-Python is a library of programming functions mainly aimed at real-time computer vision. cv2.flip() method is used to flip a 2D array. The function cv::flip flips a 2D array around vertical, horizontal, or both axes. Syntax: cv2.cv.flip(src, flipCode[, dst] )Parameters: src: Input array.dst: O
1 min read