Extracting Contours and Shapes from Images using OpenCV

OpenCV is a powerful open-source computer vision library that provides numerous functions for image processing and analysis. One of the key features of OpenCV is its ability to extract contours and identify different shapes from images. In this article, we will explore how to utilize OpenCV with Python to perform these tasks effortlessly.

Table of Contents

  1. What are Contours?
  2. Extracting Contours from Images
  3. Identifying Shapes
  4. Conclusion

1. What are Contours?

Contours can be defined as the boundaries of objects or shapes within an image. They are widely used in various computer vision applications such as object detection, image segmentation, and shape analysis. By extracting contours from an image, we can isolate specific objects and analyze their shape-related properties.

2. Extracting Contours from Images

Using OpenCV, extracting contours from images is a straightforward process. Let's walk through the necessary steps:

Step 1: Import Required Libraries

Before we begin, we need to import the necessary libraries. We require cv2 for image processing and matplotlib for visualizing the results. You can install these packages using pip.

import cv2
from matplotlib import pyplot as plt

Step 2: Read and Preprocess the Image

We start by reading an image using OpenCV's imread() function. Additionally, converting the image to grayscale enhances the contour extraction process.

image = cv2.imread('path/to/image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Step 3: Apply Thresholding

Thresholding is an essential step to convert the grayscale image into a binary image. It helps to segment the image and isolate the objects we are interested in.

_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

Step 4: Find and Draw Contours

Now, we can use OpenCV's findContours() method to detect contours in the binary image. This function returns a list of contours found in the image.

contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Step 5: Visualize Contours

To visualize the contours, we can loop through each contour and draw it on a copy of the original image using drawContours().

contour_image = image.copy()
cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 2)

Step 6: Display the Results

Finally, we can display both the original image and the image with drawn contours using imshow().

plt.subplot(121), plt.imshow(image), plt.title('Original Image')
plt.subplot(122), plt.imshow(contour_image), plt.title('Contours')
plt.show()

3. Identifying Shapes

After extracting the contours, we can also identify and classify the shapes present in an image. OpenCV provides a approxPolyDP() function to approximate a contour's shape into a simpler polygon. By examining the number of vertices of these polygons, we can classify the shapes accordingly.

For example, a polygon with three vertices indicates a triangle, four vertices represent a rectangle or square, and a higher number of vertices could indicate a polygon or circle.

4. Conclusion

Extracting contours and identifying shapes from images using OpenCV is a valuable technique in various computer vision applications. With its extensive set of functions and easy integration with Python, OpenCV enables developers to analyze images effectively. By following the steps outlined in this article, you should be able to extract contours and identify shapes effortlessly.


noob to master © copyleft