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
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:
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.
Check out the example below to check how the Canny Filter is used in OpenCV:
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:
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.
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 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 !