This tutorial will teach you how to:
Implement the Canny edge detection algorithm.
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 image significantly 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.
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.
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.
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:
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:
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:
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), or 135 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 the green range (22.5 to 67.5 degrees) is set to 45 degrees. Any edge direction falling in the blue range (67.5 to 112.5 degrees) is set to 90 degrees. And finally, any edge direction falling within the red 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.
Canny边缘检测
图像边缘检测的算法:
导言
边缘特征在图像处理中是一个极为重要的问题。在图像的边缘地区,强度强的反差强度从一个像素猛增到下一个。边缘检测的图像大大减少了大量的数据,并过滤掉无用的信息,同时保持重要的结构性能的特征。这也是索贝尔和拉普拉斯边缘检测教程中提到过的,但需要再次强调这一点,为什么要检测的边缘呢。
图像边缘检测算法是众所周知的被许多人视为最佳的边缘检测。意图是在于要加强许多已经存在的边缘探测器。他开始了他的工作,并很成功地实现了他的目标,从他的思想和方法中可以找到他的论文“边缘检测的计算方法” 。在他的文件中,他遵循的标准清单,以改善目前的边缘检测方法。第一个也是最明显的是错误率低。重要的是,在图像边缘出现不应该错过,而且不应有任何非边缘的反应。第二个标准是,边缘点很好地本地化。换言之,距离之间的边缘像素作为探测器发现和实际边缘要在最低限度。第三个标准是,只有一个回应单一优势。这是因为实施前2不大,不足以完全消除了一个边缘多个响应的可能性。
根据这些标准, 图像边缘检测器的第一个平滑的图像,以消除和噪音。然后认定的形象,以突出地区梯度高空间衍生物。该算法然后轨道沿着这些地区和抑制任何像素这不是在最高( 非最大限度制止) 。梯度阵列现在进一步减少滞后。磁滞用来追踪沿其余像素,但没有压制。磁滞使用两个阈值,如果规模低于第一道门槛,这是设置为零(发了nonedge ) 。如果是规模以上的高门槛,这是一个优势。如果震级之间的2阈值,那么它设置为零,除非有一条从这个像素一个像素的梯度上述时刻。
第1步 :
为了落实图像边缘检测算法,一系列步骤必须遵循。第一步是筛选出任何噪音的原始图像在寻找和发现任何边缘。而且因为高斯滤波器可以用一个简单的计算面具,它是专门用于在Canny算法。一旦合适的面罩已计算,高斯平滑可以用标准的卷积方法。阿卷积掩模通常远远小于实际的形象。因此,该面具是下跌的形象,操纵一个正方形像素的时间。较大的宽度高斯面具,较低的是探测器的敏感性噪音。定位误差检测边缘也略有增加的高斯宽度增加。Gaussian使用我在执行下面显示。
第2步 :
经过平滑的形象,消除噪音,下一步就是要找到优势兵力,采取梯度的形象。的Sobel算子进行二维空间梯度测量的形象。然后,大约绝对梯度幅度(边缘强度)各点可以找到。 Sobel算子的使用对3x3卷积口罩,一个梯度估计在X方向(栏)和其他的梯度估计的Y方向(行) 。它们如下所示:
的规模,或EDGE强度,梯度近似然后使用公式:
|G| = |Gx| + |Gy|
第3步 :
寻找边缘方向是小事,一旦梯度在X和Y方向是众所周知的。然而,你会产生错误时sumX等于零。因此,在代码中必须有一个规定只要发生。每当梯度在x方向等于零,边缘的方向,必须等于90度或0度,取决于什么的价值梯度的Y方向等于。如果青的值为零,边缘方向将等于0度。否则边缘方向将等于90度。公式为寻找边缘方向是:
theta = invtan (Gy / Gx)
第4步 :
一旦边缘方向确定,下一步是与边缘方向为方向,可以追溯到在一个图像。因此,如果一个5x5像素图像对齐如下:
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
然后,可以看到看像素的“ A ” ,只有4个可能的方向时,描述了周围的像素- 0度(水平方向) , 45度(沿积极对角线) , 90度(垂直方向) ,或135度(沿负对角线) 。所以,现在的边缘方向已经得到解决纳入其中四个方向取决于哪个方向,它是最接近于(如角被发现有3度,使零摄氏度) 。认为这是采取了半圆形和成5个地区。
因此,任何先进的方向范围内的黄色范围( 0至5月22日& 157.5至180度)设置为0度。任何先进的方向下滑的绿色范围( 22.5至67.5度)设置为45度。任何先进的方向下滑的蓝色范围( 67.5至112.5度)设置为90度。最后,任何先进的方向范围内的红色范围( 112.5到157.5度)设置为135度。
第5步 :
在被称为边缘方向, 非最大限度制止目前适用。 非最大限度抑制是用来追踪沿边缘方向和制止任何像素值(套等于0 )这不是被认为是优势。这将让细线在输出图像上。
第6步 :
最后,滞后是用来作为消除条纹手段。条纹是打破了一个操作边缘轮廓上面的输出造成的波动及以下的门槛。如果一个阈值,T1讯号适用于图像,并具有优势的平均强度相等的T1 ,然后由于噪声,将先进的情况下,低于门槛边缘。同样也将延长超过阈值决策的优势看起来像一条虚线。为了避免这种情况,迟滞使用2门槛,一高一低在图像的任何有大于T1的像素值被假定为一个边缘像素,并立即标记等。然后,任何像素连接到这个边缘的像素,而像素值也比T2的大选定为边缘像素。如果您认为下列优势,你需要一个渐变的T2开始,但你不停止,直到你触及梯度低于T1的梯度。下载本文