makegaussian2d.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. function [f,xx,yy] = makegaussian2d(res,r,c,sr,sc,xx,yy,ang,omitexp)
  2. % function [f,xx,yy] = makegaussian2d(res,r,c,sr,sc,xx,yy,ang,omitexp)
  3. %
  4. % <res> is the number of pixels along one side
  5. % <r> is the row associated with the peak of the Gaussian (can be a decimal).
  6. % if [], default to the exact center of the image along the vertical dimension.
  7. % <c> is the column associated with the peak of the Gaussian (can be a decimal).
  8. % if [], default to the exact center of the image along the horizontal dimension.
  9. % <sr> is the standard deviation in the vertical direction
  10. % <sc> is the standard deviation in the horizontal direction
  11. % <xx>,<yy> (optional) are speed-ups (dependent on <res>)
  12. % <ang> (optional) is the CCW rotation to apply in [0,2*pi). 0 means no rotation.
  13. % it's okay for <ang> to go out of range. default: 0.
  14. % <omitexp> (optional) is whether to omit the final exp operation. default: 0.
  15. %
  16. % return an image where values are in [0,1].
  17. %
  18. % if you want an L1-normalized image, divide the image by 2*pi*<sr>*<sc>.
  19. % note that this is in reference to the ideal case where the Gaussian has
  20. % enough room to extend out. so, if you are constructing a Gaussian that
  21. % does not fit very well within the image, the actual L1 length of the image
  22. % that is constructed will not be exactly 1.
  23. %
  24. % note that it doesn't matter if <sr> or <sc> are negative, since they
  25. % are always squared in function evaluation.
  26. %
  27. % history:
  28. % - 2013/08/28 - implement speed-up
  29. %
  30. % example:
  31. % figure; imagesc(makegaussian2d(32,8,8,4,2),[0 1]);
  32. % input
  33. if isempty(r)
  34. r = (1+res)/2;
  35. end
  36. if isempty(c)
  37. c = (1+res)/2;
  38. end
  39. if ~exist('ang','var') || isempty(ang)
  40. ang = 0;
  41. end
  42. if ~exist('omitexp','var') || isempty(omitexp)
  43. omitexp = 0;
  44. end
  45. % construct coordinates
  46. if ~exist('xx','var') || isempty(xx)
  47. [xx,yy] = calcunitcoordinates(res);
  48. end
  49. % convert to the unit coordinate frame
  50. % r = normalizerange(r,.5,-.5,.5,res+.5,0,0,1); % note the signs
  51. % c = normalizerange(c,-.5,.5,.5,res+.5,0,0,1);
  52. r = (-1/res) * r + (.5 + .5/res); % this is faster
  53. c = (1/res) * c + (-.5 - .5/res); % this is faster
  54. sr = sr/res;
  55. sc = sc/res;
  56. % construct coordinates (see makegabor2d.m)
  57. coord = [cos(ang) sin(ang); -sin(ang) cos(ang)]*[flatten(xx-c); flatten(yy-r)];
  58. % handle equal std dev as a separate case for speed reasons
  59. if sc==sr
  60. f = (coord(1,:).^2+coord(2,:).^2)/-(2*sc^2);
  61. else
  62. f = coord(1,:).^2/-(2*sc^2) + coord(2,:).^2/-(2*sr^2);
  63. end
  64. if ~omitexp
  65. f = exp(f);
  66. end
  67. f = reshape(f,size(xx));