From 5a5ac33c874d8d3bbfb5d2812bbb701ba77b9fcb Mon Sep 17 00:00:00 2001 From: Yvonne Date: Tue, 4 Aug 2026 11:55:18 +0800 Subject: [PATCH] make absdiff overflow test accept saturate or wraparound --- modules/core/include/opencv2/core.hpp | 2 +- modules/core/test/test_arithm.cpp | 33 ++++++++++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index e3836523d5..577225e109 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -1437,7 +1437,7 @@ The function cv::absdiff calculates: \f[\texttt{dst}(I) = \texttt{saturate} (| \texttt{src1} - \texttt{src2}(I) |)\f] where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each channel is processed independently. -@note Saturation is not applied when the arrays have the depth CV_32S. +@note Saturation might not be applied when the arrays have the depth CV_32S. You may even get a negative value in the case of overflow. @note (Python) Be careful to difference behaviour between src1/src2 are single number and they are tuple/array. `absdiff(src,X)` means `absdiff(src,(X,X,X,X))`. diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 4c64e36f91..7eb642b51d 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -2849,13 +2849,15 @@ TEST(Core_ConvertTo, regression_12121) } TEST(Core_AbsDiff, regression_29639_integer_overflow) - { - const struct { int a, b, expected; } cases[] = { - { INT_MIN, 0, INT_MIN }, - { 0, INT_MIN, INT_MIN }, - { INT_MIN, -1, INT_MAX }, - { INT_MAX, 0, INT_MAX }, - { 7, -5, 12 }, +{ + const struct { int a, b; } cases[] = { + { INT_MIN, 0 }, + { 0, INT_MIN }, + { INT_MIN, INT_MAX }, + { INT_MAX, INT_MIN }, + { INT_MIN, -1 }, + { INT_MAX, 0 }, + { 7, -5 }, }; for (const auto& c : cases) { @@ -2863,10 +2865,19 @@ TEST(Core_AbsDiff, regression_29639_integer_overflow) cv::Mat b(3, 11, CV_32SC1, cv::Scalar(c.b)); cv::Mat d; cv::absdiff(a, b, d); - EXPECT_EQ(c.expected, d.at(0, 0)) - << "absdiff(" << c.a << ", " << c.b << ") first element (vector path)"; - EXPECT_EQ(c.expected, d.at(d.rows - 1, d.cols - 1)) - << "absdiff(" << c.a << ", " << c.b << ") last element (scalar remainder)"; + + int wraparound = (int)((unsigned)std::max(c.a, c.b) - (unsigned)std::min(c.a, c.b)); + int64 diff = (int64)c.a - (int64)c.b; + int saturated = cv::saturate_cast(diff < 0 ? -diff : diff); + + int first = d.at(0, 0); + int last = d.at(d.rows - 1, d.cols - 1); + EXPECT_TRUE(first == wraparound || first == saturated) + << "absdiff(" << c.a << ", " << c.b << ") first element (vector path) = " + << first << ", expected wraparound " << wraparound << " or saturate " << saturated; + EXPECT_TRUE(last == wraparound || last == saturated) + << "absdiff(" << c.a << ", " << c.b << ") last element (scalar remainder) = " + << last << ", expected wraparound " << wraparound << " or saturate " << saturated; } }