Edge Detection in Opencv 4.0, A 15 Minutes Tutorial

Edge Detection on Still and Moving objects.

Dan Ringwald
Sicara's blog
3 min readMar 12, 2019

--

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 load, modify 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?

Read the full article here.

--

--