make absdiff overflow test accept saturate or wraparound

This commit is contained in:
Yvonne
2026-08-04 11:55:18 +08:00
parent 154dac563f
commit 5a5ac33c87
2 changed files with 23 additions and 12 deletions

View File

@@ -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))`.

View File

@@ -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<int>(0, 0))
<< "absdiff(" << c.a << ", " << c.b << ") first element (vector path)";
EXPECT_EQ(c.expected, d.at<int>(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<int>(diff < 0 ? -diff : diff);
int first = d.at<int>(0, 0);
int last = d.at<int>(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;
}
}