Open In App

OpencV getGaborKernel() Method

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Using cv2.getGaborKernel() for various rotations

 

Using cv2.getGaborKernel() for various rotations

 

Using cv2.getGaborKernel() for various rotations

 

Using cv2.getGaborKernel() for various rotations

 

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads