Files
opencv-MIRROR/modules/imgproc/test/test_clahe.cpp
NIKHIL bc14301ac1 Merge pull request #30304 from Nikhi00718:fix/imgproc-clahe-border-padding
imgproc: avoid padding divisible dimensions in CLAHE - #30304

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake

`CLAHE::apply` pads both dimensions whenever either dimension is not divisible by the tile grid. For an image with 80 rows and 81 columns and an 8x8 grid, the height already fits, but the implementation adds eight reflected rows anyway. The resulting 11-pixel tile height changes the histograms and interpolation across the image; only seven columns are needed to obtain tiles 11 pixels wide and 10 pixels high.

Compute the minimum reflected padding independently for each dimension in both the CPU and OpenCL paths. Images with both dimensions divisible, or neither dimension divisible, retain their existing tile geometry. No public interface changes.

Reproduction (applies to both `uint8` and `uint16`):

```python
import cv2
import numpy as np

src = np.random.default_rng(42).integers(0, 65536, (80, 81), dtype=np.uint16)
clahe = cv2.createCLAHE(2.0, (8, 8))
actual = clahe.apply(src)
padded = cv2.copyMakeBorder(src, 0, 0, 0, 7, cv2.BORDER_REFLECT_101)
expected = clahe.apply(padded)[:80, :81]
assert np.array_equal(actual, expected)  # fails before this fix
```

Testing on `b254d3455a`, Linux x86-64, GCC 15.2, Release, IPP disabled, OpenCL enabled with PoCL 6 CPU device (`OPENCV_OPENCL_DEVICE=:CPU:`, `POCL_MAX_PTHREAD_COUNT=4`, `OPENCV_FOR_THREADS_NUM=2`):

- On base `9d80321677` with only the new tests: 88 failed, 64 passed. All 24 new OpenCL cases fail; the remaining failures are the CPU mixed-divisibility cases.
- With the fix, `opencv_test_imgproc --gtest_filter="*CLAHE_Padding*:*CLAHETest.ReflectedBorder*"`: 152 passed.
- `OPENCV_TEST_DATA_PATH=~/ocv-testdata opencv_test_imgproc --gtest_filter="*Hist*:*BackProject*:*CLAHE*"`: 232 passed, including the existing OpenCL CLAHE accuracy tests. The initial run lacked `cv/optflow/RubberWhale1.png`; rerunning with the official `opencv_extra` image passed.
- `opencv_perf_imgproc --gtest_filter="*CLAHE*" --perf_min_samples=10 --perf_force_samples=10`: 12 existing CLAHE performance cases passed, plus four framework checks.
- `git diff --check`: passed.
- CI revision `801fea96e3`: renamed the OpenCL test RNG to avoid shadowing its fixture member. Rebuilt `opencv_test_imgproc` with no compiler warnings and reran the selected accuracy suite: 232 passed.

The regression matrix covers 8-/16-bit inputs, square/rectangular/single-axis grids, all divisibility combinations, clipping, strided inputs, and in-place use. OpenCL cases cover both axes and UMat offsets. No additional test data is needed. Full imgproc suite and physical GPU execution were not run.

The reproduction above is the original report; no earlier issue or competing fix was found.
2026-10-21 15:13:32 +03:00

51 lines
2.1 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 ends at the parent's bottom-right corner so border extrapolation
// uses the same pixels as a standalone image, while preserving a row stride.
Mat storage(size.height + 2, size.width + 3, type);
Mat src = useRoi ? storage(Rect(3, 2, size.width, size.height)) : Mat(size, type);
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);
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