Home » Blog » Image Processing » Canny edge detection: How to extract stable image contours
Canny edge detector algorithm

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 object outlines from photos and pictures.

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 algorithm is a multi-stage filter that produces stable image edges. As a result, the dominant objects in the scene receive well-connected contours. The next few points provide details on how to develop a Canny edge detector.

Canny edge detector Lego demo
Example result of Canny Edge Detector

Typically, the algorithm works on gray-scale images. Therefore, at first we can use color space conversion to transform RGB image to grayscale.

The following steps describe how to find stable image edges using the Canny operator:

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

The classic Canny algorithm uses the Sobel edge detector for image contours extraction. However, the algorithm can also work well with other gradient detectors. Some popular alternatives are:

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: Edge thinning

Typically, edge detectors result in thick contour lines, which can make further processing difficult. To improve the border detection result, the Canny algorithm applies an edge thinning procedure by non-maximum suppression.

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

Step 4: Hysteresis trsholding (Double threshold)

An important step in the Canny operator is hysteresis thresholding. This operation finds “weak” edges that have a connection to “strong”.

The hysteresis threshold uses two limit values – low and high and therefore we can also meet it as a double threshold. 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. Pixels that have no connection to a strong pixel are assumed to be noise and therefore discarded.

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

With the implementation of this phase we get a list of connected contour lines of the objects in the scene.

How the edge tracing works?

To implement a Canny edge detector, we need a decent edge tracking algorithm. The brute force edge tracing is too expensive for online machine vision. Fortunately, there is an elegant smart edge tracing algorithm.

This section provides an overview of how to retrieve lists of pixels that belong only to strong contour lines in an image. The following steps describe how it works.

  1. Define two threshold values (the hysteresis)
  2. Scan the picture to find the appearance of a “strong” edge pixel.
  3. Mark this pixel as visited one and add it to the edge list.
  4. From its 8-neighbors find a pixel that is bigger than the low limit value.
  5. Keep track of the direction from point 4 and move to the new pixel
  6. Use the same direction to search for the next contour pixel and repeat point 3 – 6
  7. The result list contains the current edge line and we can repeat point 2 – 5 for the next one
  8. We can throw out the contour lines that are below a certain length.

Source Code

Useful Resources

Online Tools

The Free Online Image Applications section contains various edge detection tools and utilities.

WEB APPLET

See how it works in the browser!

Picture Outlines Extraction - SVG

Conclusion

The Canny operator is a multi stage image processing algorithm for edge detection. This blog post gives an overview of what the Canny’s edge detection is and how to use it to extract a list of objects contours. The article discusses how to reduce image noise using Gaussian blur and how to make thin edges using non-maximal suppression.

In case you found this article helpful, you can read more about computer graphics and programming tutorials in the blog here.

Transform Your Images: Explore Our Must-Have Tools!