方式一:
function result = gaborKernel2d( lambda, theta, phi, gamma, bandwidth)
% GABORKERNEL2D
% Version: 2012/8/17 by watkins.song
% Version: 1.0
% Fills a (2N+1)*(2N+1) matrix with the values of a 2D Gabor function.
% N is computed from SIGMA.
%
% LAMBDA - preferred wavelength (period of the cosine factor) [in pixels]
% SIGMA - standard deviation of the Gaussian factor [in pixels]
% THETA - preferred orientation [in radians]
% PHI - phase offset [in radians] of the cosine factor
% GAMMA - spatial aspect ratio (of the x- and y-axis of the Gaussian elipse)
% BANDWIDTH - spatial frequency bandwidth at half response,
% *******************************************************************
%
% BANDWIDTH, SIGMA and LAMBDA are interdependent. To use BANDWIDTH,
% the input value of one of SIGMA or LAMBDA must be 0. Otherwise BANDWIDTH is ignored.
% The actual value of the parameter whose input value is 0 is computed inside the
% function from the input vallues of BANDWIDTH and the other parameter.
%
% pi -1 x'^2+gamma^2*y'^2
% G(x,y,theta,f) = --------------- *exp ([----{-------------------}])*cos(2*pi*f*x'+phi);
% 2*sigma*sigma 2 sigma^2
%
%%% x' = x*cos(theta)+y*sin(theta);
%%% y' = y*cos(theta)-x*sin(theta);
%
% Author: watkins.song
% Email: watkins.song@gmail.com
% calculation of the ratio sigma/lambda from BANDWIDTH
% according to Kruizinga and Petkov, 1999 IEEE Trans on Image Processing 8 (10) p.1396
% note that in Matlab log means ln
slratio = (1/pi) * sqrt( (log(2)/2) ) * ( (2^bandwidth + 1) / (2^bandwidth - 1) );
% calcuate sigma
sigma = slratio * lambda;
% compute the size of the 2n+1 x 2n+1 matrix to be filled with the values of a Gabor function
% this size depends on sigma and gamma
if (gamma <= 1 && gamma > 0)
n = ceil(2.5*sigma/gamma);
else
n = ceil(2.5*sigma);
end
% creation of two (2n+1) x (2n+1) matrices x and y that contain the x- and y-coordinates of
% a square 2D-mesh; the rows of x and the columns of y are copies of the vector -n:n
[x,y] = meshgrid(-n:n);
% change direction of y-axis (In Matlab the vertical axis corresponds to the row index
% of a matrix. If the y-coordinates run from -n to n, the lowest value (-n) comes
% in the top row of the matrix ycoords and the highest value (n) in the
% lowest row. This is oposite to the customary rendering