资源简介
该代码使用matlab实现,代码结构清晰完整,方便大家学习
代码片段和文件信息
function e=canny(I)
% gray = rgb2gray(I);
gray=I;
a = im2single(gray);
[mn] = size(a);
% 用于输出的边界位图
e = false(mn);
% Magic numbers
GaussianDieOff = .0001;
PercentOfPixelsNotEdges = .7; % 用于阀值选择
ThresholdRatio = .4; % 低阀值相对高阀值的比值
sigma = 1; %设置sigma
thresh = [];
% 设计滤波器 - a gaussian和它的导数
pw = 1:30; % possible widths
ssq = sigma^2;
width = find(exp(-(pw.*pw)/(2*ssq))>GaussianDieOff1‘last‘);%find函数很给力...
if isempty(width)
width = 1; % the user entered a really small sigma
end
t = (-width:width);
gau = exp(-(t.*t)/(2*ssq))/(2*pi*ssq); % 高斯一维滤波
% Find the directional derivative of 2D Gaussian (along X-axis)
% Since the result is symmetric along X we can get the derivative along
% Y-axis simply by transposing the result for X direction.
[xy]=meshgrid(-width:width-width:width);
dgau2D=-x.*exp(-(x.*x+y.*y)/(2*ssq))/(pi*ssq);%二维高斯方向导数
% Convolve the filters with the image in each direction
% The canny edge detector first requires convolution with
% 2D gaussian and then with the derivitave of a gaussian.
% Since gaussian filter is separable for smoothing we can use
% two 1D convolutions in order to achieve the effect of convolving
% with 2D Gaussian. We convolve along rows and then columns.
%用一阶高斯滤波器平滑图像
aSmooth=imfilter(agau‘conv‘‘replicate‘); % run the filter accross rows
aSmooth=imfilter(aSmoothgau‘‘conv‘‘replicate‘); % and then accross columns
%应用方向导数
ax = imfilter(aSmooth dgau2D ‘conv‘‘replicate‘);
ay = imfilter(aSmooth dgau2D‘ ‘conv‘‘replicate‘);
%计算梯度幅值
mag = sqrt((ax.*ax) + (ay.*ay));
magmax = max(mag(:));
if magmax>0
mag = mag / magmax; % normalize
end
% 选择高低两个阀值用于双阀值算法检测和连接边缘.
if isempty(thresh)
counts=imhist(mag 64);
highThresh = find(cumsum(counts) > PercentOfPixelsNotEdges*m*n...
1‘first‘) / 64;
lowThresh = ThresholdRatio*highThresh;
thresh = [lowThresh highThresh];
elseif length(thresh)==1
highThresh = thresh;
if thresh>=1
eid = sprintf(‘Images:%s:thresholdMustBeLessThanOne‘ mfilename);
msg = ‘The threshold must be less than 1.‘;
error(eid‘%s‘msg);
end
lowThresh = ThresholdRatio*thresh;
thresh = [lowThresh highThresh];
elseif length(thresh)==2
lowThresh = thresh(1);
highThresh = thresh(2);
if (lowThresh >= highThresh) || (highThresh >= 1)
eid = sprintf(‘Images:%s:thresholdOutOfRange‘ mfilename);
msg = ‘Thresh must be [low high] where low < high < 1.‘;
error(eid‘%s‘msg);
end
end
% The next step is to do the non-maximum supression.
% We will accrue indices which specify ON pixels in strong edgemap
% The array e will become the weak edge m
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-08 21:15 CorrelationMatching\
文件 3667 2017-12-08 21:14 CorrelationMatching\canny.m
文件 857 2017-12-08 21:15 CorrelationMatching\cannyFindLocalMaxima.m
文件 256 2017-12-08 21:38 CorrelationMatching\main.m
文件 22995 2011-12-24 18:59 CorrelationMatching\Scene.jpg
文件 1521 2011-12-18 17:32 CorrelationMatching\Template_1.jpg
文件 9752 2011-12-16 17:35 CorrelationMatching\Template_2.jpg
评论
共有 条评论