上QQ阅读APP看书,第一时间看更新
How to do it...
Perform the following steps:
- Import the packages:
import math
import cv2
import numpy as np
import matplotlib.pyplot as plt
- Read the test image as grayscale and convert it to np.float32:
image = cv2.imread('../data/Lena.png', 0).astype(np.float32) / 255
- 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())
- Filter the image:
filtered = cv2.filter2D(image, -1, kernel)
- 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()