How to do it...

Perform the following steps:

  1. Import the packages:
import mathimport cv2import numpy as npimport 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') ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.