-
-
Notifications
You must be signed in to change notification settings - Fork 55.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new function that approximates the polygon bounding a convex hull with a certain number of sides #25607
base: 4.x
Are you sure you want to change the base?
Add a new function that approximates the polygon bounding a convex hull with a certain number of sides #25607
Conversation
|
|
modules/imgproc/src/approx.cpp
Outdated
if relevant = 0, the intersection in the heap must be recalculated | ||
if relevant = 1, intersection has been calculated | ||
*/ | ||
short relevant; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use int8_t
instead of short
or use enum class
Also suggest to rename relevant
to pointStatus
@@ -4060,6 +4060,26 @@ CV_EXPORTS_W void approxPolyDP( InputArray curve, | |||
OutputArray approxCurve, | |||
double epsilon, bool closed ); | |||
|
|||
/** @brief Approximates a polygon with a convex hull with a specified accuracy and number of sides. | |||
The cv::approxBoundingPoly function approximates a polygon with a convex hull |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As soon as the implementation is base on public paper I propose to add the paper to doc/opencv.bib
and add reference to the function description.
modules/imgproc/src/approx.cpp
Outdated
int vertex; | ||
cv::Point2f intersection; | ||
|
||
changes(float area_, int vertex_, cv::Point2f intersection_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicit?
const cv::Point2f&
modules/imgproc/src/approx.cpp
Outdated
int next; | ||
int prev; | ||
|
||
neighbours(int next_ = -1, int prev_ = -1, cv::Point2f point_ = { -1, -1 }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicit?
CV_Assert(side > 2); | ||
CV_Assert(_approxCurve.type() == CV_32FC2 | ||
|| _approxCurve.type() == CV_32SC2 | ||
|| _approxCurve.type() == 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why .type() == 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type of the function matches the type OutputArray _approxCurve
.
However, in Python, you don't need to specify OutputArray, and its type will be zero _approxCurve.type() == 0
:
output = cv.approxBoundingPoly(contour, side=4, make_hull=True)
In this case the function currently uses the InputArray _curve
type for output:
if (_approxCurve.type()!= 0) depth = _approxCurve.depth();
modules/imgproc/src/approx.cpp
Outdated
curve = curve.t(); | ||
if (curve.rows == 1) | ||
{ | ||
curve = curve.t(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.t() call may happen twice. It does not make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Y can just call cv::Mat::reshape() to get propoer shape for existing buffer without memory transfers.
modules/imgproc/src/approx.cpp
Outdated
|
||
if (recalculation(hull, vertex_id, area, new_x, new_y) == -1) | ||
{ | ||
area = 1e+38f; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What 1d+38 means? I propose to use something like FLT_MAX instead.
modules/imgproc/src/approx.cpp
Outdated
if (recalculation(hull, vertex_id, area, new_x, new_y) == -1) | ||
{ | ||
area = 1e+38f; | ||
new_x = -1; new_y = -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I propose to move the initialization for the negative case inside recalculation
function.
modules/imgproc/src/approx.cpp
Outdated
area = 1e+38f; | ||
new_x = -1; new_y = -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same.
The algorithm based on the paper | ||
<https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&doi=1fbd43f3827fffeb76641a9c5ab5b625eb5a75ba>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use Tex notation for references. Links are generated by doxygen automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes have been added. @asmorkalov
merge PR with opencv/opencv_extra#1179
This PR is based on the paper View Frustum Optimization To Maximize Object’s Image Area.
Problem
I needed to reduce the number of vertices of the convex hull so that the additional area was minimal, andall vertices of the original contour enter the new contour.
Description
Initially in the contour of n vertices, at each stage we consider the intersection points of the lines formed by each adjacent edges. Each of these intersection points will form a triangle with vertices through which lines pass. Let's choose a triangle with the minimum area and merge the two vertices at the intersection point. We continue until there are more vertices than the specified number of sides of the approximated polygon.
Complexity:
Using a std::priority_queue or std::set time complexity is (O(n*ln(n)), memory O(n),
n - number of vertices in convex hull.
count of sides - the number of points by which we must reduce.
Comment
If epsilon_percentage more 0, algorithm can return more values than side.
Algorithm returns OutputArray. If OutputArray.type() equals 0, algorithm returns values with InputArray.type().
New test uses image which are not in opencv_extra, needs to be added.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.