-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanny.m
61 lines (50 loc) · 2.17 KB
/
canny.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
% CANNY - Canny edge detection
%
% Function to perform Canny edge detection. Code uses modifications as
% suggested by Fleck (IEEE PAMI No. 3, Vol. 14. March 1992. pp 337-345)
%
% Usage: [gradient or] = canny(im, sigma)
%
% Arguments: im - image to be procesed
% sigma - standard deviation of Gaussian smoothing filter
% (typically 1)
% scaling - factor to reduce input image by
% vert - weighting for vertical gradients
% horz - weighting for horizontal gradients
%
% Returns: gradient - edge strength image (gradient amplitude)
% or - orientation image (in degrees 0-180, positive
% anti-clockwise)
%
% See also: NONMAXSUP, HYSTHRESH
% Author:
% Peter Kovesi
% Department of Computer Science & Software Engineering
% The University of Western Australia
% [email protected] www.cs.uwa.edu.au/~pk
%
% April 1999 Original version
% January 2003 Error in calculation of d2 corrected
% March 2003 Modified to accept scaling factor and vertical/horizontal
% gradient bias (Libor Masek)
function [gradient, or] = canny(im, sigma, scaling, vert, horz)
xscaling = vert;
yscaling = horz;
hsize = [6*sigma+1, 6*sigma+1]; % The filter size.
gaussian = fspecial('gaussian',hsize,sigma);
im = filter2(gaussian,im); % Smoothed image.
im = imresize(im, scaling);
[rows, cols] = size(im);
h = [ im(:,2:cols) zeros(rows,1) ] - [ zeros(rows,1) im(:,1:cols-1) ];
v = [ im(2:rows,:); zeros(1,cols) ] - [ zeros(1,cols); im(1:rows-1,:) ];
d1 = [ im(2:rows,2:cols) zeros(rows-1,1); zeros(1,cols) ] - ...
[ zeros(1,cols); zeros(rows-1,1) im(1:rows-1,1:cols-1) ];
d2 = [ zeros(1,cols); im(1:rows-1,2:cols) zeros(rows-1,1); ] - ...
[ zeros(rows-1,1) im(2:rows,1:cols-1); zeros(1,cols) ];
X = ( h + (d1 + d2)/2.0 ) * xscaling;
Y = ( v + (d1 - d2)/2.0 ) * yscaling;
gradient = sqrt(X.*X + Y.*Y); % Gradient amplitude.
or = atan2(-Y, X); % Angles -pi to + pi.
neg = or<0; % Map angles to 0-pi.
or = or.*~neg + (or+pi).*neg;
or = or*180/pi; % Convert to degrees.