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.
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.
Using OpenCV, extracting contours from images is a straightforward process. Let's walk through the necessary steps:
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
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)
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)
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)
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)
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()
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.
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