Skip to content

imgproc: avoid padding divisible dimensions in CLAHE - #30304

Merged
asmorkalov merged 3 commits into
opencv:4.xfrom
Nikhi00718:fix/imgproc-clahe-border-padding
Oct 21, 2026
Merged

asmorkalov merged 3 commits into
opencv:4.xfrom
Nikhi00718:fix/imgproc-clahe-border-padding

Conversation

@Nikhi00718

@Nikhi00718 Nikhi00718 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request Readiness Checklist

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

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • 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):

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.

@Nikhi00718

Copy link
Copy Markdown
Contributor Author

The current PR:4.x run is marked action_required and has no jobs or check runs: https://github.com/opencv/opencv/actions/runs/38732572726. Could a maintainer approve the workflow for 801fea96e3? The local CPU/OpenCL regression and performance results are recorded in the PR description.

@Nikhi00718 Nikhi00718 changed the title imgproc: avoid padding divisible dimensions in CLAHE 🤖🤖🤖 imgproc: avoid padding divisible dimensions in CLAHE Oct 19, 2026
Comment thread modules/imgproc/test/test_clahe.cpp Resolved
@asmorkalov asmorkalov added this to the 4.15.0 milestone Oct 20, 2026
@asmorkalov asmorkalov added the bug label Oct 20, 2026
@asmorkalov asmorkalov self-assigned this Oct 21, 2026
@asmorkalov
asmorkalov merged commit bc14301 into opencv:4.x Oct 21, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants