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()