April 8, 2022 • 2 min read

How to deal with Edge Detection in Opencv 4.0, A 15 Minutes Tutorial

Rédigé par Alumni Sicara

Alumni Sicara

This tutorial will teach you, with examples, two OpenCV techniques in python to deal with edge detection.

This tutorial will teach you, with examples, two OpenCV techniques in python to deal with edge detection.

Getting Up and Running

Example of what can be achieved with this tutorial

So you want to build a super cool computer vision tool? You will need to isolate objects’ or persons’ contours to simplify your video.

But first, let’s begin this tutorial with the basics. This tutorial begins with how to loadmodify and display a video with OpenCV 4.0 in Python.

Follow a tutorial to install OpenCV and find a video you want to play with (I use this video). Finally, fire your favorite text editor to run this example:

Below is the result this example yields using an excerpt of a French TV show:

The raw data I work on, as displayed by OpenCV

Still objects edge detection

The Canny Filter

Let’s jump to the extraction of the edges in the scene.

The most famous tool to perform this task in OpenCV is the Canny filter. It is based on:

  • the gradient of the image (the difference between two adjacent pixels)
  • a hysteresis filtering.

The hysteresis enables the selection of lines of adjacent pixels contrasting with their neighbors.

For the full sequence of steps of the Canny filter, please refer to the image below. Also, feel free to consult this very comprehensive description of the steps of the Canny Filter.

The successive steps of the Canny filter

Check out the example below to check how the Canny Filter is used in OpenCV:

From left to right: the original image, the canny filter output with low thresholds, the canny filter output with high thresholds

The Canny filter thresholds can be tuned to catch only the strongest edges and get cleaner contours. The higher the thresholds, the cleaner the edges.

Rough threshold values are most of the time enough to do the job. This Quora thread will help you understand How to set the thresholds in canny edge detection?

The bilateral filtering

Notice how the plant in the background or the left man’s tie both look messy? A nice trick to smooth out the image without blurring the edges is called bilateral filtering. Take a look at the job the OpenCV bilateralFilter function does:

Applying the Canny Filter without (middle) and with (right) a bilateral filtering. The bilateral filter smooths out all the textures and keeps only the edges !

Moving objects edge detection

What if we don’t care about the tables and walls in the background? The way this tutorial will present you to extract moving objects contours is the background subtraction.

The next example presents the createBackgroundSubtractorMOG2 function of OpenCV. It extracts the moving parts of the images (middle image below). Pay attention to the morphologyEx function:

  • It dilates the moving objects
  • Then shrinks them back.

It joins together the white patches outputted by the background subtractor.

Output of the background subtractor in the middle, Output of the morphologyEx on the right.

The moving part of the image is then used as a mask. It crops the edges computed with canny, keeping only the moving part.

The previously computed Canny edge are in the middle. The right picture corresponds to the canny edges, restricted to the moving area computed by the background subtractor.

The bad video quality makes the mask on the moving objects jumps a bit, and we need more smoothing and filtering. Still, with OpenCV we can achieve:

  • a perfect still image edge detection
  • a satisfactory moving image edge detection

in a matter of minutes.

Thank you for going through this OpenCV tutorial.

Thanks to Laurent Montier, Charlotte Clément-Cottuz, Raphaël Meudec, Florian Carra, and Flavian Hautbois.

If you are looking for Computer Vision expert's, don't hesitate to contact us !

Cet article a été écrit par

Alumni Sicara

Alumni Sicara