gaussgradient.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. function [gx,gy]=gaussgradient(IM,sigma)
  2. %GAUSSGRADIENT Gradient using first order derivative of Gaussian.
  3. % [gx,gy]=gaussgradient(IM,sigma) outputs the gradient image gx and gy of
  4. % image IM using a 2-D Gaussian kernel. Sigma is the standard deviation of
  5. % this kernel along both directions.
  6. %
  7. % Contributed by Guanglei Xiong (xgl99@mails.tsinghua.edu.cn)
  8. % at Tsinghua University, Beijing, China.
  9. %determine the appropriate size of kernel. The smaller epsilon, the larger
  10. %size.
  11. epsilon=1e-2;
  12. halfsize=ceil(sigma*sqrt(-2*log(sqrt(2*pi)*sigma*epsilon)));
  13. size=2*halfsize+1;
  14. %generate a 2-D Gaussian kernel along x direction
  15. for i=1:size
  16. for j=1:size
  17. u=[i-halfsize-1 j-halfsize-1];
  18. hx(i,j)=gauss(u(1),sigma)*dgauss(u(2),sigma);
  19. end
  20. end
  21. hx=hx/sqrt(sum(sum(abs(hx).*abs(hx))));
  22. %generate a 2-D Gaussian kernel along y direction
  23. hy=hx';
  24. %2-D filtering
  25. gx=imfilter(IM,hx,'replicate','conv');
  26. gy=imfilter(IM,hy,'replicate','conv');
  27. function y = gauss(x,sigma)
  28. %Gaussian
  29. y = exp(-x^2/(2*sigma^2)) / (sigma*sqrt(2*pi));
  30. function y = dgauss(x,sigma)
  31. %first order derivative of Gaussian
  32. y = -x * gauss(x,sigma) / sigma^2;