OpenCV Is it possible to detect rectangle from corners?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, OpenCV can help you detect rectangles from corner information, but corners alone are rarely enough for a robust detector. In practice, the most reliable pipeline uses edges, contours, polygon approximation, and geometric validation, with corner detection acting as a supporting signal rather than the only test.
Start With Preprocessing and Contours
A common rectangle detector begins by simplifying the image, extracting edges, and finding contours.
This works better than relying on raw corners because contours preserve larger shape information rather than isolated point evidence.
Approximate Contours to Quadrilaterals
Once you have contours, reduce them to polygons and keep convex shapes with four vertices.
At this stage you have quadrilateral candidates, not guaranteed rectangles. A trapezoid or an arbitrary convex four-sided shape can still pass this filter.
Validate Rectangle Geometry
To decide whether a quadrilateral is rectangle-like, inspect its angles. A rectangle should have four near-right angles.
Smaller cosine values mean the angles are closer to ninety degrees. This removes many non-rectangular quadrilaterals.
Where Corner Detection Still Helps
Corner detectors such as Shi-Tomasi can still be useful, but usually as a support tool rather than the main detector.
This is useful for debugging, for visualizing likely candidate points, or for later perspective correction steps.
Rotated and Skewed Rectangles
If the rectangle is rotated or seen in perspective, minAreaRect can still provide a rotated bounding box estimate.
This is especially helpful in document scanning, tabletop scenes, or camera feeds where the target is not axis-aligned.
The Image Quality Problem
Detection quality often depends more on the input image than on the final geometric tests. Better contrast, less background clutter, and cleaner edges can improve rectangle detection dramatically before you change a single threshold.
That is why preprocessing is not just a preliminary step. It is often half of the detector.
Common Pitfalls
A common mistake is treating any four-corner polygon as a rectangle without angle validation. Four corners only prove that the contour is a quadrilateral.
Another issue is using fixed thresholds for every image regardless of lighting, blur, and scale. Real scenes usually need some tuning.
Developers also often rely on corner detectors alone when contour-based shape information would be much stronger.
Summary
- Rectangle detection from corners is possible, but corners alone are rarely robust enough.
- A practical OpenCV pipeline uses preprocessing, contours, polygon approximation, and angle checks.
- Four vertices give you a quadrilateral candidate, not proof of a rectangle.
- Corner detectors are useful supporting tools for visualization and refinement.
- Rotated cases often benefit from
minAreaRector other geometry-aware post-processing.

