Could someone give a good explanation about how epsilon works?
This is how I use it.
cv::approxPolyDP(contour, approx, cv::arcLength(contour, true) * precision, true);
As default double precision=0.02
.
Somthing that doesn't make sense to me is that the lower precision
is the less strict the shape detection gets?
For example if I'm looking for rectangle contours in an image and not all rectangular contours are detected and precision
is set to 0.5
(higher) even fewer rectangular contours are detected as rectangles. But if I set precision
to 0.01
(lower) more rectangular contours are detected???
Shouldn't it be the other way around? Lower precision
= more strict shape detection?
CodePudding user response:
approxPolyDP
implements the Ramer–Douglas–Peucker algorithm
The algorithm does not detect shapes, it simplifies contours.
It removes points that contribute very little (epsilon) to the shape of the contour. Colinear points are a trivial case because they contribute zero to the shape of the contour. The most prominent corners are left standing. The result is an approximation of the input contour.
A tighter epsilon forces a more faithful approximation, leaving more points left standing, down to all points remaining (no action).
Note that in the case of a square/rectangle/quad with even just rounded corners, the locations of the edges of the quad are not maintained. Points on the rounded corners remain. However, the approximation can be used to segment the original contour, discard "corner" points, and work with edge points.