tieskedh

Untitled

Sep 30th, 2016
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.89 KB | None | 0 0
  1. function [Gmag, Gdir] = imgradient(varargin)
  2. % IMGRADIENT Find the gradient magnitude and direction of an image.
  3. %   [Gmag, Gdir] = IMGRADIENT(I) takes a grayscale or binary image I as
  4. %   input and returns the gradient magnitude, Gmag, and the gradient
  5. %   direction, Gdir. Gmag and Gdir are the same size as the input image I.
  6. %   Gdir contains angles in degrees within the range [-180 180] measured
  7. %   counterclockwise from the positive X axis (X axis points in the
  8. %   direction of increasing column subscripts).
  9. %
  10. %   [Gmag, Gdir] = IMGRADIENT(I, METHOD) calculates the gradient magnitude
  11. %   and direction using the specified METHOD. Supported METHODs are:
  12. %
  13. %       'sobel'                 : Sobel gradient operator (default)
  14. %
  15. %       'prewitt'               : Prewitt gradient operator
  16. %
  17. %       'central'               : Central difference gradient dI/dx = (I(x+1)- I(x-1))/ 2
  18. %
  19. %       'intermediate'          : Intermediate difference gradient dI/dx = I(x+1) - I(x)
  20. %
  21. %       'roberts'               : Roberts gradient operator
  22. %
  23. %   [Gmag, Gdir] = IMGRADIENT(Gx, Gy) calculates the gradient magnitude and
  24. %   direction from the directional gradients along the X axis, Gx, and
  25. %   Y axis, Gy, such as that returned by IMGRADIENTXY. X axis points in the
  26. %   direction of increasing column subscripts and Y axis points in the
  27. %   direction of increasing row subscripts.
  28. %
  29. %   Class Support
  30. %   -------------
  31. %   The input image I and the input directional gradients Gx and Gy can be
  32. %   numeric or logical two-dimensional matrices, and they must be
  33. %   nonsparse. Both Gmag and Gdir are of class double in all cases, except
  34. %   when the input image I or either one or both of the directional
  35. %   gradients Gx and Gy is of class single. In that case Gmag and Gdir will
  36. %   be of class single.
  37. %
  38. %   Notes
  39. %   -----
  40. %   1. When applying the gradient operator at the boundaries of the image,
  41. %      values outside the bounds of the image are assumed to equal the
  42. %      nearest image border value. This is similar to the 'replicate'
  43. %      boundary option in IMFILTER.
  44. %
  45. %   Example 1
  46. %   ---------
  47. %   This example computes and displays the gradient magnitude and direction
  48. %   of the image coins.png using Prewitt's gradient operator.
  49. %
  50. %   I = imread('coins.png');
  51. %   imshow(I)
  52. %  
  53. %   [Gmag, Gdir] = imgradient(I,'prewitt');
  54. %
  55. %   figure, imshow(Gmag, []), title('Gradient magnitude')
  56. %   figure, imshow(Gdir, []), title('Gradient direction')
  57. %
  58. %   Example 2
  59. %   ---------
  60. %   This example computes and displays both the directional gradients and the
  61. %   gradient magnitude and gradient direction for the image coins.png.
  62. %
  63. %   I = imread('coins.png');
  64. %   imshow(I)
  65. %  
  66. %   [Gx, Gy] = imgradientxy(I);
  67. %   [Gmag, Gdir] = imgradient(Gx, Gy);
  68. %
  69. %   figure, imshow(Gmag, []), title('Gradient magnitude')
  70. %   figure, imshow(Gdir, []), title('Gradient direction')
  71. %   figure, imshow(Gx, []), title('Directional gradient: X axis')
  72. %   figure, imshow(Gy, []), title('Directional gradient: Y axis')
  73. %
  74. %   See also EDGE, FSPECIAL, IMGRADIENTXY, IMGRADIENTXYZ, IMGRADIENT3.
  75.  
  76. % Copyright 2012-2016 The MathWorks, Inc.
  77.  
  78. narginchk(1,2);
  79.  
  80. [I, Gx, Gy, method] = parse_inputs(varargin{:});
  81.  
  82. % Compute directional gradients
  83. if (isempty(I))    
  84.     % Gx, Gy are given as inputs
  85.     if ~isfloat(Gx)
  86.         Gx = double(Gx);
  87.     end
  88.     if ~isfloat(Gy)
  89.         Gy = double(Gy);
  90.     end
  91.    
  92. else  
  93.     % If Gx, Gy not given, compute them. For all others except Roberts
  94.     % method, use IMGRADIENTXY to compute Gx and Gy.
  95.     if (strcmpi(method,'roberts'))        
  96.         if ~isfloat(I)
  97.             I = double(I);
  98.         end
  99.         Gx = imfilter(I,[1 0; 0 -1],'replicate');        
  100.         Gy = imfilter(I,[0 1; -1 0],'replicate');
  101.        
  102.     else        
  103.         [Gx, Gy] = imgradientxy(I,method);
  104.        
  105.     end
  106. end
  107.  
  108. % Compute gradient magnitude
  109. Gmag = hypot(Gx,Gy);
  110.  
  111. % Compute gradient direction
  112. if (nargout > 1)
  113.     if (strcmpi(method,'roberts'))
  114.        
  115.         Gdir = zeros(size(Gx));
  116.        
  117.         % For pixels with zero gradient (both Gx and Gy zero), Gdir is set
  118.         % to 0. Compute direction only for pixels with non-zero gradient.
  119.         xyNonZero = ~((Gx == 0) & (Gy == 0));
  120.         Gdir(xyNonZero) = atan2(Gy(xyNonZero),-Gx(xyNonZero)) - (pi/4);
  121.         Gdir(Gdir < -pi) = Gdir(Gdir < -pi) + 2*pi; % To account for the discontinuity at +-pi.
  122.        
  123.         Gdir = Gdir*180/pi; % Radians to degrees
  124.        
  125.     else
  126.        
  127.         Gdir = atan2(-Gy,Gx)*180/pi; % Radians to degrees
  128.     end    
  129. end
  130.  
  131. end
  132. %======================================================================
  133. function [I, Gx, Gy, method] = parse_inputs(varargin)
  134.  
  135. methodstrings = {'sobel','prewitt','roberts','centraldifference', ...
  136.             'intermediatedifference'};
  137. I = [];
  138. Gx = [];
  139. Gy = [];
  140. method = 'sobel'; % Default method
  141.  
  142. if (nargin == 1)
  143.     I = varargin{1};
  144.     validateattributes(I,{'numeric','logical'},{'2d','nonsparse','real'}, ...
  145.                        mfilename,'I',1);
  146.        
  147. else % (nargin == 2)
  148.     if ischar(varargin{2})
  149.         I = varargin{1};
  150.         validateattributes(I,{'numeric','logical'},{'2d','nonsparse', ...
  151.                            'real'},mfilename,'I',1);
  152.         method = validatestring(varargin{2}, methodstrings, ...
  153.             mfilename, 'Method', 2);
  154.     else
  155.         Gx = varargin{1};
  156.         Gy = varargin{2};
  157.         validateattributes(Gx,{'numeric','logical'},{'2d','nonsparse', ...
  158.                            'real'},mfilename,'Gx',1);
  159.         validateattributes(Gy,{'numeric','logical'},{'2d','nonsparse', ...
  160.                            'real'},mfilename,'Gy',2);
  161.         if (~isequal(size(Gx),size(Gy)))
  162.             error(message('images:validate:unequalSizeMatrices','Gx','Gy'));
  163.         end
  164.     end
  165.          
  166. end
  167.  
  168. end
  169. %----------------------------------------------------------------------
Advertisement