Home » Blog » Tutorials » Canny edge detection: How to extract stable image contours

Canny edge detection: How to extract stable image contours

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.

Canny edge detector Lego demo
Example result of Canny Edge Detector

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.

Alternative methods for image filtering are described into our lesson on Symmetric Nearest Neighbor filter and 2D Median filtering.

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

Sobel operator with gaussian blur with sigma 3
Gaussian blur with sigma=3.0 and Sobel 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.

Gradient Non-maximum suppression
Example picture of Non-maximal suppression of the Sobel operator

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.

Edge trace with hysteresis threshold
Example of edge tracing with double threshold (hysteresis)

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.

  1. As a prerequisite we define two threshold values (the hysteresis) – low and high grayscale values.
  2. Apply a low pass Gaussian filter to the input image to eliminate high-frequency noise
  3. Do an edge detection using a gradient operator like Sobel
  4. Apply non-maximum suppression to image contours
  5. 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.
  6. From its 8-neighbors find a pixel that is bigger than the low limit value.
  7. Keep track of the direction from point 3. And mark the current pixel as visited.
  8. Use the same direction to search for the next contour pixel and again rotate by 8-connection.
  9. The result list contains the current edge line and we can repeat point 2 – 5 for the next one
  10. We can throw out the contour lines that are below a certain length.

Source Code

References

Canny edge detector – Wikipedia

WEB APPLET

See how it works in the browser!

Picture Outlines Extraction - SVG

You can support me at ko-fi.com