Adventures in Machine Learning

Detecting Contours in Images: A Step-by-Step Guide with OpenCV

Detecting Contours in an Image: A Step-by-Step Guide

Have you ever wondered how computers can detect the edges of an object in a picture? This is called contour detection, and it is a fundamental technique in computer vision.

It is useful in various applications, such as object recognition, shape analysis, and image segmentation. In this article, we will take you through the steps of detecting contours in an image using OpenCV, a popular computer vision library developed by Intel.

We will explain each step in detail and provide example code to help you implement it on your own. If you’re ready, let’s get started!

1. Importing Modules

Before we start, we need to import the necessary modules. OpenCV provides several functions for image processing and analysis, while matplotlib can be used to display the image and its contours.

1.1 Importing OpenCV and Matplotlib

import cv2
from matplotlib import pyplot as plt

2. Loading Image into the Program

The next step is to load the image into our program. We can use the imread function of OpenCV to read the image in RGB format.

2.1 Reading the Image

image = cv2.imread('example.jpg', cv2.IMREAD_COLOR)

3. Converting Image to Grayscale

The contour detection algorithm works on grayscale images. Therefore, we need to convert our image to grayscale before applying the algorithm.

3.1 Converting to Grayscale

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

4. Getting Binary Image

The contour detection algorithm requires a binary image as input. Therefore, we need to convert our grayscale image into a binary image.

4.1 Converting to Binary

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

The threshold function returns two values: the threshold value and the binary image.

We set the threshold value to 127, which means that all pixel values below 127 will be set to 0, and all pixel values above 127 will be set to 255.

5. Detecting Contours

Finally, we are ready to detect contours in our binary image. We can use the findContours function of OpenCV to do this.

5.1 Finding Contours

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

The findContours function returns two values: the contours themselves and a hierarchy of the contours. We set the retrieval mode to cv2.RETR_EXTERNAL, which means that we only want to retrieve the external contours.

We also set the contour approximation mode to cv2.CHAIN_APPROX_SIMPLE, which means that we want to approximate the contours as simple polygons.

6. Final Code for Detecting Contours

import cv2
from matplotlib import pyplot as plt

# Load the image
image = cv2.imread('example.jpg', cv2.IMREAD_COLOR)

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Convert the grayscale image to binary
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Detect contours in the binary image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours on the original image
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

# Display the image with contours
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()

In this code, we first load the image, convert it to grayscale, and then convert it to binary. We then use the findContours function to detect the contours in the binary image.

Finally, we use the drawContours function to draw the contours on the original image, and we display the image with contours using Matplotlib.

7. Results on a Different Image

To demonstrate the accuracy of contour detection, we applied the same method to a different image. Here’s the original image:

Original image

And here’s the result:

Image with contours

As you can see, the contour detection algorithm accurately detected the edges of the object in the image.

8. Conclusion

Contour detection is a fundamental technique in computer vision that can be useful in various applications. By following the steps outlined in this article, you can detect contours in an image using OpenCV and Python.

We hope you found this article informative and helpful. Happy coding!

Contour detection is an essential technique in computer vision, and mastering these techniques is highly recommended for a budding computer vision engineer.

Popular Posts