Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gabor kernel filter example in python
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

This comment has been minimized.

Copy link

@epicure24 epicure24 commented Mar 9, 2019

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)

@styx97

This comment has been minimized.

Copy link

@styx97 styx97 commented Jul 8, 2019

@epicure24 you're right, got it to work after that change.

@dipayandas97

This comment has been minimized.

Copy link

@dipayandas97 dipayandas97 commented Mar 8, 2020

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.

@intsynko

This comment has been minimized.

Copy link

@intsynko intsynko commented Mar 2, 2021

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