Algorithm to find a square shape in an image?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
In computer vision, detecting geometric shapes like squares in an image is a fundamental task for various applications such as object recognition, robotics, and automated navigation systems. This article provides a comprehensive overview of algorithms and techniques used to identify square shapes within an image. We will delve into the stages of pre-processing, edge detection, shape detection, and post-processing, while also covering essential considerations and optimization strategies.
Pre-Processing
Before attempting to detect shapes, particularly squares, an image must be pre-processed. Preprocessing typically involves the following steps:
- Grayscale Conversion: Most shape detection algorithms operate on single-channel images. Convert the image to grayscale to reduce complexity.
- Noise Reduction: Apply Gaussian blur to remove noise, which can lead to false detections.
- Thresholding: Use techniques like Otsu’s method to convert the grayscale image to a binary image, making the edges more distinguishable.
Edge Detection
Edge detection is a critical step in finding potential square contours. The most common methods include:
- Canny Edge Detector: This popular edge detection algorithm finds a wide range of edges in images. It involves:
- Applying Gaussian filtering to smooth the image.
- Computing intensity gradients.
- Applying non-maximum suppression to remove spurious responses.
- Using double thresholding and edge tracking by hysteresis to identify strong and weak edges.
The Canny algorithm can be implemented using the following OpenCV function: `cv2.Canny(image, threshold1, threshold2)`.
Shape Detection
Once edges are detected, the next step is to find contours and identify square shapes.
- Finding Contours: Utilize algorithms to extract contours from the binary image. OpenCV provides `findContours()` for this purpose.
- The number of vertices (should be 4 for squares).
- The angle between these vertices (approximately 90 degrees).
- Equal length of all sides.
- Verification: Use geometric properties to further verify squares, such as aspect ratio checks (close to 1:1) and area constraints.
- Hierarchical Sorting: To prioritize detection, sort shapes based on size or proximity.
- Filter Outliers: Remove contours that do not meet the typical size or shape characteristics of squares.
Related reading
- algorithm to trace border in 2D array
- Algorithmic question Best angle to view trees from fixed camera
- Algorithms for finding a look alike face?
- Apple Vision framework – Text extraction from image
- Algorithm to find added/removed elements in an array
- Algorithm to find ALL factorizations of an integer
- Are modern CNN convolutional neural network as DetectNet rotate invariant?
- Are there any plans for ROI Pooling layer in tensorflow for object detection?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.