0% found this document useful (0 votes)
65 views24 pages

Canny Edge Detection Step by Step in Python - Computer Vision - by Sofiane Sahir - Towards Data Science

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views24 pages

Canny Edge Detection Step by Step in Python - Computer Vision - by Sofiane Sahir - Towards Data Science

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Get unlimited access to the best of Medium for less than $1/week. Become a member

Canny Edge Detection Step by Step


in Python — Computer Vision
Sofiane Sahir · Follow
Published in Towards Data Science · 6 min read · Jan 25, 2019

1.7K 20

W hen it comes to image classification, the human eye has the


incredible ability to process an image in a couple of milliseconds,
and to determine what it is about (label). It is so amazing that it can do it
whether it is a drawing or a picture.

Open(Right):
Drawing of a car (Left) — Real car picture in app The human eye is able to classify both.

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 1/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

3
Search Write
The idea today is to build an algorithm that can sketch the edges of any
object present on a picture, using the Canny edge detection algorithm.

First of all, let’s describe what is the Canny Edge Detector:

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)

The Canny edge detection algorithm is composed of 5 steps:

1. Noise reduction;

2. Gradient calculation;

3. Non-maximum suppression;

4. Double threshold;

5. Edge Tracking by Hysteresis.

After applying these steps, you will be able to get the following result:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 2/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Original image on the left — Processed image on the right

One last important thing to mention, is that the algorithm is based on


grayscale pictures. Therefore, the pre-requisite is to convert the image to
grayscale before following the above-mentioned steps.

Noise Reduction
Since the mathematics involved behind the scene are mainly based on
derivatives (cf. Step 2: Gradient calculation), edge detection results are highly
sensitive to image noise.

One way to get rid of the noise on the image, is by applying Gaussian blur to
smooth it. To do so, image convolution technique is applied with a Gaussian
Kernel (3x3, 5x5, 7x7 etc…). The kernel size depends on the expected

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 3/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

blurring effect. Basically, the smallest the kernel, the less visible is the blur.
In our example, we will use a 5 by 5 Gaussian kernel.

The equation for a Gaussian filter kernel of size (2k+1)×(2k+1) is given by:

Gaussian filter kernel equation

Python code to generate the Gaussian 5x5 kernel:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 4/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Gaussian Kernel function

After applying the Gaussian blur, we get the following result:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 5/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Original image (left) — Blurred image with a Gaussian filter (sigma=1.4 and kernel size of 5x5)

Gradient Calculation
The Gradient calculation step detects the edge intensity and direction by
calculating the gradient of the image using edge detection operators.

Edges correspond to a change of pixels’ intensity. To detect it, the easiest way
is to apply filters that highlight this intensity change in both directions:
horizontal (x) and vertical (y)

When the image is smoothed, the derivatives Ix and Iy w.r.t. x and y are
calculated. It can be implemented by convolving I with Sobel kernels Kx and
Ky, respectively:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 6/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Sobel filters for both direction (horizontal and vertical)

Then, the magnitude G and the slope θ of the gradient are calculated as
follow:

Gradient intensity and Edge direction

Below is how the Sobel filters are applied to the image, and how to get both
intensity and edge direction matrices:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 7/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 8/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Blurred image (left) — Gradient intensity (right)

The result is almost the expected one, but we can see that some of the edges
are thick and others are thin. Non-Max Suppression step will help us
mitigate the thick ones.

Moreover, the gradient intensity level is between 0 and 255 which is not
uniform. The edges on the final result should have the same intensity (i-e.
white pixel = 255).

Non-Maximum Suppression
Ideally, the final image should have thin edges. Thus, we must perform non-
maximum suppression to thin out the edges.

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 9/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

The principle is simple: the algorithm goes through all the points on the
gradient intensity matrix and finds the pixels with the maximum value in the
edge directions.

Let’s take an easy example:

The upper left corner red box present on the above image, represents an
intensity pixel of the Gradient Intensity matrix being processed. The
corresponding edge direction is represented by the orange arrow with an
angle of -pi radians (+/-180 degrees).

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 10/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Focus on the upper left corner red box pixel

The edge direction is the orange dotted line (horizontal from left to right).
The purpose of the algorithm is to check if the pixels on the same direction
are more or less intense than the ones being processed. In the example
above, the pixel (i, j) is being processed, and the pixels on the same direction
are highlighted in blue (i, j-1) and (i, j+1). If one those two pixels are more
intense than the one being processed, then only the more intense one is
kept. Pixel (i, j-1) seems to be more intense, because it is white (value of 255).
Hence, the intensity value of the current pixel (i, j) is set to 0. If there are no
pixels in the edge direction having more intense values, then the value of the
current pixel is kept.

Let’s now focus on another example:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 11/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

In this case the direction is the orange dotted diagonal line. Therefore, the
most intense pixel in this direction is the pixel (i-1, j+1).

Let’s sum this up. Each pixel has 2 main criteria (edge direction in radians,
and pixel intensity (between 0–255)). Based on these inputs the non-max-
suppression steps are:

