From fa55ed2837bbf44e0d4463b64b200a9079d2e6d0 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Thu, 11 Jun 2026 19:54:39 +0200 Subject: [PATCH] Merge pull request #29267 from vrabaud:aruco Fix speed regression in Aruco identify #29267 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [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 --- .../objdetect/src/aruco/aruco_dictionary.cpp | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/modules/objdetect/src/aruco/aruco_dictionary.cpp b/modules/objdetect/src/aruco/aruco_dictionary.cpp index b8e478fc85..8de140b7e5 100644 --- a/modules/objdetect/src/aruco/aruco_dictionary.cpp +++ b/modules/objdetect/src/aruco/aruco_dictionary.cpp @@ -76,6 +76,35 @@ void Dictionary::writeDictionary(FileStorage& fs, const String &name) bool Dictionary::identify(const Mat &onlyCellPixelRatio, CV_OUT int &idx, CV_OUT int &rotation, double maxCorrectionRate, float validBitIdThreshold) const { CV_Assert(onlyCellPixelRatio.rows == markerSize && onlyCellPixelRatio.cols == markerSize); + // Fill bit masks of cells that are not black (not0) and not white (not1). + const int s = (markerSize * markerSize + 8 - 1) / 8; + AutoBuffer temp(4 * s); + uint8_t* not0 = temp.data(), * not1 = not0 + s; + int not0Byte = 0, not1Byte = 0, currentByte = 0, currentBit = 0; + for(int j = 0; j < markerSize; j++) { + const float* cellPixelRatioRow = onlyCellPixelRatio.ptr(j); + for(int i = 0; i < markerSize; i++) { + not0Byte <<= 1; not1Byte <<= 1; + if(cellPixelRatioRow[i] > validBitIdThreshold) not0Byte |= 1; + if(cellPixelRatioRow[i] < 1 - validBitIdThreshold) not1Byte |= 1; + ++currentBit; + if(currentBit == 8) { + not0[currentByte] = not0Byte; + not1[currentByte] = not1Byte; + not0Byte = not1Byte = 0; + ++currentByte; + currentBit = 0; + } + } + } + if (currentBit != 0) { + not0[currentByte] = not0Byte; + not1[currentByte] = not1Byte; + } + uint8_t* notXor = not1 + s, * temp0 = notXor + s; + // Computing: notXor = not0 ^ not1 + hal::xor8u(not0, s, not1, s, notXor, s, s, 1, nullptr); + int maxCorrectionRecalculed = int(double(maxCorrectionBits) * maxCorrectionRate); idx = -1; // by default, not found @@ -84,25 +113,21 @@ bool Dictionary::identify(const Mat &onlyCellPixelRatio, CV_OUT int &idx, CV_OUT for(int m = 0; m < bytesList.rows; m++) { int currentMinDistance = markerSize * markerSize + 1; int currentRotation = -1; - for(int r = 0; r < 4; r++) { - - Mat bitsRot = getBitsFromByteList(bytesList.rowRange(m, m + 1), markerSize, r); - bitsRot.convertTo(bitsRot, CV_32F); - - // Loop over all bits dictBitsList [m, markerSize * markerSize, 4]; onlyCellPixelRatio [markerSize, markerSize] - int currentHamming = 0; - for(int i = 0; i < markerSize; i++) { - for(int j = 0; j < markerSize; j++) { - // If detected bit is too far from the ground truth, consider it false. - if(fabs(onlyCellPixelRatio.at(i, j) - static_cast(bitsRot.at(i, j))) > validBitIdThreshold){ - currentHamming++; - } - } - } + const uchar* bytesRot = bytesList.ptr(m); + for(int r = 0; r < 4; r++, bytesRot += s) { + // Error if: (marker is 0 and input is not 0) or (marker is 1 and input is not 1) + // i.e. if: (!bytesRot && not0) || (bytesRot && not1) + // This is actually: not0 ^ ((not0 ^ not1) & bytesRot) + // Computing: temp0 = (not0 ^ not1) & bytesRot + hal::and8u(notXor, s, bytesRot, s, temp0, s, s, 1, nullptr); + // Computing the final result (xor is performed internally). + int currentHamming = cv::hal::normHamming(not0, temp0, s); if(currentHamming < currentMinDistance) { currentMinDistance = currentHamming; currentRotation = r; + // Break for perfect distance. + if (currentMinDistance == 0) break; } }