top of page

DETECTING CONTOURS AND COUNTING SHAPES

Writer's picture: madrasresearchorgmadrasresearchorg

Author : Gowri Thusoo

Contours are abstract collections of points and segments corresponding to the shapes of the objects in the image. As a result, we can manipulate contours in our programs such as counting the number of contours, using them to categorize the shapes of objects, cutting out objects from an image (called image segmentation) and many other things. In simple words, contours can be described as a curve connecting all the continuous points (along the boundary), having the same intensity or colour. They are a helpful tool for shape analysis and object detection and recognition.
 

Contour Detection

By implementing contour detection, we can mark the borders of objects, and concentrate them in an image with ease. Contour detections take a binary image as an input which is the output of the canny edge detector or a binary image obtained by applying the global thresholding technique on a grayscale image. It calculates the boundaries of objects and makes a hierarchy of the object contours to keep the holes inside the parent objects. This information can be used to extract and draw any contour depending upon the user requirement.

Applications of Contours

Contour detection is helpful in various fields of machine learning. A couple of common applications are:

- Background (or Foreground Segmentation)

To replace the background of an image with another, you need to perform image-foreground extraction (similar to image segmentation). Segmentation can be performed by using contours.

- Motion Detection

Motion detection technology has numerous applications, ranging from indoor and outdoor security environments, traffic control, behaviour detection during sports activities, detection of unattended objects.

Using contours with OpenCV

OpenCV is an open-source library used mainly for processing images and videos to identify shapes, objects, text etc. It is used mostly in Python. Using contours with OpenCV, you can get a sequence of points of vertices of each white patch. You can even identify features of polygons such as convexity, concavity, equilateral etc by calculating and comparing distances between vertices. OpenCV makes it easy to find and draw contours in images. It provides two simple functions:

- findContours()

- drawContours()

Steps for detecting and counting Contours

The approach for this implementation consists of the following steps:

  • Importing the required modules

import cv2 
import matplotlib.pyplot as plt 
import imutils
  • Importing the image on which we have to detect objects using OpenCV

image=cv2.imread("/content/drive/MyDrive/cuaderno-papel-negro-lapiz-sobre-fondo-blanco_115509-84.jpg")

FIG:1

  • Blurring the image a bit if there are any shiny/sharp lines present

image = cv2.medianBlur(image,15)
  • Converting the image to grayscale format

image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)#toRGB gray_img=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)  #to grayscale
  • Applying binary thresholding on the image

#creating binary thresholded image _, binary = cv2.threshold(gray_img, 225, 255, cv2.THRESH_BINARY_INV) #displaying it plt.imshow(binary, cmap="gray") plt.show()

The output would be:

FIG:2

  • Finding out the contours

# finding the contours from the thresholded image contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  • Drawing the contours

# drawing all contours image = cv2.drawContours(image, contours, -1, (0, 255, 0), 2) plt.imshow(image) plt.show()
  • The detected contours are shown in the output:

FIG:3

  • Counting the objects

cnts = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts)
  • Displaying the result

print(len(cnts))

The output is: 2

Conclusion

In this article, we have explained how to detect contours using OpenCV. We also counted the number of objects detected by this algorithm. This algorithm works well with images with a relatively dark background, paired with a well-defined object. Although to make it work better for images otherwise, you can either perform edge detection or tweak the thresholding value.

GitHub Code:

References:

1. An Overview of Contour Detection Approaches

2. Detecting and Counting Objects with OpenCV | by Furkan Gulsen | Analytics Vidhya

3. How to Detect Contours in Images using OpenCV in Python - Python Code

4. Contour Detection using OpenCV (Python/C++)


Recent Posts

See All

Commentaires


Madras Scientific  Research Foundation

About US

 

Contact

 

Blog

 

Internship

 

Join us 

Know How In Action 

bottom of page