From 37040df2066f27757541ec147968f3e596b4cff6 Mon Sep 17 00:00:00 2001 From: Zhewen Tan <1090179959@qq.com> Date: Fri, 16 Oct 2026 19:06:36 +0800 Subject: [PATCH] Merge pull request #30249 from tandede/fix/checkrange-float16 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix checkRange for new 5.x data types 🤖🤖🤖 --- modules/core/src/mathfuncs.cpp | 56 +++++++++++++++++++++++- modules/core/test/test_math.cpp | 77 +++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index 6bb5330e5f..1bdc47e5f5 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -1168,6 +1168,30 @@ check_range_function check_range_functions[] = &checkIntegerRange }; +template +static void checkTypedRange(Mat& src, Point& badPt, double minVal, double maxVal) +{ + int loc = 0; + int cn = src.channels(); + Size size = getContinuousSize2D(src, cn); + const T* isrc = src.ptr(); + size_t step = src.step / sizeof(isrc[0]); + + for( ; badPt.x < 0 && size.height--; loc += size.width, isrc += step ) + { + for( int i = 0; i < size.width; i++ ) + { + double val = (double)isrc[i]; + if( !(val >= minVal && val < maxVal) ) + { + int pixelId = (loc + i) / cn; + badPt = Point(pixelId % src.cols, pixelId / src.cols); + break; + } + } + } +} + bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double maxVal) { CV_INSTRUMENT_REGION(); @@ -1208,7 +1232,15 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma int cn = src.channels(); Size size = getContinuousSize2D(src, cn); - if( depth == CV_32F ) + if( depth == CV_16F ) + { + checkTypedRange(src, badPt, minVal, maxVal); + } + else if( depth == CV_16BF ) + { + checkTypedRange(src, badPt, minVal, maxVal); + } + else if( depth == CV_32F ) { Cv32suf a, b; int ia, ib; @@ -1237,7 +1269,7 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma } } } - else + else if( depth == CV_64F ) { Cv64suf a, b; int64 ia, ib; @@ -1266,6 +1298,26 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma } } } + else if( depth == CV_Bool ) + { + checkTypedRange(src, badPt, minVal, maxVal); + } + else if( depth == CV_32U ) + { + checkTypedRange(src, badPt, minVal, maxVal); + } + else if( depth == CV_64U ) + { + checkTypedRange(src, badPt, minVal, maxVal); + } + else if( depth == CV_64S ) + { + checkTypedRange(src, badPt, minVal, maxVal); + } + else + { + CV_Error(Error::StsUnsupportedFormat, "Unsupported matrix depth in checkRange"); + } } if( badPt.x >= 0 ) diff --git a/modules/core/test/test_math.cpp b/modules/core/test/test_math.cpp index cab932a4ed..341be985a0 100644 --- a/modules/core/test/test_math.cpp +++ b/modules/core/test/test_math.cpp @@ -2318,6 +2318,83 @@ TEST(Core_CheckRange, Inf_Minus) ASSERT_EQ(bad_pt.y, 1); } +typedef testing::TestWithParam Core_CheckRange_Float; + +TEST_P(Core_CheckRange_Float, accuracy) +{ + const int depth = GetParam(); + cv::Mat src(3, 3, CV_MAKETYPE(depth, 3)); + cv::randu(src, cv::Scalar::all(-10), cv::Scalar::all(10)); + cv::Point bad_pt(-1, -1); + + ASSERT_TRUE(checkRange(src, true, &bad_pt)); + ASSERT_EQ(bad_pt.x, -1); + ASSERT_EQ(bad_pt.y, -1); + + const float inf = std::numeric_limits::infinity(); + const int bad_scalar = 2 * src.channels() + 2; + switch (depth) + { + case CV_16F: + src.ptr(1)[bad_scalar] = cv::hfloat(inf); + break; + case CV_16BF: + src.ptr(1)[bad_scalar] = cv::bfloat(inf); + break; + case CV_32F: + src.ptr(1)[bad_scalar] = inf; + break; + default: + FAIL() << "Unexpected depth: " << depth; + } + + ASSERT_FALSE(checkRange(src, true, &bad_pt)); + ASSERT_EQ(bad_pt.x, 2); + ASSERT_EQ(bad_pt.y, 1); +} + +INSTANTIATE_TEST_CASE_P(/*nothing*/, Core_CheckRange_Float, + testing::Values(CV_16F, CV_16BF, CV_32F)); + +typedef testing::TestWithParam Core_CheckRange_ExtendedInteger; + +TEST_P(Core_CheckRange_ExtendedInteger, accuracy) +{ + const int depth = GetParam(); + cv::Mat src = cv::Mat::zeros(2, 3, CV_MAKETYPE(depth, 2)); + cv::Point bad_pt(-1, -1); + + ASSERT_TRUE(checkRange(src, true, &bad_pt, 0, 1)); + ASSERT_EQ(bad_pt.x, -1); + ASSERT_EQ(bad_pt.y, -1); + + const int bad_scalar = src.channels() + 1; + switch (depth) + { + case CV_Bool: + src.ptr(1)[bad_scalar] = 1; + break; + case CV_32U: + src.ptr(1)[bad_scalar] = 1; + break; + case CV_64U: + src.ptr(1)[bad_scalar] = 1; + break; + case CV_64S: + src.ptr(1)[bad_scalar] = 1; + break; + default: + FAIL() << "Unexpected depth: " << depth; + } + + ASSERT_FALSE(checkRange(src, true, &bad_pt, 0, 1)); + ASSERT_EQ(bad_pt.x, 1); + ASSERT_EQ(bad_pt.y, 1); +} + +INSTANTIATE_TEST_CASE_P(/*nothing*/, Core_CheckRange_ExtendedInteger, + testing::Values(CV_Bool, CV_32U, CV_64U, CV_64S)); + REGISTER_TYPED_TEST_CASE_P(Core_CheckRange, Negative, Negative3CN, Positive, Bounds, Zero, One); typedef ::testing::Types mat_data_types;