Canny Edge Detection Tutorial(Canny 边缘检测教程)

来源:互联网 发布:阿尔法橱柜门软件 编辑:程序博客网 时间:2024/05/20 14:40

关于此算法的代码可以再http://blog.csdn.net/caiye917015406/article/details/7863825找到

 

This tutorial assumes the reader:
(1) Knows how to develop source code to read raster data
(2) Has already read my Sobel edge detection tutorial

This tutorial will teach you how to:这个教程将教你:
(1) Implement the Canny edge detection algorithm. 实现Canny边缘检测算法

                                              

INTRODUCTION

Edges characterize boundaries and are therefore a problem of fundamental importance in image processing. Edges in images are areas with strong intensity contrasts � a jump in intensity from one pixel to the next. Edge detecting an imagesignificantly reduces the amount of data and filters out useless information, while preserving the important structural properties in an image. This was also stated in my Sobel and Laplace edge detection tutorial, but I just wanted reemphasize the point of why you would want to detect edges.

边缘描述边界在图像的预处理中是一个重要的问题。边缘是像素点之间越跳比较明显的区域。边缘提取可以明显的减少数据量和滤去一些没用的信息,同时,它保留了图像的重要的结构性质。这些在前一篇的文章已经介绍了,但是,我还是想再次强调一下你为什么要做边缘提取。

The Canny edge detection algorithm is known to many as the optimal edge detector. Canny's intentions were to enhance the many edge detectors already out at the time he started his work. He was very successful in achieving his goal and his ideas and methods can be found in his paper, "A Computational Approach to Edge Detection". In his paper, he followed a list of criteria to improve current methods of edge detection. The first and most obvious is low error rate. It is important that edges occuring in images should not be missed and that there be NO responses to non-edges. The second criterion is that the edge points be well localized. In other words, the distance between the edge pixels as found by the detector and the actual edge is to be at a minimum. A third criterion is to have only one response to a single edge. This was implemented because the first 2 were not substantial enough to completely eliminate the possibility of multiple responses to an edge.

Canny边缘检测算法是被人熟知的最佳边缘检测算法,在Canny开始工作的时候,他就想提高边缘提取算法。他已经很成功的实现了他的目标和想法,他的思路可以参考“Computational Approach to Edge Detection”。在这篇论文中,他通过一系列标准来提高目前的边缘检测算法。第一个是高精度,在图像边缘检测既不忽略边界也不对非边界标记是非常重要的。第二个标准是边缘点能本地化,换句话说,就是实际边缘点与测的边缘点的距离是一个很小的值。第三个准则是单个边缘对应一个处理。对于第三个标准的提出时因为前两个标准不能使好几个计算值反映一个边缘。

 

Based on these criteria, the canny edge detector first smoothes the image to eliminate and noise. It then finds the image gradient to highlight regions with high spatial derivatives. The algorithm then tracks along these regions and suppresses any pixel that is not at the maximum (nonmaximum suppression). The gradient array is now further reduced by hysteresis. Hysteresis is used to track along the remaining pixels that have not been suppressed. Hysteresis uses two thresholds and if the magnitude is below the first threshold, it is set to zero (made a nonedge). If the magnitude is above the high threshold, it is made an edge. And if the magnitude is between the 2 thresholds, then it is set to zero unless there is a path from this pixel to a pixel with a gradient above T2.

基于这些标准,Canny边缘提取算法首先对图像进行平滑处理来消除噪声。接下来,通过空间的导数值来求的突出区域的梯度。然后,该算法找到这些区域,抑制 不是最大的值(非最大值抑制)。这些梯度级通过滞后阀值来削弱。滞后阀值方法来追踪那些没被抑制的像素点。滞后阀值使用两个阀值来处理像素点,若像素点值低于最小阀值就被设为0,若大于最大的阀值就被标记为边缘。处于两者之间的梯度级的像素点,若果这个像素点连接一个大于最大阀值的像素点,那么就是边界,否则就设为0。

Step 1
In order to implement the canny edge detector algorithm, a series of steps must be followed. The first step is to filter out any noise in the original image before trying to locate and detect any edges. And because the Gaussian filter can be computed using a simple mask, it is used exclusively in the Canny algorithm. Once a suitable mask has been calculated, the Gaussian smoothing can be performed using standard convolution methods. A convolution mask is usually much smaller than the actual image. As a result, the mask is slid over the image, manipulating a square of pixels at a time. The larger the width of the Gaussian mask, the lower is the detector's sensitivity to noise. The localization error in the detected edges also increases slightly as the Gaussian width is increased. The Gaussian mask used in my implementation is shown below.

