1. 程式人生 > >python程式碼 Gabor濾波器

python程式碼 Gabor濾波器

# coding:utf-8
import cv2
import numpy as np
import pylab as pl
from PIL import Image

#構建Gabor濾波器
def build_filters():
    filters = []
    ksize = [1,2,3,4,5,6]#[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)
    return filters

#濾波過程
def process(img,filters):
    accum = np.zeros_like(img)
    for kern in filters:
        fimg = cv2.filter2D(img,cv2.CV_8UC3,kern)
        np.maximum(accum,fimg,accum)
    return accum

#特徵圖生成並顯示
def getGabor(img,filters):
    image = Image.open(img)
    img_ndarray = np.asarray(image)
    res = [] #濾波結果
    for i in range(len(filters)):
        res1 = process(img_ndarray,filters[i])
        res.append(np.asarray(res1))

    pl.figure(2)
    for temp in range(len(res)):
        pl.subplot(4,6,temp+1)  #畫4*6格子
        pl.imshow(res[temp],cmap='gray')
    pl.show()

    return res

if __name__ == '__main__':
    filters = build_filters()
    getGabor('./1.jpg',filters)