mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 23:33:05 +04:00
Merge pull request #26834 from chacha21:findContours_speedup
Find contours speedup #26834 It is an attempt, as suggested by #26775, to restore lost speed when migrating `findContours()` implementation from C to C++ The patch adds an "Arena" (a pool) of pre-allocated memory so that contours points (and TreeNodes) can be picked from the Arena. The code of `findContours()` is mostly unchanged, the arena usage being implicit through a utility class Arena::Item that provides C++ overloaded operators and construct/destruct logic. As mentioned in #26775, the contour points are allocated and released in order, and can be represented by ranges of indices in their arena. No range subset will be released and drill a hole, that's why the internal representation as a range of indices makes sense. The TreeNodes use another Arena class that does not comply to that range logic. Currently, there is a significant improvement of the run-time on the test mentioned in #26775, but it is still far from the `findContours_legacy()` performance. - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -22,12 +22,11 @@ void cv::contourTreeToResults(CTree& tree,
|
||||
return;
|
||||
}
|
||||
|
||||
// mapping for indexes (original -> resulting)
|
||||
map<int, int> index_mapping;
|
||||
index_mapping[-1] = -1;
|
||||
index_mapping[0] = -1;
|
||||
|
||||
CV_Assert(tree.size() < (size_t)numeric_limits<int>::max());
|
||||
// mapping for indexes (original -> resulting)
|
||||
// -1 - based indexing
|
||||
vector<int> index_mapping(tree.size() + 1, -1);
|
||||
|
||||
const int total = (int)tree.size() - 1;
|
||||
_contours.create(total, 1, 0, -1, true);
|
||||
{
|
||||
@@ -39,7 +38,7 @@ void cv::contourTreeToResults(CTree& tree,
|
||||
CV_Assert(elem.self() != -1);
|
||||
if (elem.self() == 0)
|
||||
continue;
|
||||
index_mapping[elem.self()] = i;
|
||||
index_mapping.at(elem.self() + 1) = i;
|
||||
CV_Assert(elem.body.size() < (size_t)numeric_limits<int>::max());
|
||||
const int sz = (int)elem.body.size();
|
||||
_contours.create(sz, 1, res_type, i, true);
|
||||
@@ -65,10 +64,10 @@ void cv::contourTreeToResults(CTree& tree,
|
||||
if (elem.self() == 0)
|
||||
continue;
|
||||
Vec4i& h_vec = h_mat.at<Vec4i>(i);
|
||||
h_vec = Vec4i(index_mapping.at(elem.next),
|
||||
index_mapping.at(elem.prev),
|
||||
index_mapping.at(elem.first_child),
|
||||
index_mapping.at(elem.parent));
|
||||
h_vec = Vec4i(index_mapping.at(elem.next + 1),
|
||||
index_mapping.at(elem.prev + 1),
|
||||
index_mapping.at(elem.first_child + 1),
|
||||
index_mapping.at(elem.parent + 1));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user