// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #include "test_precomp.hpp" namespace opencv_test { namespace { typedef testing::TestWithParam > CLAHE_Padding; TEST_P(CLAHE_Padding, reflected_border) { const int type = testing::get<0>(GetParam()); const Size grid = testing::get<1>(GetParam()); const Size extra = testing::get<2>(GetParam()); const double clipLimit = testing::get<3>(GetParam()); const bool useRoi = testing::get<4>(GetParam()); const Size size(grid.width * 5 + extra.width, grid.height * 5 + extra.height); // The ROI sits inside the parent with more than a tile of parent pixels to its right and // bottom, so the internal padding must not read them (it would if it did not isolate the ROI). Mat storage(size.height + 2 + 8, size.width + 3 + 8, type); theRNG().fill(storage, RNG::UNIFORM, 0, type == CV_8UC1 ? 256 : 65536); Mat src = useRoi ? storage(Rect(3, 2, size.width, size.height)) : Mat(size, type); if (!useRoi) theRNG().fill(src, RNG::UNIFORM, 0, type == CV_8UC1 ? 256 : 65536); const int bottom = (grid.height - size.height % grid.height) % grid.height; const int right = (grid.width - size.width % grid.width) % grid.width; Mat padded, expected, actual; cv::copyMakeBorder(src, padded, 0, bottom, 0, right, BORDER_REFLECT_101 | BORDER_ISOLATED); Ptr clahe = cv::createCLAHE(clipLimit, grid); clahe->apply(padded, expected); clahe->apply(src, actual); EXPECT_EQ(src.type(), actual.type()); EXPECT_EQ(src.size(), actual.size()); EXPECT_MAT_NEAR(expected(Rect(Point(), size)), actual, 0); Mat inPlace = src.clone(); clahe->apply(inPlace, inPlace); EXPECT_MAT_NEAR(actual, inPlace, 0); } INSTANTIATE_TEST_CASE_P(Imgproc, CLAHE_Padding, testing::Combine( testing::Values(MatType(CV_8UC1), MatType(CV_16UC1)), testing::Values(Size(8, 8), Size(3, 5), Size(1, 8), Size(8, 1)), testing::Values(Size(0, 0), Size(1, 0), Size(0, 1), Size(1, 1)), testing::Values(0.0, 2.0), testing::Bool())); // The numbers of tiles are used as divisors, so a tile grid with a zero in it used to end in a division by zero // and a crash. A negative size used to be rejected only by accident, when a matrix of negative size was created. TEST(Imgproc_CLAHE, regression_invalid_tile_grid) { const Size grids[] = {Size(0, 0), Size(0, 8), Size(8, 0), Size(-1, -1), Size(-8, 8), Size(8, -8)}; const int types[] = {CV_8UC1, CV_16UC1}; for (size_t t = 0; t < sizeof(types) / sizeof(types[0]); t++) { Mat src(64, 64, types[t], Scalar(7)), dst; for (size_t g = 0; g < sizeof(grids) / sizeof(grids[0]); g++) { SCOPED_TRACE(cv::format("type %d, grid %d x %d", types[t], grids[g].width, grids[g].height)); Ptr created = cv::createCLAHE(2.0, grids[g]); EXPECT_THROW(created->apply(src, dst), cv::Exception); Ptr changed = cv::createCLAHE(2.0, Size(8, 8)); changed->setTilesGridSize(grids[g]); EXPECT_THROW(changed->apply(src, dst), cv::Exception); // a valid grid works again changed->setTilesGridSize(Size(8, 8)); EXPECT_NO_THROW(changed->apply(src, dst)); EXPECT_EQ(src.size(), dst.size()); } } } }} // namespace