为了进行边缘提取,需要做一些预处理。第一步要进行滤波来滤去噪声。因为高斯滤波采用一个简单的掩码,所以它通常应用于Canny算法。一旦一个合适的掩码被确定,高斯滤波就可以进行标准的卷积计算。一个卷积往往要小于原始图像。因此,掩码要在原始图像上滑动,每一次计算一个区域的值。高斯掩码越大,对噪声越敏感。随着高斯掩码的增大,对应的边界检测的误差越大。以下是我们所用的高斯掩码:

Step 2
After smoothing the image and eliminating the noise, the next step is to find the edge strength by taking the gradient of the image. The Sobel operator performs a 2-D spatial gradient measurement on an image. Then, the approximate absolute gradient magnitude (edge strength) at each point can be found. The Sobel operator uses a pair of 3x3 convolution masks, one estimating the gradient in the x-direction (columns) and the other estimating the gradient in the y-direction (rows). They are shown below:

进行完平滑处理和滤波处理,下一步是通过图像的梯度求的边缘强度。用Sobel运算来求的二维图像的梯度。(具体见上一篇)

 

The magnitude, or EDGE STRENGTH, of the gradient is then approximated using the formula:

|G| = |Gx| + |Gy|

Step 3
Finding the edge direction is trivial once the gradient in the x and y directions are known. However, you will generate an error whenever sumX is equal to zero. So in the code there has to be a restriction set whenever this takes place. Whenever the gradient in the x direction is equal to zero, the edge direction has to be equal to 90 degrees or 0 degrees, depending on what the value of the gradient in the y-direction is equal to. If GY has a value of zero, the edge direction will equal 0 degrees. Otherwise the edge direction will equal 90 degrees. The formula for finding the edge direction is just:

一旦求的x和y方向的梯度,就可以很容易的求的边界方向。然而,当强度为0时,会差生错误,因此,在代码中进行了处理。当x方向的梯度等于0时,边界的方向将被设置未0或者90.进一步确定,若y方向的梯度也等于0,那么边界方向被设置未0,否则被设置为90.以下是寻找边缘方向的公式:

theta = invtan (Gy / Gx)

Step 4
Once the edge direction is known, the next step is to relate the edge direction to a direction that can be traced in an image. So if the pixels of a 5x5 image are aligned as follows:

一旦求的边缘方向,下一步就是将边界方向与图像联系起来。因此,若5*5的图像如下图:

x     x     x     x     x
x     x     x     x     x
x     x     a     x     x
x     x     x     x     x
x     x     x     x     x

Then, it can be seen by looking at pixel "a", there are only four possible directions when describing the surrounding pixels -0 degrees (in the horizontal direction), 45 degrees (along the positive diagonal),90 degrees (in the vertical direction), or135 degrees (along the negative diagonal). So now the edge orientation has to be resolved into one of these four directions depending on which direction it is closest to (e.g. if the orientation angle is found to be 3 degrees, make it zero degrees). Think of this as taking a semicircle and dividing it into 5 regions.



 


Therefore, any edge direction falling within the yellow range (0 to 22.5 & 157.5 to 180 degrees) is set to 0 degrees. Any edge direction falling in thegreen range (22.5 to 67.5 degrees) is set to 45 degrees. Any edge direction falling in theblue range (67.5 to 112.5 degrees) is set to 90 degrees. And finally, any edge direction falling within thered range (112.5 to 157.5 degrees) is set to 135 degrees.

Step 5
After the edge directions are known, nonmaximum suppression now has to be applied. Nonmaximum suppression is used to trace along the edge in the edge direction and suppress any pixel value (sets it equal to 0) that is not considered to be an edge. This will give a thin line in the output image.

确定边界的方向后,下一步就是应用非最大抑制算法。非最大抑制算法在边缘沿着边缘方向来抑制任何不被认为为边缘的点。这将使得输出图像的边缘线变细。

 

Step 6
Finally, hysteresis is used as a means of eliminating streaking. Streaking is the breaking up of an edge contour caused by the operator output fluctuating above and below the threshold. If a single threshold, T1 is applied to an image, and an edge has an average strength equal to T1, then due to noise, there will be instances where the edge dips below the threshold. Equally it will also extend above the threshold making an edge look like a dashed line. To avoid this, hysteresis uses 2 thresholds, a high and a low. Any pixel in the image that has a value greater than T1 is presumed to be an edge pixel, and is marked as such immediately. Then, any pixels that are connected to this edge pixel and that have a value greater than T2 are also selected as edge pixels. If you think of following an edge, you need a gradient of T2 to start but you don't stop till you hit a gradient below T1.

 

这是从网上摘抄http://blog.csdn.net/carson2005/article/details/7516434

Canny算子是John.F.Canny20世纪80年代提出的一种多级边缘检测算法。该算子最初的提出是为了能够得到一个最优的边缘检测,即:检测到的边缘要尽可能跟实际的边缘接近,并尽可能的多,同时,要尽量降低噪声对边缘检测的干扰。

Canny算子边缘检测算法的计算步骤: