imGradient.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. % [dx, dy] = imGradient(im, edges)
  2. %
  3. % Compute the gradient of the image using smooth derivative filters
  4. % optimized for accurate direction estimation. Coordinate system
  5. % corresponds to standard pixel indexing: X axis points rightward. Y
  6. % axis points downward. EDGES specify boundary handling (see corrDn
  7. % for options).
  8. %
  9. % Unlike matlab's new gradient function, which is based on local
  10. % differences, this function computes derivatives using 5x5 filters
  11. % designed to accurately reflect the local orientation content.
  12. % EPS, 1997.
  13. % original filters from Int'l Conf Image Processing, 1994.
  14. % updated filters 10/2003: see Farid & Simoncelli, IEEE Trans Image Processing, 13(4):496-508, April 2004.
  15. % Incorporated into matlabPyrTools 10/2004.
  16. function [dx, dy] = imGradient(im, edges)
  17. if (exist('edges') ~= 1)
  18. edges = 'dont-compute';
  19. end
  20. %% kernels from Farid & Simoncelli, IEEE Trans Image Processing, 13(4):496-508, April 2004.
  21. gp = [0.037659 0.249153 0.426375 0.249153 0.037659]';
  22. gd = [-0.109604 -0.276691 0.000000 0.276691 0.109604]';
  23. dx = corrDn(corrDn(im, gp, edges), gd', edges);
  24. dy = corrDn(corrDn(im, gd, edges), gp', edges);
  25. return
  26. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  27. %%% TEST:
  28. %%Make a ramp with random slope and direction
  29. dir = 2*pi*rand - pi;
  30. slope = 10*rand;
  31. sz = 32
  32. im = mkRamp(sz, dir, slope);
  33. [dx,dy] = imGradient(im);
  34. showIm(dx + sqrt(-1)*dy);
  35. ctr = (sz*sz/2)+sz/2;
  36. slopeEst = sqrt(dx(ctr).^2 + dy(ctr).^2);
  37. dirEst = atan2(dy(ctr), dx(ctr));
  38. [slope, slopeEst]
  39. [dir, dirEst]