Quadrilateral Shape Finding Algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Detecting quadrilaterals is a common computer-vision task because many real objects project to four-sided shapes: documents, screens, road signs, cards, and boxes. A practical quadrilateral finder usually combines image preprocessing, contour extraction, polygon approximation, and geometric validation rather than trying to classify pixels directly.
A Reliable Detection Pipeline
A good algorithm does not begin by asking, "is this a rectangle?" It first isolates strong edges and closed contours, then narrows the candidates.
A typical pipeline looks like this:
- convert the image to grayscale
- reduce noise with blur
- detect edges
- find contours
- approximate each contour as a polygon
- keep polygons with four vertices and sensible geometry
That separation is useful because each stage removes a different class of false positives.
A Runnable OpenCV Example
The example below finds convex four-sided contours and filters out tiny shapes.
This is a practical starting point for document scanners or shape-highlighting tools. It is not magic, but it covers the main steps that matter.
Why Polygon Approximation Matters
Contours often contain dozens or hundreds of points. cv2.approxPolyDP simplifies that contour to a smaller polygon while preserving the outline within a tolerance.
The tolerance value controls how aggressively the shape is simplified. A value around 0.01 to 0.03 times the contour perimeter is a common starting range. Too small and you keep noisy points. Too large and you collapse real corners.
For quadrilateral detection, this approximation stage is what turns a ragged contour into a testable "does it have four corners" candidate.
Add Geometric Checks Beyond Four Vertices
Not every four-vertex polygon is useful. Depending on the application, you may want extra tests:
- minimum area, to reject tiny noise blobs
- convexity, to reject self-folded or concave shapes
- angle similarity, if you expect rectangles
- aspect-ratio limits, if the target object has known proportions
A document scanner, for example, often expects a large convex quadrilateral near the center of the frame. A generic shape detector may want to keep more candidates and let later stages rank them.
If you need rectangle-like shapes specifically, you can estimate the angle between adjacent edges and discard candidates whose corners are far from right angles.
Perspective and Ordering of Corners
Real images rarely show perfect front-facing rectangles. Perspective distortion can turn a rectangle into a general convex quadrilateral, which is why a four-corner detector is often better than a strict rectangle detector.
Once a quadrilateral is found, the next useful step is ordering the corners consistently, usually top-left, top-right, bottom-right, bottom-left. That allows you to compute a perspective transform and warp the region into a flat view for OCR or downstream analysis.
So in many applications, "find a quadrilateral" is really the front half of a document-normalization pipeline.
Common Pitfalls
Using thresholds that are too aggressive can erase valid edges before contour detection even begins. Tune blur, Canny thresholds, and minimum area together rather than independently.
Assuming the largest contour is always the correct one also causes false detections in cluttered scenes.
Ignoring convexity can admit bizarre four-point approximations that are not useful target shapes.
Finally, do not expect one fixed tolerance value to work for every image scale. Contour approximation parameters often need to be chosen relative to perimeter or image size.
Summary
- quadrilateral detection is usually built from preprocessing, contour extraction, polygon approximation, and geometric filtering
- '
cv2.approxPolyDPis the key step that converts dense contours into corner candidates' - four vertices alone are not enough; area and convexity checks remove many false positives
- perspective distortion means many real rectangles appear as general quadrilaterals in the image
- the detected corners are often used next for perspective correction or document warping
Related reading
- QuadTree find neighbor
- Query for documents where array size is greater than 1
- Query regarding dijkstra algorithm
- Question about Backpropagation Algorithm with Artificial Neural Networks -- Order of updating
- Ramer-Douglas-Peucker path simplification algorithm
- Random-first search?
- Question from Interview, Retrieve alphabetic order from dictionary
- Queue data structure supporting fast k-th largest element finding

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.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.