Canny edge detection is a popular image processing technique for extracting stable and thin image edges. In this tutorial we will describe how to extract the outlines of objects from photos and videos.
The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images. It was developed by John F. Canny in 1986. Canny also produced a computational theory of edge detection explaining why the technique works.Wikipedia
How to develop a Canny algorithm
The Canny Edge Detector is a multi-stage filter that produces stable image edges. As a result, the dominant objects in the scene receive well-connected contours.
Generally the algorithm works on gray-scale pictures. Therefore, at first we can use color space conversion to transform RGB image to grayscale.
In order to locate stable image edges with Canny edge detector you must perform the following steps.
Step 1: Reduce image noise
This is an important step, since edge detection is sensitive to image noise. The classical Canny algorithm relies on Gaussian blur for image denoising.
Step 2: Select a suitable edge detector
There is different edge detection algorithms and for good image detection we need to select a decent one. A common choice is the Sobel edge detector, but there other options as well.
Alternatives to Sobel operator
- Prewitt edge detector
- Schaar operator
Note that together with the edges we need to extract the parameters of the image gradients. In this way we will have preliminary information to help us in the further processing steps.
Step 3: Apply Non-maximum suppression to edge detection result
So far, we have an image with thick contours of the objects. As a next step, we will apply Non-maximum suppression to find sharp intensity changes. As a result, we will have thin object boundaries for further image analysis.
Step 4: Edge tracking with Hysteresis (Double threshold)
The core step of Canny algorithm is the edge tracking with hysteresis. This step detects “weak” edges that have connection with “strong” ones.
We define two threshold values. A strong pixel is one that has a value greater than the upper limit, while a weak pixel is one that is between the low and high ranges.
We consider the weak edge pixels as noise if they don’t have connection to at least one strong pixel.
With the implementation of this phase we get a list of connected contour lines of the objects in the scene.
How the edge linking works?
The brute force edge linking is too expensive for online machine vision. Therefore, we use an elegant smart edge tracing algorithm. The result is lists of pixels that belong only to strongly connected contour lines of the image.
The following steps describe how it works.
- As a prerequisite we define two threshold values (the hysteresis) – low and high grayscale values.
- Apply a low pass Gaussian filter to the input image to eliminate high-frequency noise
- Do an edge detection using a gradient operator like Sobel
- Apply non-maximum suppression to image contours
- Scan the picture to find the appearance of a “strong” edge pixel. Mark this pixel as visited one and add it to the edge list.
- From its 8-neighbors find a pixel that is bigger than the low limit value.
- Keep track of the direction from point 3. And mark the current pixel as visited.
- Use the same direction to search for the next contour pixel and again rotate by 8-connection.
- The result list contains the current edge line and we can repeat point 2 – 5 for the next one
- We can throw out the contour lines that are below a certain length.