matlab实现gabor filter (3)

从wikipedia上找到一段gabor filter的代码,简单的写了生成5个尺度8个方向的滤波器组。

 

gabor filter的代码:

 

% 这个gabor实现是来自维基百科的,应该没有问题
% watkins
function gb=gabor(sigma,theta,lambda,psi,gamma)
 
sigma_x = sigma;
sigma_y = sigma/gamma;
 
% Bounding box
nstds = 4;
xmax = max(abs(nstds*sigma_x*cos(theta)),abs(nstds*sigma_y*sin(theta)));
xmax = ceil(max(1,xmax));
ymax = max(abs(nstds*sigma_x*sin(theta)),abs(nstds*sigma_y*cos(theta)));
ymax = ceil(max(1,ymax));
xmin = -xmax; ymin = -ymax;
[x,y] = meshgrid(xmin:xmax,ymin:ymax);
 
% Rotation 
x_theta=x*cos(theta)+y*sin(theta);
y_theta=-x*sin(theta)+y*cos(theta);
 
gb= exp(-.5*(x_theta.^2/sigma_x^2+y_theta.^2/sigma_y^2)).*cos(2*pi/lambda*x_theta+psi);


调用代码:

 

gaborfilter=gabor(12,0,8,0,1);
%imshow(gaborfilter/2+0.5);

theta=[0 pi/4 2*pi/4 3*pi/4 4*pi/4 5*pi/4 6*pi/4 7*pi/4];
gamma=1;
psi=0;
sigma=6; % 也可以为12
lambda=[5 6 7 8 9];

[nh nw]=size(gaborfilter);

G=cell(5,8);
for i = 1:5
    for j = 1:8
        G{i,j}=zeros(nh,nw);
    end
end

for i = 1:5
    for j = 1:8
        G{i,j}=gabor(sigma,theta(j),lambda(i),psi,gamma);
    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


生成的滤波器组图片:

matlab实现gabor filter (3)_第1张图片

 

你可能感兴趣的:(filter,matlab,lambda)