🐶
Machine Vision

Square Detection in Images: Algorithms and Techniques

By Jan on 03/04/2025

Learn how to accurately detect squares in images using computer vision techniques and algorithms for various applications.

Square Detection in Images: Algorithms and Techniques

Table of Contents

Introduction

This code snippet demonstrates a simple computer vision technique using OpenCV in Python to detect squares in an image. The process involves converting the image to grayscale, applying thresholding to obtain a binary representation, finding contours in the image, and then identifying squares based on their properties.

Step-by-Step Guide

import cv2

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

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

# Apply thresholding to create a binary image
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

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

# Loop over the contours
for contour in contours:
    # Approximate the contour to a polygon
    approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)

    # Check if the polygon has 4 sides and is convex
    if len(approx) == 4 and cv2.isContourConvex(approx):
        # Calculate the bounding box of the square
        x, y, w, h = cv2.boundingRect(approx)

        # Draw the bounding box on the original image
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display the image with the detected squares
cv2.imshow('Squares', image)
cv2.waitKey(0)

Code Example

This Python code uses OpenCV to detect squares in an image. It loads an image, converts it to grayscale, and applies thresholding to create a binary image. Then, it finds contours in the binary image and iterates over them. For each contour, it approximates the contour to a polygon and checks if it's a square by verifying if it has 4 sides and is convex. If a square is detected, the code calculates its bounding box and draws it on the original image. Finally, it displays the image with the detected squares.

import cv2

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

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

# Apply thresholding to create a binary image
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

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

# Loop over the contours
for contour in contours:
    # Approximate the contour to a polygon
    approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)

    # Check if the polygon has 4 sides and is convex
    if len(approx) == 4 and cv2.isContourConvex(approx):
        # Calculate the bounding box of the square
        x, y, w, h = cv2.boundingRect(approx)

        # Draw the bounding box on the original image
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display the image with the detected squares
cv2.imshow('Squares', image)
cv2.waitKey(0)

Additional Notes

Code Functionality:

  • Purpose: This code identifies and highlights squares within an image.
  • Image Preprocessing: The code converts the input image to grayscale and then applies thresholding. This simplifies the image, making it easier to detect shapes.
  • Contour Detection: Contours are outlines of objects in an image. The code finds these contours in the preprocessed image.
  • Square Identification: The code uses two key properties to identify squares:
    • Number of Sides: Squares have four sides.
    • Convexity: Squares are convex shapes (all interior angles are less than 180 degrees).
  • Bounding Boxes: Once a square is identified, a bounding box is drawn around it on the original image.

Potential Improvements and Considerations:

  • Thresholding: The threshold value (127 in this case) might need adjustment depending on the input image's contrast. Experiment with different values for optimal results.
  • Contour Approximation: The cv2.approxPolyDP function approximates the contour with a polygon. The epsilon value (0.01 * arc length) controls the approximation accuracy. Adjust this value for different levels of detail.
  • Square Size Filtering: The code currently detects squares of any size. You can add size constraints to filter out very small or very large squares.
  • Real-World Applications: This code can be a starting point for applications like:
    • Object detection (e.g., detecting square-shaped objects on a conveyor belt).
    • Image analysis (e.g., counting squares in a grid).
    • Augmented reality (e.g., overlaying virtual objects on top of detected squares).

Additional Resources:

  • OpenCV Documentation: https://docs.opencv.org/
  • Contour Features: Learn more about contour features (like area, perimeter, convexity) in the OpenCV documentation. These can be used for more advanced shape analysis.
  • Hough Transform: For detecting perfect squares even in noisy images, consider exploring the Hough Transform for lines.

Summary

This Python code uses the OpenCV library (cv2) to detect squares within an image. Here's a breakdown of the process:

  1. Image Loading and Preprocessing:

    • Loads an image named "image.jpg".
    • Converts the image to grayscale for easier processing.
    • Applies binary thresholding to create a black and white image, highlighting edges.
  2. Contour Detection and Filtering:

    • Finds contours (outlines of connected regions) in the binary image.
    • Focuses on external contours, ignoring any nested shapes.
    • Simplifies the contours for computational efficiency.
  3. Square Identification and Visualization:

    • Iterates through each detected contour.
    • Approximates the contour shape as a polygon.
    • Checks if the polygon has 4 sides and is convex (no inward angles), indicating a potential square.
    • If a square is identified:
      • Calculates its bounding box (location and size).
      • Draws a green rectangle around the square on the original image.
  4. Result Display:

    • Displays the original image with the detected squares highlighted.
    • Waits for a key press before closing the display window.

In essence, this code leverages image processing techniques to identify and visually mark squares present within a given image.

Conclusion

This code provides a foundational understanding of how to detect squares in images using OpenCV. By understanding the principles of image processing, contour detection, and shape analysis, you can adapt and extend this code for various applications, including object recognition, image analysis, and computer vision tasks. Experiment with different images and explore additional OpenCV functions to enhance the accuracy and robustness of your square detection algorithm.

References

Were You Able to Follow the Instructions?

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