Canny edge detection tutorial
Author: Bill green( 2002)
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:
(1) 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 imageSignificantly CES 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 wowould want to detect edges.
The Canny edge detection algorithm is known to operate as the optimal edge detector. the specified 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 shoshould 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 already ced by hysteresis. hysteresis is used to track along the remaining pixels that have not been suppressed. hysteresis uses two thresholds and if the magncategory is below the first threshold, it is set to zero (made a nonedge ). if the magncategory is above the high threshold, it is made an edge. and if the magncategory 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 med 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 magnweight (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 magnqueue, 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 ctions 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. 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:
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 ctions 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.
X
X
X a x
X
X
Therefore, any edge direction falling withinYellow Range(0 to 22.5 & 157.5 to 180 degrees) is set to 0 degrees. Any edge direction falling inGreen range(22.5 to 67.5 degrees) is set to 45 degrees. Any edge direction falling inBlue range(67.5 to 112.5 degrees) is set to 90 degrees. And finally, any edge direction falling withinRed 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 ction 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 abve 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.
You are visitor number: