www.pudn.com > eyedemo.rar > canny.asv
% CANNY - Canny边缘检测
% 代码使用了由 Fleck 提出的修改 (IEEE PAMI No. 3, Vol. 14. March 1992. pp 337-345)
%
% 使用:
% [gradient or] = canny(im, sigma)
%
% Arguments: im - 要处理的图像
% sigma - 高斯平滑滤波器的标准偏移,典型值为1
% scaling - 缩小输入图像的比例因子
% vert - 竖直梯度分量
% horz - 水平梯度分量
%
% Returns: gradient - 边界增强图像 (梯度幅值)
% or - 定位图像 (in degrees 0-180, positive
% anti-clockwise)
%
% 同时参见: NONMAXSUP, HYSTHRESH
% Author:
% Peter Kovesi
% Department of Computer Science & Software Engineering
% The University of Western Australia
% pk@cs.uwa.edu.au 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.