From 1ae9b2b523ee61540fceb2b4bb9635fa552aaa24 Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Fri, 20 Nov 2026 14:37:51 +0800 Subject: [PATCH] core: support 1D Mat in findNonZero Fixes #30995 --- modules/core/src/count_non_zero.dispatch.cpp | 2 +- modules/core/test/test_arithm.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/modules/core/src/count_non_zero.dispatch.cpp b/modules/core/src/count_non_zero.dispatch.cpp index f4aa0d66af..07d06ba1b4 100644 --- a/modules/core/src/count_non_zero.dispatch.cpp +++ b/modules/core/src/count_non_zero.dispatch.cpp @@ -153,7 +153,7 @@ int countNonZero(InputArray _src) void findNonZero(InputArray _src, OutputArray _idx) { Mat src = _src.getMat(); - CV_Assert( src.channels() == 1 && src.dims == 2 ); + CV_Assert( src.channels() == 1 && src.dims <= 2 ); int depth = src.depth(); std::vector idxvec; diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 9bfb5a98c9..4629875cd0 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -2487,6 +2487,20 @@ TEST(Core_FindNonZero, wide_rows_8u) EXPECT_EQ(expected, pts); } +TEST(Core_FindNonZero, mat_1d) +{ + int sz[] = {7}; + Mat src(1, sz, CV_8U, Scalar(0)); + src.at(2) = 5; + src.at(6) = 1; + + std::vector pts; + findNonZero(src, pts); + + std::vector expected = { Point(2, 0), Point(6, 0) }; + EXPECT_EQ(expected, pts); +} + TEST(Core_BoolVector, support) { std::vector test;