function hp=gbfilter2(sigmax,mu,mv)
% 函数功能:生成二维离散gabor函数模板hp。可能主要用于图像的纹理分析;
%输入参数:sigmax——x方向的方差;
% mu——x方向中心频率;mv——y方向中心频率;
%输出参数:hp——二维离散gabor模板;
%按照生理视觉的研究 y方向标准差为x方向标准差的1.5-2.0倍,此处取1.7
sigmay=sigmax*1.7;
%按照误差原则,(-3sigama,3sigma)包含了97.4%的能量。N也为滤波器长度。
%通常在纹理分析前,需要用到模板长度N,此时可用N=fix(6*sigmax*1.7);这一语句求得。
N=fix(6*sigmay);
%该句主要防止被零除
if mu==0
mu=1e-12;
end
% theta角为纹理方向
theta=atan(mv/mu);
%采样区间和间隔,此处为过采样。临界采样并不可取。
kx=-(N-1)/2:(N-1)/2;
ky=kx;
%生成x,y坐标。
[tpx,tpy]=meshgrid(kx,ky);
%计算gabor函数旋转生成的新坐标
x=tpx*cos(theta)+tpy*sin(theta);
y=-tpx*sin(theta)+tpy*cos(theta);
%计算gauss函数
z=1/(2*pi*sigmax*sigmay)*exp(-0.5*((
x/sigmax).^2+(y/sigmay).^2));
%计算调制因子
exptab=exp(-j2*pi*(tpx*mu+tpy*mv)/N);
%gauss函数被调制因子调制
hp=z.*exptab;
%矩阵转置以生成与习惯相符的x,y轴
hp=hp';
end
第二个产生二维gabor滤波器的程序
function
[hp,sigmax]=gbfilt2(ax,mu,mv,N)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数功能:生成用于图像滤波的gabor函数的模板
% 输入参数:
ax——关于x轴的sigma因子,ax=N/(2*sigma),3.5<=ax<=N/2;
% mu——x方向中心频率,mu<=N/4;mv——y方向中心频率;mv<=N/4;
% N——滤波器长度,与滤波器宽度相同;
% 输出参数: hp——生成的gabor模板;sigmax对应x方向的sigama;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 定义sigmay=1.7*sigmax
ay=ax/1.7;
% 防止被零除
if mu==0
mu=1e-16;
end
% 计算gabor滤波器的方向
theta=atan(mv/mu);
% 定义x,y取值范围为长度N
kx=-(N-1)/2:(N-1)/2;
ky=kx;
% 生成x,y坐标
[tpx,tpy]=meshgrid(kx,ky);
% 旋转theta角
x=tpx*cos(theta)+tpy*sin(theta);
y=-tpx*sin(theta)+tpy*cos(theta);
% gauss函数
z=2*ax*ay/(pi*N^2)*exp((-1/2)*((ax*x/(N/2)).^2+(ay*y/(N/2)).^2));
% 频率调制因子
facmod=cos(2*pi*(tpx*mu+tpy*mv)/N);
% 返回模板
hp=(z.*facmod)';
% 返回sigmax
sigmax=N/(2*ax);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%