function [f,xx,yy] = makegaussian2d(res,r,c,sr,sc,xx,yy,ang,omitexp) <res> is the number of pixels along one side <r> is the row associated with the peak of the Gaussian (can be a decimal). if [], default to the exact center of the image along the vertical dimension. <c> is the column associated with the peak of the Gaussian (can be a decimal). if [], default to the exact center of the image along the horizontal dimension. <sr> is the standard deviation in the vertical direction <sc> is the standard deviation in the horizontal direction <xx>,<yy> (optional) are speed-ups (dependent on <res>) <ang> (optional) is the CCW rotation to apply in [0,2*pi). 0 means no rotation. it's okay for <ang> to go out of range. default: 0. <omitexp> (optional) is whether to omit the final exp operation. default: 0. return an image where values are in [0,1]. if you want an L1-normalized image, divide the image by 2*pi*<sr>*<sc>. note that this is in reference to the ideal case where the Gaussian has enough room to extend out. so, if you are constructing a Gaussian that does not fit very well within the image, the actual L1 length of the image that is constructed will not be exactly 1. note that it doesn't matter if <sr> or <sc> are negative, since they are always squared in function evaluation. history: - 2013/08/28 - implement speed-up example: figure; imagesc(makegaussian2d(32,8,8,4,2),[0 1]);
0001 function [f,xx,yy] = makegaussian2d(res,r,c,sr,sc,xx,yy,ang,omitexp) 0002 0003 % function [f,xx,yy] = makegaussian2d(res,r,c,sr,sc,xx,yy,ang,omitexp) 0004 % 0005 % <res> is the number of pixels along one side 0006 % <r> is the row associated with the peak of the Gaussian (can be a decimal). 0007 % if [], default to the exact center of the image along the vertical dimension. 0008 % <c> is the column associated with the peak of the Gaussian (can be a decimal). 0009 % if [], default to the exact center of the image along the horizontal dimension. 0010 % <sr> is the standard deviation in the vertical direction 0011 % <sc> is the standard deviation in the horizontal direction 0012 % <xx>,<yy> (optional) are speed-ups (dependent on <res>) 0013 % <ang> (optional) is the CCW rotation to apply in [0,2*pi). 0 means no rotation. 0014 % it's okay for <ang> to go out of range. default: 0. 0015 % <omitexp> (optional) is whether to omit the final exp operation. default: 0. 0016 % 0017 % return an image where values are in [0,1]. 0018 % 0019 % if you want an L1-normalized image, divide the image by 2*pi*<sr>*<sc>. 0020 % note that this is in reference to the ideal case where the Gaussian has 0021 % enough room to extend out. so, if you are constructing a Gaussian that 0022 % does not fit very well within the image, the actual L1 length of the image 0023 % that is constructed will not be exactly 1. 0024 % 0025 % note that it doesn't matter if <sr> or <sc> are negative, since they 0026 % are always squared in function evaluation. 0027 % 0028 % history: 0029 % - 2013/08/28 - implement speed-up 0030 % 0031 % example: 0032 % figure; imagesc(makegaussian2d(32,8,8,4,2),[0 1]); 0033 0034 % input 0035 if isempty(r) 0036 r = (1+res)/2; 0037 end 0038 if isempty(c) 0039 c = (1+res)/2; 0040 end 0041 if ~exist('ang','var') || isempty(ang) 0042 ang = 0; 0043 end 0044 if ~exist('omitexp','var') || isempty(omitexp) 0045 omitexp = 0; 0046 end 0047 0048 % construct coordinates 0049 if ~exist('xx','var') || isempty(xx) 0050 [xx,yy] = calcunitcoordinates(res); 0051 end 0052 0053 % convert to the unit coordinate frame 0054 % r = normalizerange(r,.5,-.5,.5,res+.5,0,0,1); % note the signs 0055 % c = normalizerange(c,-.5,.5,.5,res+.5,0,0,1); 0056 r = (-1/res) * r + (.5 + .5/res); % this is faster 0057 c = (1/res) * c + (-.5 - .5/res); % this is faster 0058 sr = sr/res; 0059 sc = sc/res; 0060 0061 % construct coordinates (see makegabor2d.m) 0062 coord = [cos(ang) sin(ang); -sin(ang) cos(ang)]*[flatten(xx-c); flatten(yy-r)]; 0063 0064 % handle equal std dev as a separate case for speed reasons 0065 if sc==sr 0066 f = (coord(1,:).^2+coord(2,:).^2)/-(2*sc^2); 0067 else 0068 f = coord(1,:).^2/-(2*sc^2) + coord(2,:).^2/-(2*sr^2); 0069 end 0070 if ~omitexp 0071 f = exp(f); 0072 end 0073 f = reshape(f,size(xx));