Create a matrix initialized to 0 of the same size of the original gradient


intensity matrix;

Identify the edge direction based on the angle value from the angle
matrix;

Check if the pixel in the same direction has a higher intensity than the
pixel that is currently processed;

Return the image processed with the non-max suppression algorithm.

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 12/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

The result is the same image with thinner edges. We can however still notice
some variation regarding the edges’ intensity: some pixels seem to be
brighter than others, and we will try to cover this shortcoming with the two
final steps.

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 13/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Result of the non-max suppression.

Double threshold
The double threshold step aims at identifying 3 kinds of pixels: strong, weak,
and non-relevant:

Strong pixels are pixels that have an intensity so high that we are sure
they contribute to the final edge.

Weak pixels are pixels that have an intensity value that is not enough to
be considered as strong ones, but yet not small enough to be considered
as non-relevant for the edge detection.

Other pixels are considered as non-relevant for the edge.

Now you can see what the double thresholds holds for:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 14/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

High threshold is used to identify the strong pixels (intensity higher than
the high threshold)

Low threshold is used to identify the non-relevant pixels (intensity lower


than the low threshold)

All pixels having intensity between both thresholds are flagged as weak
and the Hysteresis mechanism (next step) will help us identify the ones
that could be considered as strong and the ones that are considered as
non-relevant.

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 15/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

The result of this step is an image with only 2 pixel intensity values (strong
and weak):

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 16/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Non-Max Suppression image (left) — Threshold result (right): weak pixels in gray and strong ones in white.

Edge Tracking by Hysteresis


Based on the threshold results, the hysteresis consists of transforming weak
pixels into strong ones, if and only if at least one of the pixels around the one
being processed is a strong one, as described below:

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 17/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Hysteresis function

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 18/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Results of hysteresis process

All the code used is available in the following Git Repository

FienSoP/canny_edge_detector
Canny Edge detector library. Contribute to
FienSoP/canny_edge_detector development by creating an accou…
github.com

Bibliography

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 19/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

https://opencv-python-
tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_c
anny.html

https://homepages.inf.ed.ac.uk/rbf/HIPR2/canny.htm

http://justin-liang.com/tutorials/canny/

https://en.wikipedia.org/wiki/Canny_edge_detector

Computer Vision Edge Detection Machine Learning Data Science

Image Processing

Written by Sofiane Sahir Follow

160 Followers · Writer for Towards Data Science

Intelligent Automation and Process Mining Sales Engineer at UiPath — Machine Learning
and Deep Learning enthusiast.

More from Sofiane Sahir and Towards Data Science

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 20/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Sofiane Sahir Dario Radečić in Towards Data Science

Data science and Machine learning: Python One Billion Row Challenge
Competitions vs Real world… — From 10 Minutes to 4 Seconds
Who never heard about Kaggle competition The one billion row challenge is exploding in
website? Anyone interested in Data Science… popularity. How well does Python stack up?

5 min read · Aug 30, 2018 · 10 min read · May 8, 2024

30 4K 48

Theo Wolf in Towards Data Science Rahul Nayak in Towards Data Science

Kolmogorov-Arnold Networks: the Text to Knowledge Graph Made


latest advance in Neural Network… Easy with Graph Maker
The new type of network that is making waves An open-source library for building
in the ML world. knowledge graphs from text corpus using…

· 9 min read · May 13, 2024 11 min read · May 7, 2024

1.8K 18 1.3K 13

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 21/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

See all from Sofiane Sahir See all from Towards Data Science

Recommended from Medium

Vipin Zhe LIN

A Guide to Edge Detection and Line Digital Image Processing in C


Detection in Computer Vision usi… (Chapter 9): Thresholding, Robert…
Edge detection and line detection are Roberts, Prewitt, Sobel, Threshold, and Edge
techniques in computer vision that play a… Detection with Completed Code in C

4 min read · Jan 23, 2024 11 min read · Jan 9, 2024

60 56

Lists

Predictive Modeling w/ Practical Guides to Machine


Python Learning
20 stories · 1257 saves 10 stories · 1515 saves

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 22/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

Natural Language Processing data science and AI


1495 stories · 1014 saves 40 stories · 175 saves

Alakh Sharma Alberto Formaggio

Enhancing Image Light Intensity in Implementing the Hough


OpenCV using HSV Transform from Scratch
In the realm of computer vision and image
processing, OpenCV stands as a powerful to…

4 min read · Dec 19, 2023 7 min read · Dec 19, 2023

Ayesha sidhikha Girish Ajmera

Plotting with Matplotlib and HOG (Histogram of Oriented


Seaborn Gradients): An Amazing Feature…
Introduction Histogram of Oriented Gradients (HOG) is a
powerful feature extraction technique that is…

12 min read · Jan 24, 2024 4 min read · Jan 3, 2024

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 23/24
6/8/24, 3:52 AM Canny Edge Detection Step by Step in Python — Computer Vision | by Sofiane Sahir | Towards Data Science

68 1 57

See more recommendations

https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 24/24

You might also like