Gabor 滤波简介与代码简单实现
Gabor是一个用于边缘提取的线性滤波器,其频率和方向表达与人类视觉系统类似,能够提供良好的方向选择和尺度选择特性,而且对于光照变化不敏感,因此十分适合纹理分析。在人脸识别等领域有着很广泛的应用
一、Gabor滤波简介
Gabor是一个用于边缘提取的线性滤波器,其频率和方向表达与人类视觉系统类似,能够提供良好的方向选择和尺度选择特性,而且对于光照变化不敏感,因此十分适合纹理分析。
Gabor滤波器和脊椎动物视觉皮层感受野响应的比较:第一行代表脊椎动物的视觉皮层感受野,第二行是Gabor滤波器,第三行是两者的残差。可见两者相差极小。Gabor滤波器的这一性质,使得其在视觉领域中经常被用来作图像的预处理。
另附Gabor滤波的效果图
二、代码演示
import cv2,os
import numpy as np
import matplotlib.pyplot as plt
def get_img(input_Path):
img_paths = []
for (path, dirs, files) in os.walk(input_Path):
for filename in files:
if filename.endswith(('.jpg','.png')):
img_paths.append(path+'/'+filename)
return img_paths
#构建Gabor滤波器
def build_filters():
filters = []
ksize = [7,9,11,13,15,17] # gabor尺度,6个
lamda = np.pi/2.0 # 波长
for theta in np.arange(0, np.pi, np.pi / 4): #gabor方向,0°,45°,90°,135°,共四个
for K in range(6):
kern = cv2.getGaborKernel((ksize[K], ksize[K]), 1.0, theta, lamda, 0.5, 0, ktype=cv2.CV_32F)
kern /= 1.5*kern.sum()
filters.append(kern)
plt.figure(1)
#用于绘制滤波器
for temp in range(len(filters)):
plt.subplot(4, 6, temp + 1)
plt.imshow(filters[temp])
plt.show()
return filters
#Gabor特征提取
def getGabor(img,filters):
res = [] #滤波结果
for i in range(len(filters)):
# res1 = process(img, filters[i])
accum = np.zeros_like(img)
for kern in filters[i]:
fimg = cv2.filter2D(img, cv2.CV_8UC1, kern)
accum = np.maximum(accum, fimg, accum)
res.append(np.asarray(accum))
#用于绘制滤波效果
plt.figure(2)
for temp in range(len(res)):
plt.subplot(4,6,temp+1)
plt.imshow(res[temp], cmap='gray' )
plt.show()
return res #返回滤波结果,结果为24幅图,按照gabor角度排列
if __name__ == '__main__':
input_Path = './content'
filters = build_filters()
img_paths = get_img(input_Path)
for img in img_paths:
img = cv2.imread(img)
getGabor(img, filters)
这个过程有点慢,一张图片要1-3s,若是批量处理可以开启多线程,这样会快点
此代码用来查看滤波器
#coding:utf-8
'''
Gabor滤波器参数可视化
参考:https://blog.csdn.net/lhanchao/article/details/55006663
'''
import cv2
import numpy as np
import math
# λ(波长)变化
kernel1 = cv2.getGaborKernel((200,200),10,0,5,0.5,0)
kernel2 = cv2.getGaborKernel((200,200),10,0,10,0.5,0)
kernel3 = cv2.getGaborKernel((200,200),10,0,15,0.5,0)
kernel4 = cv2.getGaborKernel((200,200),10,0,20,0.5,0)
cv2.imshow("lambda: 5", kernel1)
cv2.imshow("lambda: 10", kernel2)
cv2.imshow("lambda: 15", kernel3)
cv2.imshow("lambda: 20", kernel4)
# θ变化
kernel1 = cv2.getGaborKernel((311, 311), 10, 0, 10, 0.5, 0)
kernel2 = cv2.getGaborKernel((311, 311), 10, math.pi * 0.25, 10, 0.5)
kernel3 = cv2.getGaborKernel((311, 311), 10, math.pi * 0.5, 10, 0.5, 0)
kernel4 = cv2.getGaborKernel((311, 311), 10, math.pi * 0.75, 10, 0.5, 0)
cv2.imshow("theta: 0", kernel1)
cv2.imshow("theta: 45", kernel2)
cv2.imshow("theta: 90", kernel3)
cv2.imshow("theta: 135", kernel4)
# ψ的变化
# σ的变化:
kernel1 = cv2.getGaborKernel((311, 311), 5, 0, 10, 0.5, 0)
kernel2 = cv2.getGaborKernel((311, 311), 10, 0, 10, 0.5, 0)
kernel3 = cv2.getGaborKernel((311, 311), 15, 0, 10, 0.5, 0)
kernel4 = cv2.getGaborKernel((311, 311), 20, 0, 10, 0.5, 0)
cv2.imshow("sigma: 5", kernel1)
cv2.imshow("sigma: 10", kernel2)
cv2.imshow("sigma: 15", kernel3)
cv2.imshow("sigma: 20", kernel4)
# γ的变化
kernel1 = cv2.getGaborKernel((200, 200), 10, 0, 10, 0.5, 0)
kernel2 = cv2.getGaborKernel((200, 200), 10, 0, 10, 1.0, 0)
kernel3 = cv2.getGaborKernel((200, 200), 10, 0, 10, 1.5, 0)
kernel4 = cv2.getGaborKernel((200, 200), 10, 0, 10, 2.0, 0)
cv2.imshow("gamma: 0.5", kernel1)
cv2.imshow("gamma: 1.0", kernel2)
cv2.imshow("gamma: 1.5", kernel3)
cv2.imshow("gamma: 2.0", kernel4)
cv2.waitKey()
cv2.destroyAllWindows()