From ebf8a55d6c5a8f419f8b9e3ffd0ca9bbcdd4f8da Mon Sep 17 00:00:00 2001 From: Vladimir Iglovikov Date: Tue, 24 Nov 2026 17:00:49 +0900 Subject: [PATCH] =?UTF-8?q?core:=20support=20CV=5F16F=20in=20the=20legacy?= =?UTF-8?q?=20reduce=20overload=20=F0=9F=A4=96=F0=9F=A4=96=F0=9F=A4=96=20(?= =?UTF-8?q?#31032)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * core: support CV_16F in the legacy reduce overload Delegate half-precision CPU input to the existing N-D reduction kernels. Preserve the 2-D result for a one-dimensional legacy input. * core: use CV_CheckEQ for the 1D dim check in CV_16F reduce Co-Authored-By: Claude Sonnet 5.5 --------- Co-authored-by: Claude Sonnet 5.5 --- modules/core/include/opencv2/core.hpp | 1 + modules/core/perf/perf_reduce.cpp | 16 ++++++++++ modules/core/src/matrix_operations.cpp | 11 +++++++ modules/core/test/test_mat.cpp | 43 ++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index abb7086819..4ea03963bc 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -895,6 +895,7 @@ obtained. For example, the function can be used to compute horizontal and vertic raster image. In case of #REDUCE_MAX and #REDUCE_MIN, the output image should have the same type as the source one. In case of #REDUCE_SUM, #REDUCE_SUM2 and #REDUCE_AVG, the output may have a larger element bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction modes. +On the CPU, CV_16F inputs use the n-dimensional reduction kernels (see cv::ReduceParams). The following code demonstrates its usage for a single channel matrix. @snippet snippets/core_reduce.cpp example diff --git a/modules/core/perf/perf_reduce.cpp b/modules/core/perf/perf_reduce.cpp index bcd10b18c5..51b4ee66fd 100644 --- a/modules/core/perf/perf_reduce.cpp +++ b/modules/core/perf/perf_reduce.cpp @@ -8,6 +8,22 @@ CV_ENUM(ROp, REDUCE_SUM, REDUCE_AVG, REDUCE_MAX, REDUCE_MIN, REDUCE_SUM2) typedef tuple Size_MatType_ROp_t; typedef perf::TestBaseWithParam Size_MatType_ROp; +typedef perf::TestBaseWithParam > Size_Axis; + +PERF_TEST_P(Size_Axis, reduce_fp16_sum, + testing::Combine(testing::Values(szVGA, sz1080p), testing::Values(0, 1))) +{ + const Size size = get<0>(GetParam()); + const int dim = get<1>(GetParam()); + Mat wide(size, CV_32FC(9)), src, dst; + randu(wide, 0, 1); + wide.convertTo(src, CV_16F); + + TEST_CYCLE() cv::reduce(src, dst, dim, REDUCE_SUM, CV_64F); + + SANITY_CHECK_NOTHING(); +} + PERF_TEST_P(Size_MatType_ROp, reduceR, testing::Combine( diff --git a/modules/core/src/matrix_operations.cpp b/modules/core/src/matrix_operations.cpp index 2858c35655..a52ffdaa57 100644 --- a/modules/core/src/matrix_operations.cpp +++ b/modules/core/src/matrix_operations.cpp @@ -822,6 +822,17 @@ void cv::reduce(InputArray _src, OutputArray _dst, int dim, int op, int dtype) src.convertTo(_dst, dtype); return; } + if (sdepth == CV_16F) + { + if (src.dims <= 1) + { + CV_CheckEQ(dim, 0, "1D input can only be reduced along dim 0"); + src = src.reshape(cn, {1, src.cols}); + dim = 1; + } + cv::reduce(src, _dst, cv::ReduceParams(op, {dim}, true, ddepth)); + return; + } if (src.dims <= 1) { CV_Assert(dim == 0); dim = 1; diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 3944086255..5263730e83 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -1183,6 +1183,49 @@ protected: TEST(Core_Reduce, accuracy) { Core_ReduceTest test; test.safe_run(); } +typedef testing::TestWithParam > Core_ReduceFP16; + +TEST_P(Core_ReduceFP16, matches_widened_reference) +{ + const int channels = get<0>(GetParam()); + const int dim = get<1>(GetParam()); + const int op = get<2>(GetParam()); + Mat1f values(9, 11 * channels); + for (int row = 0; row < values.rows; ++row) + for (int col = 0; col < values.cols; ++col) + values(row, col) = ((row * 11 + col) % 49 - 24) * 0.0625f; + Mat storage; + values.reshape(channels).convertTo(storage, CV_16F); + Mat src = storage(Rect(1, 1, 9, 7)); + const bool extrema = op == REDUCE_MIN || op == REDUCE_MAX; + const int dtype = extrema ? CV_16F : CV_64F; + Mat dst, wide, expected; + cv::reduce(src, dst, dim, op, dtype); + src.convertTo(wide, CV_64F); + cv::reduce(wide, expected, dim, op, CV_64F); + ASSERT_EQ(dst.size(), expected.size()); + ASSERT_EQ(dst.type(), CV_MAKETYPE(dtype, channels)); + dst.convertTo(dst, CV_64F); + EXPECT_LE(cvtest::norm(dst, expected, NORM_INF), 1e-12); +} + +INSTANTIATE_TEST_CASE_P(/**/, Core_ReduceFP16, + testing::Combine(testing::Values(1, 3, 9), testing::Values(0, 1), + testing::Values(REDUCE_SUM, REDUCE_AVG, REDUCE_SUM2, REDUCE_MIN, REDUCE_MAX))); + +TEST(Core_Reduce, fp16_vector_keeps_matrix_output) +{ + const Mat1f values({4}, {1, 2, 3, 4}); + Mat src, dst; + values.convertTo(src, CV_16F); + ASSERT_EQ(src.dims, 1); + cv::reduce(src, dst, 0, REDUCE_SUM, CV_32F); + ASSERT_EQ(dst.dims, 2); + ASSERT_EQ(dst.size(), Size(1, 1)); + ASSERT_EQ(dst.type(), CV_32F); + EXPECT_EQ(dst.at(0, 0), 10.f); +} + // Core_ReduceTest fills with randu(0, 100), so it never reaches the range where a 32-bit // accumulator would overflow. CV_32S reduces through a double accumulator for that reason. TEST(Core_Reduce, int32_accumulator_does_not_overflow)