1

I have an OpenCV contour c which is close to a quadrilateral in its shape, but it can have 4 to multiple of dozens of points.

I want to find the quadrilateral q which is the closest to the contour c (one way could be to minimize the area of the difference between them).

I currently use

q = cv2.approxPolyDP(c, 0.02 * cv2.arcLength(c, True), closed=True)

but the result is not always a quadrilateral. I have already tried techniques like those in cv2.approxPolyDP() , cv2.arcLength() How these works and in How to force approxPolyDP() to return only the best 4 corners? - Opencv 2.4.2 (binary search for example), but it doesn't always yield a quadrilateral.

Is there another way (probably with something else than approxPolyDP?) to find the best quadrilateral approximation to a contour?

Note: another linked question is Crop image based on largest quadrilateral contour OpenCV but it does not give a solution to this problem.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Basj
  • 41,386
  • 99
  • 383
  • 673
  • I am afraid that this problem is arduous. https://www.researchgate.net/publication/226436325_Minimum_area_circumscribing_Polygons If your shape is close to a rectangle, the tightest rectangle is much easier to find. –  Feb 17 '22 at 20:05
  • Thanks for the link @YvesDaoust. This seems to be for the *circumscribing* polygon. I don't know if it changes the level of difficulty, but I'm okay about any quadrilateral, not specifically circumscribing. Do you think it can be doable with a minimization of the area in the symmetrical difference of `c` and `q`? – Basj Feb 17 '22 at 20:10
  • Certainly more difficult. –  Feb 17 '22 at 20:12
  • look at all the algorithms used by various AR markers and their detection methods. -- no, opencv has no simply usable function that preserves edges (and infers true corners). approxPolyDP is old stuff, and it "preserves" corners, while completely butchering (moving) edges. -- maybe someone should do a GSoC and implement some of these methods, or refactor the relevant parts from the aruco code. it has a few refinement methods (apriltag etc) – Christoph Rackwitz Feb 18 '22 at 10:58
  • btw, it pays to shrug off that academic vagueness/abstraction that hobbles all the academics. you talk of "quadrilaterals". *show* what you're really talking about. – Christoph Rackwitz Feb 18 '22 at 10:59

0 Answers0