python实现Gabor滤波器

Gabor 函数表示

复数表示:

Gabor函数复数表示

实数部分:

Gabor函数实数部分

虚数部分:

Gabor函数虚数部分

其中:

Gabor函数 x' 、y' 计算公式

代码中参数和Gabor函数参数对应关系

代码实现了Gabor滤波器的实数部分。代码中参数和Gabor函数实数部分参数对应如下:
python实现Gabor的核心代码

对应

Gabor实数部分
看上去是不是感觉很容易实现Gabor滤波器,那我来实现一下,并使用Gabor滤波器来提取图像特征。

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Grayscale
def BGR2GRAY(img):
	# Grayscale
	gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
	return gray

# Gabor Filter
def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0):
	# get half size
	d = K_size // 2

	# prepare kernel
	gabor = np.zeros((K_size, K_size), dtype=np.float32)

	# each value
	for y in range(K_size):
		for x in range(K_size):
			# distance from center
			px = x - d
			py = y - d

			# degree -> radian
			theta = angle / 180. * np.pi

			# get kernel x
			_x = np.cos(theta) * px + np.sin(theta) * py

			# get kernel y
			_y = -np.sin(theta) * px + np.cos(theta) * py

			# fill kernel
			gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi)

	# kernel normalization
	gabor /= np.sum(np.abs(gabor))

	return gabor


# 使用Gabor滤波器作用于图像上
def Gabor_filtering(gray, K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0):
    # get shape
    H, W = gray.shape

    # padding
    gray = np.pad(gray, (K_size//2, K_size//2), 'edge')

    # prepare out image
    out = np.zeros((H, W), dtype=np.float32)

    # get gabor filter
    gabor = Gabor_filter(K_size=K_size, Sigma=Sigma, Gamma=Gamma, Lambda=Lambda, Psi=0, angle=angle)
        
    # filtering
    for y in range(H):
        for x in range(W):
            out[y, x] = np.sum(gray[y : y + K_size, x : x + K_size] * gabor)

    out = np.clip(out, 0, 255)
    out = out.astype(np.uint8)

    return out


# 使用6个不同角度的Gabor滤波器对图像进行特征提取
def Gabor_process(img):
    # get shape
    H, W, _ = img.shape

    # gray scale
    gray = BGR2GRAY(img).astype(np.float32)

    # define angle
    #As = [0, 45, 90, 135]
    As = [0,30,60,90,120,150]

    # prepare pyplot
    plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2)

    out = np.zeros([H, W], dtype=np.float32)

    # each angle
    for i, A in enumerate(As):
        # gabor filtering
        _out = Gabor_filtering(gray, K_size=11, Sigma=1.5, Gamma=1.2, Lambda=3, angle=A)

        # add gabor filtered image
        out += _out

    # scale normalization
    out = out / out.max() * 255
    out = out.astype(np.uint8)

    return out


# Read image
img = cv2.imread("../paojie.jpg").astype(np.float32)

# gabor process
out = Gabor_process(img)

cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
实验结果:

原图
Gabor滤波器特征提取结果

实验中,我使用了6个不同角度的Gabor滤波器对图像进行了滤波,并将滤波结果相加得到了最终的图像特征图。可以观察到,结果特征图其实就是原图像的纹理表示。

点个赞再走呗

  • 31
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Gabor滤波器是一种基于Gabor函数的滤波器,用于图像处理和计算机视觉中。下面是用Python实现Gabor滤波器的步骤: 1. 导入必要的库: ```python import cv2 import numpy as np import matplotlib.pyplot as plt ``` 2. 定义Gabor函数: ```python def gabor_kernel(size, theta, lambd, sigma, gamma): """ size: int, 滤波器尺寸 theta: float, 方向角度 lambd: float, 波长 sigma: float, 标准差 gamma: float, 空间纵横比 """ x, y = np.meshgrid(np.linspace(-1, 1, size), np.linspace(-1, 1, size)) x_theta = x * np.cos(theta) + y * np.sin(theta) y_theta = -x * np.sin(theta) + y * np.cos(theta) gb = np.exp(-0.5 * (x_theta ** 2 + gamma ** 2 * y_theta ** 2) / sigma ** 2) * np.cos(2 * np.pi * x_theta / lambd) return gb ``` 3. 定义Gabor滤波器函数: ```python def gabor_filter(img, kernels): """ img: numpy.ndarray, 输入图像 kernels: list, Gabor函数列表 """ result = np.zeros_like(img) for kernel in kernels: fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel) result = np.maximum(result, fimg, result) return result ``` 4. 使用Gabor滤波器图像进行滤波: ```python # 读取图像 img = cv2.imread("lena.png", cv2.IMREAD_GRAYSCALE) # 定义Gabor函数列表 kernels = [] for theta in range(4): theta = theta / 4. * np.pi for lambd in [1, 3]: for sigma in [1, 3]: for gamma in [0.05, 0.5]: kernel = gabor_kernel(64, theta, lambd, sigma, gamma) kernels.append(kernel) # 对图像进行Gabor滤波 result = gabor_filter(img, kernels) # 显示结果 plt.subplot(121), plt.imshow(img, cmap="gray") plt.title("Original"), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(result, cmap="gray") plt.title("Gabor Filter"), plt.xticks([]), plt.yticks([]) plt.show() ``` 这里以Lena图像为例,使用了16个不同参数的Gabor函数进行滤波。最后将滤波结果和原图像进行对比并显示出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值