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.