-
Notifications
You must be signed in to change notification settings - Fork 1
/
harris.m
83 lines (69 loc) · 3.09 KB
/
harris.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
% HARRIS - Harris corner detector
%
% Usage: [cim, r, c] = harris(im, sigma, thresh, radius, disp)
%
% Arguments:
% im - image to be processed.
% sigma - standard deviation of smoothing Gaussian. Typical
% values to use might be 1-3.
% thresh - threshold (optional). Try a value ~1000.
% radius - radius of region considered in non-maximal
% suppression (optional). Typical values to use might
% be 1-3.
% disp - optional flag (0 or 1) indicating whether you want
% to display corners overlayed on the original
% image. This can be useful for parameter tuning.
%
% Returns:
% cim - binary image marking corners.
% r - row coordinates of corner points.
% c - column coordinates of corner points.
%
% If thresh and radius are omitted from the argument list 'cim' is returned
% as a raw corner strength image and r and c are returned empty.
% Reference:
% C.G. Harris and M.J. Stephens. "A combined corner and edge detector",
% Proceedings Fourth Alvey Vision Conference, Manchester.
% pp 147-151, 1988.
%
% Author:
% Bicheng Zhang
% Department of Computer Engineering
% University of Illinois Urbana Champaign
%
% March 2014
function [cim, r, c] = harris(im, sigma, padding_size, thresh, radius, disp)
error(nargchk(3,6,nargin));
[img_row, img_col] = size(im);
binary_mask = padarray(true(img_row-padding_size, img_col-padding_size), [padding_size/2, padding_size/2]);
dx = [-1 0 1; -1 0 1; -1 0 1]; % Derivative masks
dy = dx';
Ix = conv2(im, dx, 'same'); % Image derivatives
Iy = conv2(im, dy, 'same');
% Generate Gaussian filter of size 6*sigma (+/- 3sigma) and of
% minimum size 1x1.
g = fspecial('gaussian',max(1,fix(6*sigma)), sigma);
Ix2 = conv2(Ix.^2, g, 'same'); % Smoothed squared image derivatives
Iy2 = conv2(Iy.^2, g, 'same');
Ixy = conv2(Ix.*Iy, g, 'same');
cim = (Ix2.*Iy2 - Ixy.^2)./(Ix2 + Iy2 + eps); % Harris corner measure
% Alternate Harris corner measure used by some. Suggested that
% k=0.04 - I find this a bit arbitrary and unsatisfactory.
% cim = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;
if nargin > 2 % We should perform nonmaximal suppression and threshold
% Extract local maxima by performing a grey scale morphological
% dilation and then finding points in the corner strength image that
% match the dilated image and are also greater than the threshold.
sze = 2*radius+1; % Size of mask.
mx = ordfilt2(cim,sze^2,ones(sze)); % Grey-scale dilate.
cim = (cim==mx)&(cim>thresh); % Find maxima.
cim = cim.*binary_mask;
[r,c] = find(cim); % Find row,col coords.
if nargin==6 & disp % overlay corners on original image
figure, imagesc(im), axis image, colormap(gray), hold on
plot(c,r,'ys'), title('corners detected');
end
else % leave cim as a corner strength image and make r and c empty.
r = []; c = [];
end