Gabor kernel filter example in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
# cv2.getGaborKernel(ksize, sigma, theta, lambda, gamma, psi, ktype) | |
# ksize - size of gabor filter (n, n) | |
# sigma - standard deviation of the gaussian function | |
# theta - orientation of the normal to the parallel stripes | |
# lambda - wavelength of the sunusoidal factor | |
# gamma - spatial aspect ratio | |
# psi - phase offset | |
# ktype - type and range of values that each pixel in the gabor kernel can hold | |
g_kernel = cv2.getGaborKernel((21, 21), 8.0, np.pi/4, 10.0, 0.5, 0, ktype=cv2.CV_32F) | |
img = cv2.imread('test.jpg') | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
filtered_img = cv2.filter2D(img, cv2.CV_8UC3, g_kernel) | |
cv2.imshow('image', img) | |
cv2.imshow('filtered image', filtered_img) | |
h, w = g_kernel.shape[:2] | |
g_kernel = cv2.resize(g_kernel, (3*w, 3*h), interpolation=cv2.INTER_CUBIC) | |
cv2.imshow('gabor kernel (resized)', g_kernel) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
@epicure24 you're right, got it to work after that change.
I think instead of
g_kernel = cv2.resize(g_kernel, (3_w, 3_h), interpolation=cv2.INTER_CUBIC)
there should be this
g_kernel = cv2.resize(filtered_img, (3_w, 3_h), interpolation=cv2.INTER_CUBIC)
He is trying to visualize the gabor filter itself by enlarging the 21x21 filter. I guess he has written it quite correct though.
You example is very simple, but very understandable. I'm improve this example to interactive dashboard, mayby it will be helped for someone: https://github.com/Kostya-228/gabor_dashboard/tree/main
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think instead of
g_kernel = cv2.resize(g_kernel, (3w, 3h), interpolation=cv2.INTER_CUBIC)
there should be this
g_kernel = cv2.resize(filtered_img, (3w, 3h), interpolation=cv2.INTER_CUBIC)