mirror of
https://github.com/opencv/opencv.git
synced 2026-09-11 04:43:22 -05:00
755546643a671420fae0c20ec5d4cd24a228da8e
Speed up imgproc warpAffine and warpPerspective for BORDER_TRANSPARENT - #29779 ### Pull Request Readiness Checklist - [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 - [ ] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake ## Why I was using `warpPerspective` to draw several source images on a large destination image in mode `BORDER_TRANSPARENT` and ran into `warpPerspective` being surprisingly slow. Upon reading the code, it turns out that `warpPerspective`, as well as `warpAffine`, iterates over all the destination image's pixels even if the source image gets projected to only a small part of the destination image, resulting in considerable overhead for my use case. I believe other users would also benefit from making this case more efficient. ## Changesd19c343704calculates the ROI of the source image in the destination image and then limits the destination image walk to that area. Given that the existing tests didn't cover `BORDER_TRANSPARENT`, I extended that ine451f59cda. Next to that, I added a small performance test dedicated to this use case (c4e271c2dc). ## Performance improvement The following table show the timing difference before and after, generated using the added perf test (source image projects to 64x64, drawn in a 512x512 destination image): | function | type | interp | base [ms] | opt [ms] | speedup | | --- | --- | --- | --- | --- | --- | | warpAffine | 8UC1 | NEAREST | 0.204 | 0.010 | 19.8× | | warpAffine | 8UC1 | LINEAR | 0.428 | 0.026 | 16.6× | | warpAffine | 8UC4 | NEAREST | 0.239 | 0.021 | 11.5× | | warpAffine | 8UC4 | LINEAR | 0.433 | 0.031 | 14.0× | | warpPerspective | 8UC1 | NEAREST | 0.759 | 0.033 | 23.2× | | warpPerspective | 8UC1 | LINEAR | 1.125 | 0.067 | 16.8× | | warpPerspective | 8UC4 | NEAREST | 0.786 | 0.040 | 19.5× | | warpPerspective | 8UC4 | LINEAR | 1.110 | 0.101 | 11.0× | In short, a 10 to 20x speedup. ## Notes - This is my first PR for the OpenCV project, I'm sorry in case I didn't respect all contribution guidelines. - If relevant, Clause Opus 4.8 was used for exploring the codebase, some code and style suggestions and review.
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): https://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%