1 | % HARRIS - Harris corner detector |
---|
2 | % |
---|
3 | % Usage: cim = harris(im, sigma) |
---|
4 | % [cim, r, c] = harris(im, sigma, thresh, radius, disp) |
---|
5 | % [cim, r, c, rsubp, csubp] = harris(im, sigma, thresh, radius, disp) |
---|
6 | % |
---|
7 | % Arguments: |
---|
8 | % im - image to be processed. |
---|
9 | % sigma - standard deviation of smoothing Gaussian. Typical |
---|
10 | % values to use might be 1-3. |
---|
11 | % thresh - threshold (optional). Try a value ~1000. |
---|
12 | % radius - radius of region considered in non-maximal |
---|
13 | % suppression (optional). Typical values to use might |
---|
14 | % be 1-3. |
---|
15 | % disp - optional flag (0 or 1) indicating whether you want |
---|
16 | % to display corners overlayed on the original |
---|
17 | % image. This can be useful for parameter tuning. This |
---|
18 | % defaults to 0 |
---|
19 | % |
---|
20 | % Returns: |
---|
21 | % cim - binary image marking corners. |
---|
22 | % r - row coordinates of corner points. |
---|
23 | % c - column coordinates of corner points. |
---|
24 | % rsubp - If five return values are requested sub-pixel |
---|
25 | % csubp - localization of feature points is attempted and |
---|
26 | % returned as an additional set of floating point |
---|
27 | % coords. Note that you may still want to use the integer |
---|
28 | % valued coords to specify centres of correlation windows |
---|
29 | % for feature matching. |
---|
30 | % |
---|
31 | % If thresh and radius are omitted from the argument list only 'cim' is |
---|
32 | % returned as a raw corner strength image. |
---|
33 | |
---|
34 | % References: |
---|
35 | % C.G. Harris and M.J. Stephens. "A combined corner and edge detector", |
---|
36 | % Proceedings Fourth Alvey Vision Conference, Manchester. |
---|
37 | % pp 147-151, 1988. |
---|
38 | % |
---|
39 | % Alison Noble, "Descriptions of Image Surfaces", PhD thesis, Department |
---|
40 | % of Engineering Science, Oxford University 1989, p45. |
---|
41 | |
---|
42 | % Copyright (c) 2002-2005 Peter Kovesi |
---|
43 | % School of Computer Science & Software Engineering |
---|
44 | % The University of Western Australia |
---|
45 | % http://www.csse.uwa.edu.au/ |
---|
46 | % |
---|
47 | % Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
48 | % of this software and associated documentation files (the "Software"), to deal |
---|
49 | % in the Software without restriction, subject to the following conditions: |
---|
50 | % |
---|
51 | % The above copyright notice and this permission notice shall be included in |
---|
52 | % all copies or substantial portions of the Software. |
---|
53 | % |
---|
54 | % The Software is provided "as is", without warranty of any kind. |
---|
55 | |
---|
56 | % March 2002 - original version |
---|
57 | % December 2002 - updated comments |
---|
58 | % August 2005 - changed so that code calls nonmaxsuppts |
---|
59 | |
---|
60 | function [cim, r, c, rsubp, csubp] = harris(im, sigma, thresh, radius, disp) |
---|
61 | |
---|
62 | error(nargchk(2,5,nargin)); |
---|
63 | if nargin == 4 |
---|
64 | disp = 0; |
---|
65 | end |
---|
66 | |
---|
67 | if ~isa(im,'double') |
---|
68 | im = double(im); |
---|
69 | end |
---|
70 | |
---|
71 | subpixel = nargout == 5; |
---|
72 | |
---|
73 | dx = [-1 0 1; -1 0 1; -1 0 1]; % Derivative masks |
---|
74 | dy = dx'; |
---|
75 | |
---|
76 | Ix = conv2(im, dx, 'same'); % Image derivatives |
---|
77 | Iy = conv2(im, dy, 'same'); |
---|
78 | |
---|
79 | % Generate Gaussian filter of size 6*sigma (+/- 3sigma) and of |
---|
80 | % minimum size 1x1. |
---|
81 | g = fspecial('gaussian',max(1,fix(6*sigma)), sigma); |
---|
82 | |
---|
83 | Ix2 = conv2(Ix.^2, g, 'same'); % Smoothed squared image derivatives |
---|
84 | Iy2 = conv2(Iy.^2, g, 'same'); |
---|
85 | Ixy = conv2(Ix.*Iy, g, 'same'); |
---|
86 | |
---|
87 | % Compute the Harris corner measure. Note that there are two measures |
---|
88 | % that can be calculated. I prefer the first one below as given by |
---|
89 | % Nobel in her thesis (reference above). The second one (commented out) |
---|
90 | % requires setting a parameter, it is commonly suggested that k=0.04 - I |
---|
91 | % find this a bit arbitrary and unsatisfactory. |
---|
92 | |
---|
93 | cim = (Ix2.*Iy2 - Ixy.^2)./(Ix2 + Iy2 + eps); % My preferred measure. |
---|
94 | % k = 0.04; |
---|
95 | % cim = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2; % Original Harris measure. |
---|
96 | |
---|
97 | |
---|
98 | if nargin > 2 % We should perform nonmaximal suppression and threshold |
---|
99 | |
---|
100 | if disp % Call nonmaxsuppts to so that image is displayed |
---|
101 | if subpixel |
---|
102 | [r,c,rsubp,csubp] = nonmaxsuppts(cim, radius, thresh, im); |
---|
103 | else |
---|
104 | [r,c] = nonmaxsuppts(cim, radius, thresh, im); |
---|
105 | end |
---|
106 | else % Just do the nonmaximal suppression |
---|
107 | if subpixel |
---|
108 | [r,c,rsubp,csubp] = nonmaxsuppts(cim, radius, thresh); |
---|
109 | else |
---|
110 | [r,c] = nonmaxsuppts(cim, radius, thresh); |
---|
111 | end |
---|
112 | end |
---|
113 | end |
---|
114 | |
---|