mkGaussian.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. % IM = mkGaussian(SIZE, COVARIANCE, MEAN, AMPLITUDE)
  2. %
  3. % Compute a matrix with dimensions SIZE (a [Y X] 2-vector, or a
  4. % scalar) containing a Gaussian function, centered at pixel position
  5. % specified by MEAN (default = (size+1)/2), with given COVARIANCE (can
  6. % be a scalar, 2-vector, or 2x2 matrix. Default = (min(size)/6)^2),
  7. % and AMPLITUDE. AMPLITUDE='norm' (default) will produce a
  8. % probability-normalized function. All but the first argument are
  9. % optional.
  10. % Eero Simoncelli, 6/96.
  11. function [res] = mkGaussian(sz, cov, mn, ampl)
  12. sz = sz(:);
  13. if (size(sz,1) == 1)
  14. sz = [sz,sz];
  15. end
  16. %------------------------------------------------------------
  17. %% OPTIONAL ARGS:
  18. if (exist('cov') ~= 1)
  19. cov = (min(sz(1),sz(2))/6)^2;
  20. end
  21. if ( (exist('mn') ~= 1) | isempty(mn) )
  22. mn = (sz+1)/2;
  23. else
  24. mn = mn(:);
  25. if (size(mn,1) == 1)
  26. mn = [mn, mn];
  27. end
  28. end
  29. if (exist('ampl') ~= 1)
  30. ampl = 'norm';
  31. end
  32. %------------------------------------------------------------
  33. [xramp,yramp] = meshgrid([1:sz(2)]-mn(2),[1:sz(1)]-mn(1));
  34. if (sum(size(cov)) == 2) % scalar
  35. if (strcmp(ampl,'norm'))
  36. ampl = 1/(2*pi*cov(1));
  37. end
  38. e = (xramp.^2 + yramp.^2)/(-2 * cov);
  39. elseif (sum(size(cov)) == 3) % a 2-vector
  40. if (strcmp(ampl,'norm'))
  41. ampl = 1/(2*pi*sqrt(cov(1)*cov(2)));
  42. end
  43. e = xramp.^2/(-2 * cov(2)) + yramp.^2/(-2 * cov(1));
  44. else
  45. if (strcmp(ampl,'norm'))
  46. ampl = 1/(2*pi*sqrt(det(cov)));
  47. end
  48. cov = -inv(cov)/2;
  49. e = cov(2,2)*xramp.^2 + (cov(1,2)+cov(2,1))*(xramp.*yramp) ...
  50. + cov(1,1)*yramp.^2;
  51. end
  52. res = ampl .* exp(e);