-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
Fix Memory Issues in Convex Hull algorithm #26975
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
Conversation
5c6cc6d
to
af684d2
Compare
It is now a bit faster and finally seems to be correct. without fixes: with fixes: |
Found one more issue. My own tests pass but some imgproc accuracy tests, reproducers and regression tests still fail. going through it case by case now. |
imgproc performance tests pass |
I'm still investigating test differences but i think it's ready for a look. |
getPointVec is not complete yet. Calib3d_ChessboardDetector.accuracy (among others) throws:
|
@kallaballa Thanks a lot for the issue investigation and fix. Couple of thoughts:
The similar case for output array. Just use copyTo method of Mat/InputArray/OutputArray. It handles the shapes and std::vectors automatically. |
Warnings:
|
thx! |
good to know! but those this also cover cases where numerical values are converted to Points? Also i found copyTo to be quite slow in comparision. |
oh my. i think i misunderstood something. convexHull isn't supposed to be callable with numerical values instead of points which make getPointVec rubbish. |
Simplified the implementation. Less tests fail. |
If I am not wrong some regression tests e.g.: opencv/modules/imgproc/test/test_convhull.cpp Line 158 in 39bc5df
|
opencv/modules/imgproc/test/test_convhull.cpp Line 161 in 39bc5df
|
am i right about some tests being invalid? because then this means i have to fix them one by one. |
opencv/modules/imgproc/test/test_convhull.cpp Line 227 in 39bc5df
|
The tests assume that int[][2] should work as input - the docs say otherwise. What should i do? |
So i fixed Imgproc_ConvexityDefects.ordering_4539 (
i think those changes in behavior are legitimate but I'm gonna test some more. How should i proceed with broken tests? |
I understand that those changes effect a lot of users, but since it is about memory corruption i can't see a chance to guarantee behavior doesn't change. so i think it's best to enforce the documentation by error checking and fix up tests and depending algorithms. |
maybe convexityDefects suffers from the same problem. still have to check many tests. |
I also think by adding more error checks the change won't go unnoticed because user-code will break with a meaningful message. which i would like :) |
Afaics there is no legal way to find the element type (except for numerical opencv types) of a container which makes rather complete error handling impossible. |
Maybe deprecation (with a big warning) in favor of a new function would be an option. |
I still got spurious memory errors, so i changed |
is this correct? std::vector<Point2f> vec = {{2.0f,3.0f}};
std::vector<Point2f> vec2;
OutputArray arr = vec;
arr.copyTo(vec2)
CV_Assert(vec.size() == vec2.size()) |
I pushed the current version that uses OutputArray::copy. It spuriously fails but always at (with asan enabled): opencv/modules/imgproc/src/convhull.cpp Line 174 in bee147d
|
Note that I removed undocumented behavior (returning points automatically depending on the OutputArray type) |
|
I understand now.
I saw that in the documentation and tried that but i can only create an OutputArray with numerical types. How to create an OutputArray that contains a |
Should be InputArray... void copyVec(InputArray _input, OutputArray _output) {
// _output.create ?
_input.copyTo(_output);
}
std::vector<Point2f> vec = {{2.0f,3.0f}};
std::vector<Point2f> vec2;
copyVec(vec, vec2)
CV_Assert(vec.size() == vec2.size()) So this should be correct? |
|
With lessons learned I am going back to the proverbial drawing board... |
bee147d
to
8e65075
Compare
I duplicated the original branch to https://github.com/kallaballa/opencv/tree/convexhullpointer_old and forcibly reset this one to 8e65075 |
how about this: to stick to the documentation as close as possible and catch all that did stick to the documentation, while making it obvious to others that they need to change something. Preferred signature: easy to be implemented fast and correct.template<typename Thull>
void convexHull(InputArray array, std::vector<Thull>& hull, clockwise> Deprecated signature 1: does the same as the preferred signature, only deprecated because the last parameter should be removedtemplate<typename Thull>
void convexHull(InputArray array, std::vector<Thull>& hull, clockwise, bool ignored> Deprecated signature 2: the original function fixed to the point where it retains behavior as closely as possible while preventing memory errors. probably considerably slower.void convexHull( InputArray _points, OutputArray _hull, bool clockwise, bool returnPoints ) |
Pull Request Readiness Checklist
See: #26952
Patch to opencv_extra has the same branch name.