From 2d15169ab5f97c7e04c79dbb021db15452c907f1 Mon Sep 17 00:00:00 2001 From: Yang Guanyuhan <1523543870@qq.com> Date: Mon, 23 Mar 2026 16:27:37 +0800 Subject: [PATCH] Merge pull request #28692 from YangGuanyuhan:fix-lightglue-assertion-fail dnn: fix dst_dp assertion in broadcast for size-1 dims causing crash in lightglue.onnx model #28692 The original assertion CV_Assert(dst_dp == 1) does not handle valid cases where the innermost dimension size is 1 like [10, 5, 1], resulting in dst_dp == 0. This occurs during broadcasting in LightGlue ONNX model and leads to assertion failure. Allow dst_dp == 0 for size-1 dimensions to handle this edge case correctly. ### 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 - [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 --- modules/core/src/matrix_transform.cpp | 2 +- modules/core/test/test_arithm.cpp | 33 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/core/src/matrix_transform.cpp b/modules/core/src/matrix_transform.cpp index b92c3197b7..b1d72a075b 100644 --- a/modules/core/src/matrix_transform.cpp +++ b/modules/core/src/matrix_transform.cpp @@ -890,7 +890,7 @@ void broadcast(InputArray _src, InputArray _shape, OutputArray _dst) { if (_flatten_for_broadcast(2, max_ndims, all_ndims, orig_shapes, flatten_shapes, flatten_steps)) { size_t src_dp = flatten_steps[0][max_ndims - 1]; size_t dst_dp = flatten_steps[1][max_ndims - 1]; - CV_Assert(dst_dp == 1); + CV_Assert(dst_dp == 1 || dst_dp == 0); CV_Assert(max_ndims >= 2); // >= 3? size_t rowstep_src = flatten_steps[0][max_ndims - 2]; size_t rowstep_dst = flatten_steps[1][max_ndims - 2]; diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index f00a91b6f5..5bc7c23965 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -2818,6 +2818,39 @@ TEST(BroadcastTo, basic) { } } +TEST(BroadcastTo, regression_dst_dp_zero_when_last_dim_is_one) +{ + std::vector shape_src{10, 1, 1}; + std::vector data_src(10); + for (int i = 0; i < 10; ++i) + { + data_src[i] = static_cast(i + 1); + } + Mat src(static_cast(shape_src.size()), shape_src.data(), CV_32FC1, data_src.data()); + + std::vector shape_dst{10, 5, 1}; + Mat dst; + + // Regression for broadcast() path where the innermost destination dimension is 1 + // and flattened destination step can legitimately be 0. + ASSERT_NO_THROW(broadcast(src, shape_dst, dst)); + + EXPECT_EQ(dst.dims, 3); + EXPECT_EQ(dst.size[0], 10); + EXPECT_EQ(dst.size[1], 5); + EXPECT_EQ(dst.size[2], 1); + EXPECT_EQ(dst.type(), CV_32FC1); + + for (int i = 0; i < shape_dst[0]; ++i) + { + for (int j = 0; j < shape_dst[1]; ++j) + { + int idx[] = {i, j, 0}; + EXPECT_FLOAT_EQ(dst.at(idx), static_cast(i + 1)); + } + } +} + TEST(Core_minMaxIdx, regression_9207_2) { const int rows = 13;