Canny Edge Detection Step by Step in Python - Computer Vision - by Sofiane Sahir - Towards Data Science
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
1.7K 20
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.
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)
1. Noise reduction;
2. Gradient calculation;
3. Non-maximum suppression;
4. Double threshold;
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
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:
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
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
Then, the magnitude G and the slope θ of the gradient are calculated as
follow:
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
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.
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
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.
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:
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;
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
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.
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)
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.
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
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
Image Processing
Intelligent Automation and Process Mining Sales Engineer at UiPath — Machine Learning
and Deep Learning enthusiast.
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
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?
30 4K 48
Theo Wolf in Towards Data Science Rahul Nayak in Towards Data Science
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
60 56
Lists
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
4 min read · Dec 19, 2023 7 min read · Dec 19, 2023
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
https://towardsdatascience.com/canny-edge-detection-step-by-step-in-python-computer-vision-b49c3a2d8123 24/24