我需要一个Gabor变换函数
时间: 2023-05-22 07:02:26 浏览: 102
以下是一个可以实现 Gabor 变换的函数:
```python
import numpy as np
import cv2
def gabor_transform(img, ksize, sigma, theta, lambd, gamma, psi):
"""
Applies a Gabor filter to an image.
Args:
img (ndarray): Input image.
ksize (tuple): Kernel size (width, height).
sigma (float): Standard deviation of the Gaussian envelope.
theta (float): Orientation of the Gabor filter in radians.
lambd (float): Wavelength of the sinusoidal factor.
gamma (float): Spatial aspect ratio.
psi (float): Phase offset.
Returns:
ndarray: Filtered image.
"""
ksize_half = int(ksize[0] / 2)
# Generate kernel
x, y = np.meshgrid(np.arange(-ksize_half, ksize_half + 1), np.arange(-ksize_half, ksize_half + 1))
x_theta = x * np.cos(theta) + y * np.sin(theta)
y_theta = -x * np.sin(theta) + y * np.cos(theta)
kernel = np.exp(-0.5 * (x_theta ** 2 + gamma ** 2 * y_theta ** 2) / sigma ** 2) * np.cos(2 * np.pi * x_theta / lambd + psi)
# Normalize kernel
kernel = kernel - np.mean(kernel)
kernel = kernel / np.std(kernel)
# Apply filter
filtered_img = cv2.filter2D(img, cv2.CV_32F, kernel)
return filtered_img
```
你可以根据需要调整输入参数来进行滤波。
阅读全文