In an image processing project (using opencv with python), I am trying to detect as precisely as possible the location of a rectangular object in a photograph. My final goal is to output the 4 corners of the object.

For example, an image could look like this: In the first stage I am able to detect an approximate boundary for the image, like so: Now, I have the above red convex polygon.

Next, I would like to compute the Minimum Area Bounding Quadrilateral, and this is where I'm stuck.

My question is, given a convex polygon, what is an efficient algorithm to find a minimum area bounding quadrilateral?

Thanks!

• "A rectangular object" vs "minimum area bounding quadrilateral" are not consistent. Can you explain in the question why you do not care it being rectangular any more? Jul 21, 2019 at 9:51
• @Apass.Jack: isn't it obvious that the object is rectangular and perspective deforms it ? Jul 21, 2019 at 10:07
• @YvesDaoust Yes, it is obvious. I was just being careful to avoid XY problem. I also hoped that the questioner could explain the "minimum area" as well, although it is also obvious that the "minimum area" could be a reasonable requirement. Is there a different approach to approximate a quadrilateral? Jul 21, 2019 at 10:20
• @Apass.Jack: if the shape is truly a convex quadrilateral, both the minimum area and minimum perimeter criteria will exactly return this quadrilateral; which will also be the convex hull, made of four segments. In real life, you get "some" approximation. Jul 21, 2019 at 15:52
• @YvesDaoust Yes, I agreed. I was encouraging the OP to explain his approach as well as to looks for different ideas. By the way, your answer is pretty good. I upvoted. Jul 21, 2019 at 16:03

The fitting of a minimum area quadrilateral to a set of points is an uneasy problem, and I believe that simpler approaches are good enough.

Notice that the corners are rounded and cannot be used as such. So you'd better fit straight edges and intersect them to obtain the quadrilateral.

One approach is by the Hough transform. To make it maximally efficient, I would recommend to process four tight areas or interest around the edges of the axis-aligned bounding box. This will make processing much faster, avoid false positives and avoid any ambiguity on the correspondences.

You can also sample points on the four sides and fit a straight line by total-least squares, or by some robust line fitting method.

If you want to make sure that the fitted lines don't cross the shape at all, you can handle the four sides independently and consider a partition of the plane in the four quadrants delimited by the diagonals (even if they are approximate). In every quadrant, you will find the minimum area triangle formed by the line against an edge of the convex hull. Just try every edge of the convex hull in turn.

[Technically, it is possible that the minimum is achieved with a line through some vertex of the convex hull, with an intermediate angle, i.e. not in the direction of a hull side. We should also check if the area function is always unimodal in terms of the line angle. IMO these refined considerations can come later.] • Thanks! How do I approximate the diagonals? Jul 22, 2019 at 11:07
• @Mathguy: find points farthest form the center. Jul 22, 2019 at 11:47