How to do it...

Perform the following steps:

  1. Import the packages:
import math
import cv2
import numpy as np
import matplotlib.pyplot as plt
  1. Read the test image as grayscale and convert it to np.float32:
image = cv2.imread('../data/Lena.png', 0).astype(np.float32) / 255
  1.  Construct the real-valued Gabor filter kernel. Normalize the kernel in such a way that it has an L2 unit norm:
kernel = cv2.getGaborKernel((21, 21), 5, 1, 10, 1, 0, cv2.CV_32F)
kernel /= math.sqrt((kernel * kernel).sum())
  1. Filter the image:
filtered = cv2.filter2D(image, -1, kernel)
  1. Visualize the results:
plt.figure(figsize=(8,3))
plt.subplot(131)
plt.axis('off')
plt.title('image')
plt.imshow(image, cmap='gray')
plt.subplot(132)
plt.title('kernel')
plt.imshow(kernel, cmap='gray')
plt.subplot(133)
plt.axis('off')
plt.title('filtered')
plt.imshow(filtered, cmap='gray')
plt.tight_layout()
plt.show()