🐶
Machine Vision

Rectangle Detection in Images: A How-To Guide

By Jan on 03/09/2025

Learn how to identify and locate rectangles within an image using various techniques and visual cues.

Rectangle Detection in Images: A How-To Guide

Table of Contents

Introduction

This document outlines the steps to detect rectangles in an image using OpenCV, a popular Python library for computer vision. The process involves preprocessing the image, detecting edges, finding contours, filtering for rectangular shapes, and finally extracting the coordinates of the detected rectangles.

Step-by-Step Guide

  1. Preprocess the image:

    • Convert the image to grayscale: gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    • Apply Gaussian blur to reduce noise: blurred = cv2.GaussianBlur(gray, (5, 5), 0)
  2. Detect edges:

    • Use an edge detection algorithm like Canny: edges = cv2.Canny(blurred, 50, 150)
  3. Find contours:

    • Find contours in the edge-detected image: contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  4. Filter contours for rectangles:

    • Loop through each contour and approximate its shape: approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
    • Check if the approximated contour has 4 vertices and is convex: if len(approx) == 4 and cv2.isContourConvex(approx):
      • Calculate the aspect ratio of the contour to differentiate between rectangles and squares.
  5. Extract rectangle coordinates:

    • If a contour meets the criteria, extract its corner coordinates from the approx array.
  6. (Optional) Draw rectangles on the original image:

    • Use cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) to draw rectangles on the original image, where (x1, y1) and (x2, y2) are the top-left and bottom-right corner coordinates of the detected rectangle.

Code Example

This Python code uses OpenCV to detect rectangles in an image. It preprocesses the image, detects edges, finds contours, filters them for potential rectangles based on aspect ratio, extracts rectangle coordinates, and optionally draws the rectangles on the original image.

import cv2

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

# 1. Preprocess the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# 2. Detect edges
edges = cv2.Canny(blurred, 50, 150)

# 3. Find contours
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# 4. Filter contours for rectangles
rectangles = []
for contour in contours:
    approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
    if len(approx) == 4 and cv2.isContourConvex(approx):
        # Calculate aspect ratio
        x, y, w, h = cv2.boundingRect(approx)
        aspect_ratio = float(w) / h
        # Define aspect ratio threshold for rectangles (adjust as needed)
        if 1.2 < aspect_ratio < 2.5:  
            rectangles.append(approx)

# 5. Extract rectangle coordinates
for rectangle in rectangles:
    x, y, w, h = cv2.boundingRect(rectangle)
    print(f"Rectangle coordinates: Top-left:({x}, {y}), Bottom-right:({x+w}, {y+h})")

# 6. (Optional) Draw rectangles on the original image
for rectangle in rectangles:
    x, y, w, h = cv2.boundingRect(rectangle)
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display the result
cv2.imshow('Detected Rectangles', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation:

  1. Image Preprocessing:

    • Converts the image to grayscale for easier edge detection.
    • Applies Gaussian blur to smooth out noise and improve edge detection accuracy.
  2. Edge Detection:

    • Uses the Canny edge detection algorithm to identify sharp edges in the image.
  3. Contour Finding:

    • Finds contours (continuous curves) in the edge-detected image.
    • cv2.RETR_EXTERNAL retrieves only the outermost contours.
    • cv2.CHAIN_APPROX_SIMPLE simplifies the contour representation, saving memory.
  4. Rectangle Filtering:

    • Iterates through each contour:
      • Approximates the contour shape using cv2.approxPolyDP.
      • Checks if the approximation has 4 vertices (potential rectangle) and is convex.
      • Calculates the aspect ratio (width/height) to differentiate between rectangles and squares. Adjust the threshold (1.2 and 2.5 in this example) based on your image and desired rectangle proportions.
  5. Coordinate Extraction:

    • For contours that pass the rectangle criteria, extracts the top-left (x, y) and bottom-right (x+w, y+h) coordinates using cv2.boundingRect.
  6. Drawing Rectangles (Optional):

    • Draws green rectangles on the original image using the extracted coordinates.

To use the code:

  1. Replace 'image.jpg' with the path to your image file.
  2. Run the script. It will display the image with detected rectangles highlighted. The console will also print the coordinates of each detected rectangle.

Additional Notes

General Considerations:

  • Parameter Tuning: The success of this approach heavily relies on choosing appropriate parameters for functions like GaussianBlur and Canny. These parameters might need adjustments depending on the specific image characteristics like resolution and noise levels.
  • Performance: For real-time applications or large images, optimizing the contour finding and filtering steps is crucial. Using techniques like contour approximation with a higher epsilon value in cv2.approxPolyDP can help reduce processing time.
  • Complex Backgrounds: This method works best with simple backgrounds. Complex backgrounds might lead to false positives. Consider using image segmentation techniques to isolate the region of interest before applying rectangle detection.

Possible Improvements:

  • Shape Accuracy: Instead of relying solely on the number of vertices and convexity, calculate the Hu Moments of the contour and compare them to the Hu Moments of a reference rectangle to improve shape matching accuracy.
  • Rectangle Orientation: This code detects rectangles regardless of their orientation. If you need to detect rectangles with specific orientations, you can use the cv2.minAreaRect function to get the angle of rotation and filter rectangles based on that.
  • Non-Perfect Rectangles: The code can be adapted to detect rectangles with slightly rounded corners or minor imperfections by adjusting the aspect ratio threshold and the epsilon value in cv2.approxPolyDP.

Applications:

  • Object Detection: This technique can be used as a building block for detecting objects with rectangular features like license plates, credit cards, or buildings in images.
  • Document Analysis: Detecting rectangular shapes is useful for extracting tables, forms, or text boxes from scanned documents.
  • Robotics: Robots can use rectangle detection to identify and interact with objects like boxes, doors, or furniture.

Summary

This article outlines a step-by-step process for detecting rectangles within an image using OpenCV in Python.

1. Image Preprocessing:

  • The image is converted to grayscale to simplify analysis.
  • Gaussian blur is applied to reduce noise and improve edge detection accuracy.

2. Edge Detection:

  • The Canny edge detection algorithm identifies sharp changes in intensity, highlighting the edges of potential rectangles.

3. Contour Detection:

  • Contours, which are outlines of connected points forming a curve, are extracted from the edge-detected image. This captures the shapes present.

4. Rectangle Filtering:

  • Each contour is approximated to a polygon.
  • Contours with four vertices and a convex shape are identified as potential rectangles.
  • Aspect ratio calculation can further differentiate between rectangles and squares.

5. Coordinate Extraction:

  • The corner coordinates of confirmed rectangles are extracted for further use.

6. Visualization (Optional):

  • Rectangles can be visually highlighted on the original image using their extracted coordinates.

This process effectively identifies and locates rectangular shapes within images, providing a foundation for various applications like object detection and image analysis.

Conclusion

This article provides a comprehensive guide to detecting rectangles in images using OpenCV and Python. By understanding and implementing these steps, developers can effectively identify and extract rectangular shapes for various applications. The process involves preprocessing the image, detecting edges, finding contours, filtering for rectangular shapes based on specific criteria, and finally extracting the coordinates of the detected rectangles. This technique proves valuable in diverse fields, including object detection, document analysis, and robotics, showcasing its versatility and significance in computer vision tasks. However, it's important to note that the effectiveness of this method depends on factors like image quality, background complexity, and parameter tuning. Further exploration and adaptation of these techniques can lead to more robust and accurate rectangle detection in real-world scenarios.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait