Advertisement
bkit4s0

[Harris] sourse code

Dec 22nd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.50 KB | None | 0 0
  1. function cornerness = cornermetric(varargin)
  2. %CORNERMETRIC Create corner metric matrix from image.
  3. %   C = CORNERMETRIC(I) generates a corner metric matrix for the grayscale
  4. %   or logical image I. The corner metric, C, is used to detect corner
  5. %   features in I and is the same size as I. Larger values in C correspond
  6. %   to pixels in I with a higher likelihood of being a corner feature.
  7. %
  8. %   C = CORNERMETRIC(I,METHOD) generates a corner metric matrix for the
  9. %   grayscale or logical image I using the specified METHOD.  Supported
  10. %   METHOD's are:
  11. %
  12. %      'Harris'            : The Harris corner detector. This is the
  13. %                            default METHOD.
  14. %      'MinimumEigenvalue' : Shi & Tomasi's minimum eigenvalue method.
  15. %
  16. %   C = CORNERMETRIC(...,PARAM1,VAL1,PARAM2,VAL2,...) generates a corner
  17. %   metric matrix for I, specifying parameters and corresponding values
  18. %   that control various aspects of the corner metric calculation
  19. %   algorithm. Parameter names can be abbreviated, and case does not
  20. %   matter.
  21. %
  22. %   Parameters include:
  23. %   -------------------
  24. %
  25. %   'FilterCoefficients'     A vector of filter coefficients for the
  26. %                            smoothing filter. This parameter is valid with
  27. %                            the 'Harris' and 'MinimumEigenvalue' methods.
  28. %
  29. %                            Default value: fspecial('gaussian',[1 5],1.5)
  30. %
  31. %   'SensitivityFactor'      A scalar k, 0 < k < 0.25, specifying the
  32. %                            sensitivity factor used in the Harris
  33. %                            detection algorithm. The smaller the value
  34. %                            of k the more likely the algorithm is to
  35. %                            detect sharp corners. This parameter is only
  36. %                            valid with the 'Harris' method.
  37. %
  38. %                            Default value: 0.04
  39. %
  40. %   Example
  41. %   -------
  42. %   Find corner features in pout.tif image
  43. %
  44. %       % compute cornerness
  45. %       I = imread('pout.tif');
  46. %       I = I(1:150,1:120);
  47. %       subplot(1,3,1);
  48. %       imshow(I);
  49. %       title('Original Image');
  50. %       C = cornermetric(I);
  51. %
  52. %       % adjust corner metric for viewing
  53. %       C_adjusted = imadjust(C);
  54. %       subplot(1,3,2);
  55. %       imshow(C_adjusted);
  56. %       title('Corner Metric');
  57. %
  58. %       % find & display some corner features
  59. %       corner_peaks = imregionalmax(C);
  60. %       corner_idx = find(corner_peaks == true);
  61. %       [r g b] = deal(I);
  62. %       r(corner_idx) = 255;
  63. %       g(corner_idx) = 255;
  64. %       b(corner_idx) = 0;
  65. %       RGB = cat(3,r,g,b);
  66. %       subplot(1,3,3);
  67. %       imshow(RGB);
  68. %       title('Corner Points');
  69. %
  70. %   See also EDGE.
  71.  
  72. %   Copyright 2008 The MathWorks, Inc.
  73. %   $Revision: 1.1.6.1 $  $Date: 2008/05/14 21:57:52 $
  74.  
  75. %   References
  76. %   ----------
  77. %   [1] C. Harris and M. Stephens. "A Combined Corner and Edge Detector."
  78. %       Proceedings of the 4th Alvey Vision Conference. August 1988, pp.
  79. %       147–151.
  80. %   [2] J. Shi and C. Tomasi. "Good Features to Track." Proceedings of the
  81. %       IEEE Conference on Computer Vision and Pattern Recognition. June
  82. %       1994, pp. 593–600.
  83.  
  84. % parse inputs
  85. [I,method,sensitivity_factor,filter_coef] = parseInputs(varargin{:});
  86.  
  87. % convert to double data to normalize results
  88. I = convertToDouble(I);
  89.  
  90. % reshape coefficients and generate filter
  91. filter_coef = filter_coef(:);
  92. w = filter_coef * filter_coef';
  93.  
  94. % here is the original math.  it's optimized below to reduce memory usage
  95. % dx = imfilter(I,[-1 0 1],'replicate','same','conv');
  96. % dy = imfilter(I,[-1 0 1]','replicate','same','conv');
  97. % A = dx .* dx;
  98. % B = dy .* dy;
  99. % C = dx .* dy;
  100.  
  101. % compute gradients
  102. A = imfilter(I,[-1 0 1] ,'replicate','same','conv');
  103. B = imfilter(I,[-1 0 1]','replicate','same','conv');
  104.  
  105. % only use valid gradients
  106. A = A(2:end-1,2:end-1);
  107. B = B(2:end-1,2:end-1);
  108.  
  109. % compute A, B, and C
  110. C = A .* B;
  111. A = A .* A;
  112. B = B .* B;
  113.  
  114. % filter A, B, and C
  115. A = imfilter(A,w,'replicate','full','conv');
  116. B = imfilter(B,w,'replicate','full','conv');
  117. C = imfilter(C,w,'replicate','full','conv');
  118.  
  119. % clip to image size
  120. removed = (numel(filter_coef)-1) / 2 - 1;
  121. A = A(removed+1:end-removed,removed+1:end-removed);
  122. B = B(removed+1:end-removed,removed+1:end-removed);
  123. C = C(removed+1:end-removed,removed+1:end-removed);
  124.  
  125. % 'Harris'
  126. if strcmpi(method,'Harris')
  127.     cornerness = (A .* B) - (C .^ 2) - sensitivity_factor * ( A + B ) .^ 2;
  128.    
  129. % 'MinimumEigenvalue'
  130. else
  131.     cornerness = ((A + B) - sqrt((A - B) .^ 2 + 4 * C .^ 2)) / 2;
  132. end
  133.  
  134.  
  135. %-------------------------------------------------------------------------
  136. function [I,method,sensitivity_factor,filter_coef] = parseInputs(varargin)
  137.  
  138. % setup parser
  139. parser = inputParser;
  140. parser.addRequired('Image',@checkImage);
  141. parser.addOptional('Method','Harris',@checkMethod);
  142. parser.addParamValue('SensitivityFactor','0.04',@checkSensFactor);
  143. parser.addParamValue('FilterCoefficients',...
  144.     fspecial('gaussian',[1 5],1.5),@checkFiltCoef);
  145.  
  146. % parse input
  147. parser.parse(varargin{:});
  148.  
  149. % assign outputs
  150. I = parser.Results.Image;
  151. method = parser.Results.Method;
  152. sensitivity_factor = parser.Results.SensitivityFactor;
  153. filter_coef = parser.Results.FilterCoefficients;
  154.  
  155. % check for incompatible parameters.  if user has specified a sensitivity
  156. % factor with method other than harris, we error.  We made the sensitivity
  157. % factor default value a string to determine if one was specified or if the
  158. % default was provided since we cannot get this information from the input
  159. % parser object.
  160. method_is_not_harris = ~strcmpi(method,'Harris');
  161. sensitivity_factor_specified = ~ischar(sensitivity_factor);
  162. if method_is_not_harris && sensitivity_factor_specified
  163.     eid = sprintf('Images:%s:invalidParameterCombination',mfilename);
  164.     error(eid,'%s',['The ''SensitivityFactor'' parameter is only '...
  165.         'valid with the ''Harris'' method.']);
  166. end
  167.  
  168. % convert from default strings to actual values.
  169. if ischar(sensitivity_factor)
  170.     sensitivity_factor = str2double(sensitivity_factor);
  171. end
  172.  
  173.  
  174. %--------------------------
  175. function tf = checkImage(I)
  176. validateattributes(I,{'numeric','logical'},{'2d','nonempty',...
  177.     'nonsparse','real'},'cornermetric','I',1);
  178. tf = true;
  179.  
  180.  
  181. %---------------------------
  182. function tf = checkMethod(m)
  183. validatestring(m,{'Harris','MinimumEigenvalue'},'cornermetric',...
  184.     'Method',2);
  185. tf = true;
  186.  
  187.  
  188. %-------------------------------
  189. function tf = checkSensFactor(x)
  190. validateattributes(x,{'numeric'},{'nonempty','nonnan','real',...
  191.     'scalar'},'cornermetric','SensitivityFactor');
  192. tf = true;
  193.  
  194.  
  195. %-----------------------------
  196. function tf = checkFiltCoef(x)
  197. validateattributes(x,{'numeric'},{'nonempty','nonnan','real',...
  198.     'vector'},'cornermetric','FilterCoefficients');
  199.  
  200. % verify odd number of values
  201. if mod(numel(x),2) == 0
  202.     eid = sprintf('Images:%s:invalidFilterCoefficients',mfilename);
  203.     error(eid,'%s',['''FilterCoefficients'' must contain an odd number '...
  204.         'of elements.']);
  205. end
  206. tf = true;
  207.  
  208.  
  209. %------------------------------------
  210. function new_im = convertToDouble(im)
  211.  
  212. if isa(im,'uint8') ||...
  213.         isa(im,'double') ||...
  214.         isa(im,'logical') ||...
  215.         isa(im,'int16') ||...
  216.         isa(im,'single') ||...
  217.         isa(im,'uint16')
  218.     new_im = im2double(im);
  219.    
  220. elseif isa(im,'uint32')
  221.     range = getrangefromclass(im);
  222.     new_im = double(img) / range(2);
  223.    
  224. elseif isa(im,'int32')
  225.     new_im = (double(im) + 2^31) / (2^32);
  226.    
  227. elseif isa(im,'int8')
  228.     new_im = (double(im) + 2^7) / (2^8);
  229.    
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement