Canny边缘检测的步骤:
1、高斯平滑(gaussian smoothing)
2、图像求导(derivation)
3、非极大值抑制(non-local maxima suppression)
4、滞后门限(hysteresis threshold)
* 非极大值抑制 和 滞后门限 是canny边缘检测的特色步骤
具体介绍:
http://blog.csdn.net/likezhaobin/article/details/6892176
http://www.cnblogs.com/blue-lg/archive/2011/12/25/2300899.html
算法实现(matlab源代码):
matlab中边缘检测的函数是edge(gray_image, 'canny');我把里面关于canny算法的部分提出来写成一个单独的函数。
0、首先是预处理
变量a是一个m*n的矩阵,是输入的灰度图。变量e是用来保存边缘的矩阵,是一个二值图。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 function e = canny_edge_matlab(a) 2 % Transform to a double precision intensity image if necessary 3 if ~isa(a,'double') && ~isa(a,'single') 4 a = im2single(a); 5 end 6 [m,n] = size(a); 7 % The output edge map: 8 e = false(m,n);
1、高斯平滑
GaussianDieOff是高斯衰变值,小于该值的高斯函数值都视为0.
PercentOfPixelsNotEdges代表非边缘像素占总像素个数的百分比,默认为70%。ThresholdRatio是一个系数,lowThreshhold=ThresholdRatio*highThreshold。
sigma是高斯函数里的标准差,默认为1。高斯函数的模板大小最大为60*60,实际大小由GaussianDieOff和sigma共同决定
gau保存了1D的高斯模板,用来做高斯平滑。分别沿x和y轴做平滑
dgau2D保存了2D高斯函数对x的一阶偏导,为了做图像微分。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 % Magic numbers 2 GaussianDieOff = .0001; 3 PercentOfPixelsNotEdges = .7; % Used for selecting thresholds 4 ThresholdRatio = .4; % Low thresh is this fraction of the high. 5 % Design the filters - a gaussian and its derivative 6 sigma=1; % sigma是一个入参,默认是1,这句是我加的,为了代码可以正常运行 7 pw = 1:30; % possible widths 8 ssq = sigma^2; 9 10 % 为了确定高斯核大小,当函数值小于衰减系数时,就把他视为0 11 % 窗口大小为power(2*width) 12 width = find(exp(-(pw.*pw)/(2*ssq))>GaussianDieOff,1,'last'); 13 if isempty(width) 14 width = 1; % the user entered a really small sigma 15 end 16 t = (-width:width); 17 gau = exp(-(t.*t)/(2*ssq))/(2*pi*ssq); % the gaussian 1D filter 18 19 % Find the directional derivative of 2D Gaussian (along X-axis) 20 % Since the result is symmetric along X, we can get the derivative along 21 % Y-axis simply by transposing the result for X direction. 22 [x,y]=meshgrid(-width:width,-width:width); 23 dgau2D=-x.*exp(-(x.*x+y.*y)/(2*ssq))/(pi*ssq); 24 25 % Convolve the filters with the image in each direction 26 % The canny edge detector first requires convolution with 27 % 2D gaussian, and then with the derivitave of a gaussian. 28 % Since gaussian filter is separable, for smoot