You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.9 KiB
Matlab

function gauss=gauss2D(sigma,orient,aspect,norm,pxsize,cntr,order,phase)
%function gauss=gauss2D(sigma,orient,aspect,norm,pxsize,cntr,order,phase)
%
% This function produces a numerical approximation to Gaussian function with
% variable aspect ratio.
% Parameters:
% sigma = standard deviation of Gaussian envelope, this in-turn controls the
% size of the result (pixels)
% orient = orientation of the Gaussian clockwise from the vertical (degrees)
% Optional, if not specified orient=0.
% aspect = aspect ratio of Gaussian envelope (0 = no "length" to envelope,
% 1 = circular symmetric envelope)
% Optional, if not specified aspect=1.
% norm = 1 to normalise the gaussian so that it sums to 1
% = 0 for no normalisation (gaussian has max value of 1)
% Optional, default value is 1.
% pxsize = the size of the filter.
% Optional, if not specified size is [5*sigma, 5*sigma]
% cntr = location of the centre of the gaussian.
% Optional, if not specified gaussian is centred in the middle of image.
% order = order of differential. Differential is calculated in the y direction.
% Valid values are whole numbers from 0 (output is not differentiated)
% to 2 (output is 2nd differential of a gaussian).
% Optional, if not specified default is 0.
% phase = allows this function to be used to generate filters that look like
% even and odd symmetric gabors
if nargin<2 || isempty(orient), orient=0; end
if nargin<3 || isempty(aspect), aspect=1; end
if nargin<4 || isempty(norm), norm=1; end
if nargin<5 || isempty(pxsize), pxsize=[odd(5*sigma),odd(5*sigma)]; end
if nargin<6 || isempty(cntr), cntr=ceil(pxsize./2); end
if nargin<7 || isempty(order), order=0; end
if nargin<8 || isempty(phase)
else
if phase==90 || phase==270
order=1;
elseif phase==0 || phase==180
order=2;
end
end
%define grid of x,y coodinates at which to define function
[x y]=meshgrid(1:pxsize(1),1:pxsize(2));
%rotate
orient=-orient*pi/180;
x_theta=(x-cntr(1))*cos(orient)+(y-cntr(2))*sin(orient);
y_theta=-(x-cntr(1))*sin(orient)+(y-cntr(2))*cos(orient);
%avoid division by zero errors
sigma=max(1e-15,sigma);
%define gaussian
gauss=exp(-.5*( ((x_theta.^2)./((sigma*aspect).^2)) ...
+ ((y_theta.^2)./(sigma.^2)) ));
gauss=gauss./(sigma*sqrt(2*pi));
%differentiate gaussian, if requested
if order==0
%do nothing
elseif order==1
gauss=gauss.*(-y_theta./(sigma^2));
elseif order==2
gauss=gauss.*((y_theta.^2-sigma^2)./(sigma^4));
elseif order==3
gauss=gauss.*(-y_theta*(y_theta.^2-3*sigma^2)./(sigma^6));
elseif order==4
gauss=gauss.*((y_theta.^4-6*y_theta.^2*sigma^2+3*sigma^4)./(sigma^8));
else
disp('ERROR: order of differential applied to gaussian, not defined');
end
%normalise
if norm,
gauss=gauss./sum(sum(abs(gauss)));
else
gauss=gauss./max(max(abs(gauss)));
end
if nargin<8 || isempty(phase);
else
if phase==180 || phase==270
gauss=-gauss; %invert mask
end
end