Detecting Rectangles and Squares in Images with OpenCV and Python
If you’re working with images in OpenCV, you may encounter situations where you need to detect certain shapes, such as rectangles and squares. Thankfully, with the help of the Python programming language and OpenCV library, it is possible to write code that can automatically detect these shapes in images.
In this tutorial, we’ll walk through how to detect rectangles and squares in images using OpenCV and Python. We’ll assume you have Python 3.x and OpenCV 4.x installed on your system.
Understanding the Problem
Before we dive into the code, let’s take a moment to understand what we’re trying to accomplish. Our goal is to develop an algorithm that can take an image as input and detect any rectangles and squares present in the image.
To do this, we’ll be using a process called contour detection. In OpenCV, a contour is a set of points that make up the boundary of an object in an image. By detecting contours in an image, we can identify the shapes present within it.
The Code
We’ll start by importing the necessary libraries and loading our test image into memory:
import cv2
import numpy as np
# Load the image
img = cv2.imread('test_image.png')
Next, we’ll convert the image to grayscale. This will make it easier to detect contours, as they’ll be represented as white lines on a black background.
# Convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply thresholding to create a black and white image
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]
With the image converted to grayscale and thresholded, we can now use the cv2.findContours()
function to detect the contours present in the image.
# Find contours in the image
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
We’ve used a couple of arguments with the cv2.findContours()
function. The first, thresh
, is the thresholded image we generated in the previous step. Next, we’ve told OpenCV to retrieve all of the contours present in the image (cv2.RETR_LIST
), and to approximate the contours using only their endpoints (cv2.CHAIN_APPROX_SIMPLE
).
Now that we’ve detected the contours, we can loop over them to identify which ones represent squares or rectangles. To do this, we’ll use the cv2.approxPolyDP()
function to approximate the contour as a polygon, and then look at the number of sides the polygon has.
# Loop over the contours
for c in contours:
# Approximate the contour as a polygon
approx = cv2.approxPolyDP(c, 0.01 * cv2.arcLength(c, True), True)
# If the polygon has four sides, it is likely a square or rectangle
if len(approx) == 4:
x, y, w, h = cv2.boundingRect(approx)
aspect_ratio = float(w)/h
# If the aspect ratio is close to 1, it is a square
if aspect_ratio >= 0.95 and aspect_ratio <= 1.05:
cv2.drawContours(img, [approx], 0, (0, 255, 0), 2)
# Otherwise, it is a rectangle
else:
cv2.drawContours(img, [approx], 0, (0, 0, 255), 2)
We’re using the cv2.approxPolyDP()
function to approximate each contour as a polygon. This allows us to get an idea of how many sides the contour has.
If the polygon has four sides, we assume it is either a square or rectangle. To differentiate between the two, we calculate the aspect ratio of the bounding rectangle of the polygon. If the aspect ratio is close to 1, we assume it is a square. Otherwise, we assume it is a rectangle.
For each contour that we identify as a square or rectangle, we draw a green or red bounding box around it using the cv2.drawContours()
function.
Conclusion
In this tutorial, we’ve explored how to use OpenCV and Python to detect squares and rectangles in images. By using contour detection, we’re able to identify the shapes present in an image, and then approximate them as polygons to determine the number of sides they have.
While our example code only detects squares and rectangles, you could modify it to detect other shapes as well. With a little bit of creativity and some experimentation, you can use OpenCV to accomplish a wide variety of computer vision tasks, from object detection to facial recognition and more.
It’s important to note that the algorithm we’ve developed in this tutorial isn’t perfect. In particular, it may struggle to detect squares and rectangles that aren’t oriented horizontally or vertically. If you encounter this issue in your own work, you may need to modify the code to account for rotated shapes.
With that said, we hope this tutorial has been helpful in getting you started with using OpenCV and Python to detect shapes in images. With a little bit of effort and creativity, there’s no limit to what you can accomplish with these powerful tools.