Python OpenCV cv2 | Find Contours in Image

Python has a large variety of libraries and modules which support programmers to complete different machine learning and artificial intelligence tasks. The Python OpenCV library performs computer vision tasks such as detecting objects, tracking the body, face detection, etc. Finding contours in images is a very important part while performing these tasks.

This write-up will demonstrate an overview of how to find contours in an image by utilizing Python OpenCV with an example. The following aspect is elaborated on in this article:

So, let’s get started!

How to Find Contours in Image Using OpenCV cv2?

Contours are continuous joining lines along the boundary of an object having the same intensity. To find contours in Python, the “cv2.findContours()” function and the “cv2.drawcontours()” function of the OpenCV library are used.

The syntax of “cv2.findContours()” is shown below:

cv2.findContours(src, contour_retrieval, contours_approximation)

In the following syntax:

  • The parameter “src” takes the value of the source image.
  • The second parameter, “contour_retrieval”, indicates the contour mode. There are different modes used for this parameter, one of them is “RETR_TREE”, which not only returns “all” the contours, but the major purpose of this attribute is to create\return a family hierarchy of contours.
  • The “contour_approximation” parameter stores the set points at the boundary of an object. This indicates whether all boundary points or only endpoints are going to be captured. The contour approximation method values are “cv2.CHAIN_APPROX_NONE” and “cv2.CHAIN_APPROX_SIMPLE”.

Steps for finding contours in an image

The below steps should be followed to find contours in the image:

  • The “cv2.imread()” function is utilized to read an input image.
  • Convert a colored image into Grayscale using “cv2.COLOR_BGR2GRAY”.
  • Obtaining the image threshold value using the threshold function of OpenCV “cv2.threshold()”.
  • Next step is to find the contours of the threshold image using a function named “cv2.findContours()”.
  • To draw contours on an image, the function “cv2.drawContours()” is used.

The following image is used in the upcoming example to find and draw contours.

Example: Finding and Drawing Contours of the Given Image

In the following example, different OpenCV functions are used for finding and drawing contours. Let’s understand it in detail via the following example:

Code:

import cv2
import numpy as np

img_read = cv2.imread('F:\itslinuxfoss\documents\itslinuxfoss.png', -1)
grey_image = cv2.cvtColor(img_read,cv2.COLOR_BGR2GRAY)

ret,thresh_img = cv2.threshold(grey_image,127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

img_contours = np.zeros(img_read.shape)
cv2.drawContours(img_contours, contours, -1, (255,0,0), 3)
cv2.imshow('F:\itslinuxfoss\documents\contours.png',img_contours)

In the following code:

  • Two libraries named “cv2” and “NumPy” are imported in the program.
  • The “cv2.imread()” function reads the image from the given path and takes the flag parameter value “-1”, which indicates that the loaded image will be in source format.
  • The function “cv2.cvtcolor()” is used to convert the image into grayscale. It takes the read image variable in its first parameter and commands “cv2.COLOR_BGR2GRAY” in the second parameter. This command converts RGB mode image into Grayscale image. We convert RGB images to grayscale to simplify our computation and algorithms.
  • The OpenCV function “cv2.threshold()” takes the grayscale value, threshold minimum and maximum value, and threshold technique as an argument.  Different threshold techniques are used for this function. We are using a simple technique named “cv2.THRESH_BINARY”. In this technique, if the pixel intensity value is greater than our limit intensity, then the pixel is set to “255” otherwise, “0”.
  • The “cv2.findcontours()” takes three argument values, the first argument refers to the source image and the second argument value is the contour retrieval mode. The last parameter takes the value of the contour approximation method. We use the “cv2.CHAIN_APPROX_SIMPLE” approximation method in this particular example.
  • The “np.zeros()” function takes the image’s shape and returns a new array.
  • The “cv2.drawcontour()” takes five values as a parameter. The first parameter indicates the input source image. The second parameter, “contours”, indicates the found contours from the given image. The third parameter value, “-1”, indicates that all the contour points obtained from the image would be drawn on the image. The four parameters assign the color of contour points, and the fifth parameter is used to initialize the thickness of the contour.

Output:

The output shows the contours of the given image.

Conclusion

In Python, the “cv2.findContours()” is used to find contours in a given image, and “cv2.drawContours()” is used to draw the contour on the image. To find contours, different functions of OpenCV are also used along with these two main functions. To find contours, the colored image is converted into grayscale, and the threshold value is applied using the “cv2.threshold()” function. The threshold value is passed into “cv2.findContours()” to find the contour points. This guide provides a comprehensive overview of finding contours in Python using OpenCV cv2.