Contour Detection

Learn to detect different types of shapes.

We'll cover the following

In this lesson, we’ll learn to detect contours, which will help us find different types of shapes in our image. This can be used for motion detection, object detection, and background removal, among other things.

First, we need to convert our image to grayscale. This is really important because it helps us correctly find the edges. It also helps in simplifying algorithms and eliminates the complexity of the computational requirements. We use the edge detection algorithm to detect edges and the retrieval algorithm to detect shapes.

cv::cvtColor(img, src_gray, COLOR_BGR2GRAY);

To convert an image to a Canny image, we use the Canny() function of the OpenCV library. This function requires four parameters:

  • The first parameter is the input image.
  • The second parameter is the output image.
  • The third parameter is the minimum value of the intensity gradient.
  • The fourth parameter is the maximum value of the intensity gradient.
cv::Canny( src_gray, canny_img, 50, 50);

This function converts the image to black and white, highlighting the edges of the contours. It makes the work of the contour retrieval algorithm easier.

Detect shapes

To detect shapes, we use the findContours() function of the OpenCV library. This function requires five parameters, which are listed below:

  • The first parameter is the Canny image.
  • The second parameter is the variable contours, which will store the shapes detected. We use this value to draw on those shapes later on.
  • The third parameter is the variable hierarchy that stores the hierarchy of the shapes detected.
  • The fourth parameter is the mode to find the contours. We use RETR_EXTERNAL to find the external contours. It’s one of the modes of the contour retrieval algorithm and is used to retrieve only the outer contours.
  • The fifth parameter is the contour approximation method. For this, we use CHAIN_APPROX_NONE. It stores all the contours found by our retrieval algorithm.
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(canny_img, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

We’ve initialized the variables, such as contours and hierarchy, that we’re going to use. We’ve initialized contours as an array of Point, a point in 2D space. We’ve also initialized hierarchy as an array of Vec4i, a vector with four dimensions. Then we’ll use the findContour() function.

The retrieval algorithms look for the edges in the Canny image to detect the shapes.

Draw contours

To draw the contours, we use the drawContours() function of the OpenCV library. The syntax of this function is:

cv::drawContours(img, contours, -1, Scalar(255, 0, 255), 2);

We need to give five parameters to the function. They are listed below:

  • img is the image on which we want to draw contours.
  • contours are the contours that we detected using the findContours() function.
  • -1 is the contour index. It’s the number of contours we want to draw. For example, if we only want to draw the third contour, we give the value 2. In this example, we enter the value -1 because we want to draw all the contours.
  • The fourth parameter is the scalar value of the color to be given while drawing.
  • The fifth parameter is the thickness of the drawing.

Get hands-on with 1400+ tech skills courses.