Skip to content

Instantly share code, notes, and snippets.

@lusingander
Created February 28, 2019 05:26
Show Gist options
  • Save lusingander/5fdd0ca7c0541fa6a06620b8d3caacc6 to your computer and use it in GitHub Desktop.
Save lusingander/5fdd0ca7c0541fa6a06620b8d3caacc6 to your computer and use it in GitHub Desktop.
Gabor Filter (OpenCV/Python)
import cv2
import numpy as np
class CvTrackbar:
def __init__(self, name, window_name, val, max_val, callback=None):
self.__name = name
self.__window_name = window_name
cv2.createTrackbar(name, window_name, val, max_val, callback or CvTrackbar.nothing)
def value(self):
return cv2.getTrackbarPos(self.__name, self.__window_name)
@classmethod
def nothing(v=None, u=None):
pass
def create_gabor_bgr_image(gabor, size):
gabor_image = gabor.copy()
cv2.normalize(gabor, gabor_image, 0, 255, cv2.NORM_MINMAX)
gabor_image = gabor_image.astype(np.uint8)
gabor_image = cv2.cvtColor(gabor_image, cv2.COLOR_GRAY2BGR)
gabor_image = cv2.resize(gabor_image, size)
return gabor_image
WINDOW_TITLE = 'gabor'
GABOR_IMG_SIZE = (200, 200)
TARGET_FILENAME = 'sample.png'
img = np.zeros((1), np.uint8)
target_src = cv2.imread(TARGET_FILENAME)
cv2.namedWindow(WINDOW_TITLE)
angle_tb = CvTrackbar('angle', WINDOW_TITLE, 0, 180)
while True:
cv2.imshow(WINDOW_TITLE, img)
ksize = (10, 10)
sigma = 2.0
theta = (np.pi / 180) * angle_tb.value()
lambd = 10
gamma = 0.5
gabor = cv2.getGaborKernel(ksize, sigma, theta, lambd, gamma)
target_image = cv2.filter2D(target_src, -1, gabor)
gabor_image = create_gabor_bgr_image(gabor, GABOR_IMG_SIZE)
target_image = cv2.resize(target_image, None, fx=0.5, fy=0.5)
gabor_area_image = np.zeros((target_image.shape[0], GABOR_IMG_SIZE[1], 3), np.uint8)
gabor_area_image[:GABOR_IMG_SIZE[0], :GABOR_IMG_SIZE[1]] = gabor_image
img = cv2.hconcat([gabor_area_image, target_image])
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
@olawalejuwonm
Copy link

Please can you share the sample image file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment