Skip to content

imgproc: do not read pixels outside a sub-matrix in CLAHE padding - #30782

Merged
abhishek-gola merged 2 commits into
opencv:4.xfrom
sohailshk:fix/clahe-roi-padding
Nov 13, 2026
Merged

abhishek-gola merged 2 commits into
opencv:4.xfrom
sohailshk:fix/clahe-roi-padding

Conversation

@sohailshk

@sohailshk sohailshk commented Nov 12, 2026 •

Copy link
Copy Markdown

When the image 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 and one-sided: CLAHE has no borderType, and it is only applied when the size is not divisible by the grid. Without BORDER_ISOLATED, copyMakeBorder takes those rows and columns from the parent matrix if the source is a region of a larger image, so the histograms of the last tiles and the output depended on pixels outside the region (a 23x17 region with a 4x4 grid differed from the same pixels in their own matrix). Unlike a filter halo this is not something a caller can ask for.

Both copyMakeBorder calls (CPU and OpenCL path) now pass BORDER_ISOLATED, the same fix as #30725 (phaseCorrelate, merged 2026-11-10). Callers that pass a sub-matrix with non-divisible size will see different (corrected) output; whole matrices and divisible sizes are unchanged.

Imgproc_CLAHE.roi_ignores_pixels_outside (8- and 16-bit) compares a region with unrelated pixels around it against a continuous copy; it fails on 4.x without the change and passes with it. The #30304 test is unaffected (its region ends at the parent's corner). The OpenCL branch was not built or run here.

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

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 opencv#30725.

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟡 Yellow

Both CLAHE padding paths now isolate ROI inputs, so their synthetic right/bottom border cannot read pixels from the parent matrix. The focused regression exercises the affected non-divisible tile geometry for both supported depths and matches the established phaseCorrelate fix. No actionable source issue found; LGTM pending exact-head CI, including the OpenCL path.

@varun-jaiswal17
varun-jaiswal17 self-requested a review November 12, 2026 06:44
Comment thread modules/imgproc/test/test_clahe.cpp Resolved
Move the ROI regression into CLAHE_Padding: the ROI now has parent pixels to its right and bottom, and the reference padding is isolated.

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟡 Yellow

The update folds the interior-ROI regression into the existing CLAHE padding fixture while preserving both supported depths and the non-divisible padding cases. The test still distinguishes isolated padding from parent-backed padding, so the outstanding review request is resolved. I found no remaining actionable issue in the delta; exact-head CI, including the OpenCL path, has not run yet.

@sohailshk

Copy link
Copy Markdown
Author

#30781 is already done.

@varun-jaiswal17 varun-jaiswal17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@abhishek-gola abhishek-gola added this to the 4.15.0 milestone Nov 13, 2026
@abhishek-gola abhishek-gola self-assigned this Nov 13, 2026
@abhishek-gola
abhishek-gola merged commit 0dd4bf3 into opencv:4.x Nov 13, 2026
30 of 32 checks passed
hardikkaurani added a commit to hardikkaurani/opencv that referenced this pull request Nov 20, 2026
…cing padding

When processing a sub-matrix with VNG Bayer demosaicing (COLOR_Bayer*2BGR_VNG
and COLOR_Bayer*2RGB_VNG), copyMakeBorder uses BORDER_REFLECT_101 without
BORDER_ISOLATED. This causes the internal 2-pixel boundary halo to be read from
the surrounding pixels of the parent matrix rather than reflecting the region's
own borders. Consequently, the outer 2 rows and columns of the demosaiced image
depend on unrelated pixels in the parent buffer.

This patch adds BORDER_ISOLATED to the copyMakeBorder call in Bayer2RGB_VNG_8u,
ensuring the 2-pixel padding is strictly computed by reflecting the region
boundary. This matches the behavior of isolated/cloned matrices and follows the
same pattern as the CLAHE (opencv#30782) and phaseCorrelate (opencv#30725) ROI padding fixes.

Also instantiates ImgProc_BayerEdgeAware_SubMatrix with VNG codes and CV_8U,
covering sub-matrix source and destination regions across all 4 Bayer VNG codes
with odd and even dimensions.
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.

3 participants