From 5137676ea7869e902852ad9933be0ca7779daa90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=99=A8=E5=AE=87?= <12410918@mail.sustech.edu.cn> Date: Wed, 24 Jun 2026 14:05:14 +0800 Subject: [PATCH] Merge pull request #29172 from hcy11123323:op MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fast-path transposeND for identity and 2D transpose orders #29172 ### 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 - [ ] 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. - [x] The feature is well documented and sample code can be built with the project CMake This PR adds fast paths to cv::transposeND() for two common cases: - identity permutation: dispatch to copyTo() - 2D permutation {1, 0}: dispatch to transpose() All other permutations continue to use the existing generic ND implementation. Why: transposeND() currently falls back to the generic memcpy/index-update loop even for the common 2D transpose case, while OpenCV already has an optimized transpose() path. Reusing that path avoids unnecessary index arithmetic and improves performance for 2D inputs passed through transposeND(). Performance: [BinaryOpTest.transposeND/21 (1920x1080, 8UC3): 28.21 ms -> 0.66 ms (-97.7%)] [BinaryOpTest.transposeND/22 (1920x1080, 8UC4): 37.61 ms -> 0.88 ms (-97.7%)] [BinaryOpTest.transposeND/29 (1920x1080, 32FC1): 10.26 ms -> 0.75 ms (-92.7%)] Full transposeND perf subset: 6760 ms -> 592 ms (-91.2%) --- modules/core/perf/perf_arithm.cpp | 79 ++++++++++++++++++++++++--- modules/core/src/matrix_transform.cpp | 27 +++++++++ 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/modules/core/perf/perf_arithm.cpp b/modules/core/perf/perf_arithm.cpp index cd5a750cc5..d8b65e1a08 100644 --- a/modules/core/perf/perf_arithm.cpp +++ b/modules/core/perf/perf_arithm.cpp @@ -436,7 +436,22 @@ PERF_TEST_P_(BinaryOpTest, transpose2d) SANITY_CHECK_NOTHING(); } -PERF_TEST_P_(BinaryOpTest, transposeND) +static cv::Mat makeTransposeNDOutput(const cv::Mat& src, const std::vector& order) +{ + std::vector new_sz(order.size()); + for (size_t i = 0; i < order.size(); ++i) + new_sz[i] = src.size[order[i]]; + return Mat(static_cast(new_sz.size()), new_sz.data(), src.type()); +} + +static cv::Mat makeTransposeNDInput3D(Size sz, int type) +{ + const int channels = CV_MAT_CN(type); + int dims[] = { sz.height, sz.width, channels }; + return Mat(3, dims, CV_MAKETYPE(CV_MAT_DEPTH(type), 1)); +} + +PERF_TEST_P_(BinaryOpTest, transposeND_identity_order) { Size sz = get<0>(GetParam()); int type = get<1>(GetParam()); @@ -444,14 +459,64 @@ PERF_TEST_P_(BinaryOpTest, transposeND) std::vector order(a.dims); std::iota(order.begin(), order.end(), 0); - std::reverse(order.begin(), order.end()); - std::vector new_sz(a.dims); - std::copy(a.size.p, a.size.p + a.dims, new_sz.begin()); - std::reverse(new_sz.begin(), new_sz.end()); - cv::Mat b = Mat(new_sz, type); + cv::Mat b = makeTransposeNDOutput(a, order); - declare.in(a,WARMUP_RNG).out(b); + declare.in(a, WARMUP_RNG).out(b); + + TEST_CYCLE() cv::transposeND(a, order, b); + + SANITY_CHECK_NOTHING(); +} + +PERF_TEST_P_(BinaryOpTest, transposeND_2d_swap_order) +{ + Size sz = get<0>(GetParam()); + int type = get<1>(GetParam()); + cv::Mat a = Mat(sz, type).reshape(1); + + std::vector order{1, 0}; + + cv::Mat b = makeTransposeNDOutput(a, order); + + declare.in(a, WARMUP_RNG).out(b); + + TEST_CYCLE() cv::transposeND(a, order, b); + + SANITY_CHECK_NOTHING(); +} + + +PERF_TEST_P_(BinaryOpTest, transposeND_generic_keep_tail_order) +{ + Size sz = get<0>(GetParam()); + int type = get<1>(GetParam()); + cv::Mat a = makeTransposeNDInput3D(sz, type); + + std::vector order{1, 0, 2}; + + cv::Mat b = makeTransposeNDOutput(a, order); + + randu(a, 0, 255); + declare.in(a).out(b); + + TEST_CYCLE() cv::transposeND(a, order, b); + + SANITY_CHECK_NOTHING(); +} + +PERF_TEST_P_(BinaryOpTest, transposeND_generic_move_tail_order) +{ + Size sz = get<0>(GetParam()); + int type = get<1>(GetParam()); + cv::Mat a = makeTransposeNDInput3D(sz, type); + + std::vector order{2, 0, 1}; + + cv::Mat b = makeTransposeNDOutput(a, order); + + randu(a, 0, 255); + declare.in(a).out(b); TEST_CYCLE() cv::transposeND(a, order, b); diff --git a/modules/core/src/matrix_transform.cpp b/modules/core/src/matrix_transform.cpp index e53462fdf8..fd9485d271 100644 --- a/modules/core/src/matrix_transform.cpp +++ b/modules/core/src/matrix_transform.cpp @@ -551,6 +551,33 @@ void transposeND(InputArray src_, const std::vector& order, OutputArray dst CV_CheckEQ(inp.channels(), 1, "Input array should be single-channel"); CV_CheckEQ(order.size(), static_cast(inp.dims), "Number of dimensions shouldn't change"); + bool isIdentityOrder = true; + for (size_t i = 0; i < order.size(); ++i) + { + if (order[i] != static_cast(i)) + { + isIdentityOrder = false; + break; + } + } + if (isIdentityOrder) + { + dst_.create(inp.dims, inp.size.p, inp.type()); + Mat out = dst_.getMat(); + CV_Assert(out.isContinuous()); + + if (inp.data != out.data) + inp.copyTo(out); + return; + } + + const bool is2DSwap = inp.dims == 2 && order.size() == 2 && order[0] == 1 && order[1] == 0; + if (is2DSwap) + { + transpose(inp, dst_); + return; + } + auto order_ = order; std::sort(order_.begin(), order_.end()); for (size_t i = 0; i < order_.size(); ++i)