mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
853ee9b96114f279630508b9f46be8ded6e8865e
Optimization ArUco: get border errors in one pass for both normal and inverted markers #29329 This PR is a follow up to optimization in https://github.com/opencv/opencv/pull/29267. ## Context: When detecting inverted markers, we call `_getBorderErrors` twice: - `int borderErrors = _getBorderErrors(cellPixelRatio, ...);` - `Mat invCellPixelRatio = 1.f - cellPixelRatio;` - `int invBError = _getBorderErrors(invCellPixelRatio, ...);` meaning: 1. Scan border cells once. 2. Allocate a new Mat. 3. Invert the entire `cellPixelRatio` matrix, not just the border. 4. Scan border cells again on the inverted matrix. 5. If inverted marker is better, copy the full inverted matrix back ## Solution only call `_getBorderErrors` once and compute both - borderErrors: `cellPixelRatio > validBitIdThreshold` - invBorderErrors: `1 - cellPixelRatio > validBitIdThreshold` This saves: 1. full invert into temporary Mat: (`Mat invCellPixelRatio = 1.f - cellPixelRatio;`) 2. scan temporary border: the second `_getBorderErrors` 3. copy temporary Mat back into `cellPixelRatio`: `invCellPixelRatio.copyTo(cellPixelRatio);` Note that the solution does not implement: ``` if(params.detectInvertedMarker) { _getBorderErrorsBoth(...); } else { borderErrors = _getBorderErrors(...); } ``` because the extra cost to computing invBorderErrors: `if(1.f - ratio > validBitIdThreshold) invBorderErrors++;` is negligible. this also avoids having to maintain two functions: `_getBorderErrorsBoth` and `_getBorderErrors` ## Tests: No visible results when testing the full marker detection pipeline because this step is not expensive. There is a roughly 8% noise when re-running the marker detections making it hard to spot small speed diffs. When testing only this specific step with a float matrix of size: `(markerSize + 2*border)^2` on a AMD Ryzen 7 (8 cores / 16 threads): - The new code is 30-45× faster at this stage depending on the marker size and whether the candidate was actually an inverted one, saving ~530-580 ns per candidate, mainly because we removed `Mat invCellPixelRatio = 1.f - cellPixelRatio` - The new code is equivalent when detecting non-inverted markers +/- 3 ns --- ### 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 - [ ] The PR is proposed to the proper branch - [ ] 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
OpenCV: Open Source Computer Vision Library
Resources
- Homepage: https://opencv.org
- Courses: https://opencv.org/courses
- Docs: https://docs.opencv.org/4.x/
- Q&A forum: https://forum.opencv.org
- previous forum (read only): http://answers.opencv.org
- Issue tracking: https://github.com/opencv/opencv/issues
- Additional OpenCV functionality: https://github.com/opencv/opencv_contrib
- Donate to OpenCV: https://opencv.org/support/
Contributing
Please read the contribution guidelines before starting work on a pull request.
Summary of the guidelines:
- One pull request per issue;
- Choose the right base branch;
- Include tests and documentation;
- Clean up "oops" commits before submitting;
- Follow the coding style guide.
Additional Resources
- Submit your OpenCV-based project for inclusion in Community Friday on opencv.org
- Subscribe to the OpenCV YouTube Channel featuring OpenCV Live, an hour-long streaming show
- Follow OpenCV on LinkedIn for daily posts showing the state-of-the-art in computer vision & AI
- Apply to be an OpenCV Volunteer to help organize events and online campaigns as well as amplify them
- Follow OpenCV on Mastodon in the Fediverse
- Follow OpenCV on Twitter
- OpenCV.ai: Computer Vision and AI development services from the OpenCV team.
Description
Languages
C++
87.6%
C
3.2%
Python
2.9%
CMake
2%
Java
1.5%
Other
2.6%