Fourier变换的不足
Fourier频率分析存在严重不足,它无法告知某些频率成分发生在哪些时间内,无法表示某个时刻信号频谱的分布情况。信号在某时刻的一个小的邻域内发生变化,那么信号的整个频谱都要受到影响,而频谱的变化从根本上来说无法标定发生变化的时间位置和发生变化的剧烈程度。傅里叶变换的时域和频域是完全分割开来的。
其实根本原因是傅里叶变换的三角基是全域性的,其局部化性质不好,因而只能得到信号的整个频谱,难以在任何有限频段上确定任意小范围内信号的局部特征
为了解决这种局部性的问题,1946年,DennisGabor提出了“窗口Fourier变换”的概念,即Gabor变换
1.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('./test.jpg')
getGabor(img, filters)
参考文章:https://blog.csdn.net/wsp_1138886114/article/details/84841370