Skip to content

Commit 4de2b67

Browse files
committed
aruco: use std::rotate to avoid copying all corners
1 parent 6848eb6 commit 4de2b67

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

modules/aruco/src/aruco.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,11 @@ static int _getBorderErrors(const Mat &bits, int markerSize, int borderSize) {
480480
/**
481481
* @brief Tries to identify one candidate given the dictionary
482482
*/
483-
static bool _identifyOneCandidate(const Ptr<Dictionary> &dictionary, InputArray _image,
484-
InputOutputArray _corners, int &idx, const Ptr<DetectorParameters> &params) {
485-
486-
CV_Assert(_corners.total() == 4);
483+
static bool _identifyOneCandidate(const Ptr<Dictionary>& dictionary, InputArray _image,
484+
vector<Point2f>& _corners, int& idx,
485+
const Ptr<DetectorParameters>& params)
486+
{
487+
CV_Assert(_corners.size() == 4);
487488
CV_Assert(_image.getMat().total() != 0);
488489
CV_Assert(params->markerBorderBits > 0);
489490

@@ -510,16 +511,12 @@ static bool _identifyOneCandidate(const Ptr<Dictionary> &dictionary, InputArray
510511
int rotation;
511512
if(!dictionary->identify(onlyBits, idx, rotation, params->errorCorrectionRate))
512513
return false;
513-
else {
514-
// shift corner positions to the correct rotation
515-
if(rotation != 0) {
516-
Mat copyPoints = _corners.getMat().clone();
517-
for(int j = 0; j < 4; j++)
518-
_corners.getMat().ptr< Point2f >(0)[j] =
519-
copyPoints.ptr< Point2f >(0)[(j + 4 - rotation) % 4];
520-
}
521-
return true;
514+
515+
// shift corner positions to the correct rotation
516+
if(rotation != 0) {
517+
std::rotate(_corners.begin(), _corners.begin() + 4 - rotation, _corners.end());
522518
}
519+
return true;
523520
}
524521

525522

@@ -529,11 +526,11 @@ static bool _identifyOneCandidate(const Ptr<Dictionary> &dictionary, InputArray
529526
*/
530527
class IdentifyCandidatesParallel : public ParallelLoopBody {
531528
public:
532-
IdentifyCandidatesParallel(const Mat& _grey, InputArrayOfArrays _candidates,
533-
InputArrayOfArrays _contours, const Ptr<Dictionary> &_dictionary,
529+
IdentifyCandidatesParallel(const Mat& _grey, vector< vector< Point2f > >& _candidates,
530+
const Ptr<Dictionary> &_dictionary,
534531
vector< int >& _idsTmp, vector< char >& _validCandidates,
535532
const Ptr<DetectorParameters> &_params)
536-
: grey(_grey), candidates(_candidates), contours(_contours), dictionary(_dictionary),
533+
: grey(_grey), candidates(_candidates), dictionary(_dictionary),
537534
idsTmp(_idsTmp), validCandidates(_validCandidates), params(_params) {}
538535

539536
void operator()(const Range &range) const {
@@ -542,8 +539,7 @@ class IdentifyCandidatesParallel : public ParallelLoopBody {
542539

543540
for(int i = begin; i < end; i++) {
544541
int currId;
545-
Mat currentCandidate = candidates.getMat(i);
546-
if(_identifyOneCandidate(dictionary, grey, currentCandidate, currId, params)) {
542+
if(_identifyOneCandidate(dictionary, grey, candidates[i], currId, params)) {
547543
validCandidates[i] = 1;
548544
idsTmp[i] = currId;
549545
}
@@ -554,7 +550,7 @@ class IdentifyCandidatesParallel : public ParallelLoopBody {
554550
IdentifyCandidatesParallel &operator=(const IdentifyCandidatesParallel &); // to quiet MSVC
555551

556552
const Mat &grey;
557-
InputArrayOfArrays candidates, contours;
553+
vector< vector< Point2f > >& candidates;
558554
const Ptr<Dictionary> &dictionary;
559555
vector< int > &idsTmp;
560556
vector< char > &validCandidates;
@@ -634,7 +630,7 @@ static void _identifyCandidates(InputArray _image, vector< vector< Point2f > >&
634630

635631
// this is the parallel call for the previous commented loop (result is equivalent)
636632
parallel_for_(Range(0, ncandidates),
637-
IdentifyCandidatesParallel(grey, _candidates, _contours, _dictionary, idsTmp,
633+
IdentifyCandidatesParallel(grey, _candidates, _dictionary, idsTmp,
638634
validCandidates, params));
639635

640636
for(int i = 0; i < ncandidates; i++) {

0 commit comments

Comments
 (0)