您的位置:首页 > 编程语言 > Python开发

python 借助opencv实现Gabor滤波特征提取

2017-12-19 11:35 3443 查看
首先说一下环境,windows 8.0 64位系统,OpenCV版本为2.4.7, python 版本为2.7.6,IDE为 64位 Anaconda

目的是实现图像的Gabor特征提取,分三个小程序完成

包含头文件:

import cv2
import numpy as np
import pylab as pl


程序一,gaborl滤波器的构建,使用6个尺度分四个方向

**###   构建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 xrange(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


程序二,滤波过程

###    Gabor滤波过程
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


程序三,特征图生成并显示

###    Gabor特征提取
def getGabor(img,filters):
res = [] #滤波结果
for i in xrange(len(filters)):
res1 = process(img, filters[i])
res.append(np.asarray(res1))

pl.figure(2)
for temp in xrange(len(res)):
pl.subplot(4,6,temp+1)
pl.imshow(res[temp], cmap='gray' )
pl.show()

return res  #返回滤波结果,结果为24幅图,按照gabor角度排列


结果展示如下:

24个滤波器核



24个滤波结果



转自这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python opencv