dnn: support CV_32U and CV_64U in Clip default bounds

This commit is contained in:
Shiven Solanki
2026-11-20 21:42:20 +08:00
parent c7a8a7afbd
commit fe06ceaca5
2 changed files with 38 additions and 0 deletions

View File

@@ -44,6 +44,8 @@ static double typeMin(int depth)
case CV_16S: return std::numeric_limits<short>::lowest();
case CV_32S: return std::numeric_limits<int>::lowest();
case CV_64S: return (double)std::numeric_limits<int64_t>::lowest();
case CV_32U: return std::numeric_limits<uint32_t>::lowest();
case CV_64U: return (double)std::numeric_limits<uint64_t>::lowest();
case CV_32F: return -FLT_MAX;
case CV_64F: return -DBL_MAX;
default: CV_Error(Error::StsUnsupportedFormat, "Clip: unsupported depth");
@@ -60,6 +62,8 @@ static double typeMax(int depth)
case CV_16S: return std::numeric_limits<short>::max();
case CV_32S: return std::numeric_limits<int>::max();
case CV_64S: return std::nextafter((double)std::numeric_limits<int64_t>::max(), 0.0);
case CV_32U: return std::numeric_limits<uint32_t>::max();
case CV_64U: return (double)std::numeric_limits<uint64_t>::max();
case CV_32F: return FLT_MAX;
case CV_64F: return DBL_MAX;
default: CV_Error(Error::StsUnsupportedFormat, "Clip: unsupported depth");

View File

@@ -5133,4 +5133,38 @@ TEST(Layer_Test_OneHot, UInt16Values)
EXPECT_EQ((int)out.at<uint16_t>(0, 1), 0);
}
typedef testing::TestWithParam<tuple<MatDepth, double> > Layer_Test_Clip_Unsigned;
TEST_P(Layer_Test_Clip_Unsigned, MinOnly)
{
const int depth = get<0>(GetParam());
const double typeMax = get<1>(GetParam());
LayerParams lp;
lp.type = "Clip";
lp.name = "test_clip";
lp.set("min", 2.f); // no "max": the upper bound is the largest value of the type
Ptr<Layer> layer = LayerFactory::createLayerInstance(lp.type, lp);
ASSERT_TRUE(layer);
// The largest value checks that the missing max does not clip anything
const double inVals[] = {0, 1, 3, typeMax};
const double refVals[] = {2, 2, 3, typeMax};
Mat input, ref;
Mat(1, 4, CV_64F, (void*)inVals).convertTo(input, depth);
Mat(1, 4, CV_64F, (void*)refVals).convertTo(ref, depth);
std::vector<Mat> inputs{input}, outputs;
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(depth, outputs[0].depth());
EXPECT_EQ(0, cvtest::norm(outputs[0], ref, NORM_INF));
}
INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Clip_Unsigned, Values(
make_tuple(CV_32U, (double)std::numeric_limits<uint32_t>::max()),
make_tuple(CV_64U, (double)std::numeric_limits<uint64_t>::max())
));
}} // namespace