gabor 滤波器 函数:
function [GaborReal, GaborImg] = MakeGaborKernal(GaborH, GaborW, U, V, Kmax,f,sigma ) % function [GaborReal, GaborImg] = MakeGaborKernal[GaborH, GaborW, U, V] % 用以生成 Gabor 核 % GaborReal: 核实部 GaborImg: 虚部 % GaborH,GaborW: Gabor窗口 高宽. % U,V: 方向 大小 % ||Ku,v||^2 % G(Z) = ---------------- exp(-||Ku,v||^2 * Z^2)/(2*sigma*sigma)(exp(i*Ku,v*Z)-exp(-sigma*sigma/2)) % sigma*sigma HarfH = fix(GaborH/2); HarfW = fix(GaborW/2); Qu = pi*U/8; sqsigma = sigma*sigma; % Kv = 2.5*pi*(2^(-(V+2)/2)); Kv = Kmax/(f^V); postmean = exp(-sqsigma/2); for j = -HarfH : HarfH for i = -HarfW : HarfW tmp1 = exp(-(Kv*Kv*(j*j+i*i)/(2*sqsigma))); tmp2 = cos(Kv*cos(Qu)*i+Kv*sin(Qu)*j) - postmean; % tmp3 = sin(Kv*cos(Qu)*i+Kv*sin(Qu)*j) - exp(-sqsigma/2); tmp3 = sin(Kv*cos(Qu)*i+Kv*sin(Qu)*j); GaborReal(j+HarfH+1, i+HarfW+1) = Kv*Kv*tmp1*tmp2/sqsigma; GaborImg(j+HarfH+1, i+HarfW+1) = Kv*Kv*tmp1*tmp3/sqsigma; end end
调用代码:
% GaborReal, [GaborH,GaborW,40] 40个Gabor模板实部 % GaborImg, [GaborH,GaborW,40] 40个Gabor模板虚部 % 缺省输入前2个参数,后面参数 Kmax=2.5*pi/2, f=sqrt(2), sigma=1.5*pi; % GaborH, GaborW, Gabor模板大小 % U,方向因子{0,1,2,3,4,5,6,7} % V,大小因子{0,1,2,3,4} % Kmax,f,sigma 其他参数 GaborH=31; GaborW=31; Kmax=2.5*pi/2; f=sqrt(2); sigma=1.5*pi; GaborReal = zeros( GaborH, GaborW, 40 ); GaborImg = zeros( GaborH, GaborW, 40 ); vnum=5; unum=8; % 获得滤波器组 for v = 0 : (vnum-1) for u = 0 : (unum-1) [ GaborReal(:,:,v*8+u+1), GaborImg(:,:,v*8+u+1) ] = MakeGaborKernal( GaborH, GaborW, u, v, Kmax,f,sigma ); end end G=cell(5,8); for i = 1:5 for j = 1:8 G{i,j}=zeros(GaborH,GaborW); end end for i = 1:5 for j = 1:8 G{i,j}=GaborReal(:,:,(i-1)*8+j); end end %plot figure; for i = 1:5 for j = 1:8 subplot(5,8,(i-1)*8+j); %imshow(real(G{s,j})/2-0.5,[]); imshow(real(G{i,j}),[]); end end
生成的滤波器组图像: