Files
opencv-MIRROR/modules/imgproc/test/test_clahe.cpp
Mohammad Sohail Shaikh 0dd4bf367e imgproc: do not read pixels outside a sub-matrix in CLAHE padding (#30782)
* imgproc: do not read pixels outside a sub-matrix in CLAHE padding

When the size is not a multiple of the tile grid, CLAHE pads the source on the right and bottom with BORDER_REFLECT_101. This padding is internal (CLAHE has no borderType). Without BORDER_ISOLATED, copyMakeBorder takes those rows and columns from the parent matrix when the source is a region of a larger image, so the histograms of the last tiles and the result depended on pixels outside the region. Same pattern as the phaseCorrelate fix in #30725.

* imgproc: extend CLAHE_Padding to cover interior ROIs

Move the ROI regression into CLAHE_Padding: the ROI now has parent pixels to its right and bottom, and the reference padding is isolated.
2026-11-13 20:23:23 +05:30

53 lines
2.2 KiB
C++

// 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<testing::tuple<MatType, Size, Size, double, bool> > 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> 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()));
}} // namespace