mirror of
https://github.com/zebrajr/opencv.git
synced 2025-12-06 12:19:50 +01:00
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
75 lines
2.3 KiB
C++
75 lines
2.3 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html
|
|
|
|
#include "precomp.hpp"
|
|
#include "contours_common.hpp"
|
|
#include <map>
|
|
#include <limits>
|
|
|
|
using namespace std;
|
|
using namespace cv;
|
|
|
|
void cv::contourTreeToResults(CTree& tree,
|
|
int res_type,
|
|
OutputArrayOfArrays& _contours,
|
|
OutputArray& _hierarchy)
|
|
{
|
|
// check if there are no results
|
|
if (tree.isEmpty() || (tree.elem(0).body.isEmpty() && (tree.elem(0).first_child == -1)))
|
|
{
|
|
_contours.clear();
|
|
return;
|
|
}
|
|
|
|
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);
|
|
{
|
|
int i = 0;
|
|
CIterator it(tree);
|
|
while (!it.isDone())
|
|
{
|
|
const CNode& elem = it.getNext_s();
|
|
CV_Assert(elem.self() != -1);
|
|
if (elem.self() == 0)
|
|
continue;
|
|
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);
|
|
if (sz > 0)
|
|
{
|
|
Mat cmat = _contours.getMat(i);
|
|
CV_Assert(cmat.isContinuous());
|
|
elem.body.copyTo(cmat.data);
|
|
}
|
|
++i;
|
|
}
|
|
}
|
|
|
|
if (_hierarchy.needed())
|
|
{
|
|
_hierarchy.create(1, total, CV_32SC4, -1, true);
|
|
Mat h_mat = _hierarchy.getMat();
|
|
int i = 0;
|
|
CIterator it(tree);
|
|
while (!it.isDone())
|
|
{
|
|
const CNode& elem = it.getNext_s();
|
|
if (elem.self() == 0)
|
|
continue;
|
|
Vec4i& h_vec = h_mat.at<Vec4i>(i);
|
|
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;
|
|
}
|
|
}
|
|
}
|