Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(enhance): match OpenCV CLAHE blending (#5716)

Puttala Jeevan Kumar6da8474d6bb9
diff --git a/changelog.d/5655.breaking.md b/changelog.d/5655.breaking.md
index 2927959f9..0ad346ba4 100644
--- a/changelog.d/5655.breaking.md
+++ b/changelog.d/5655.breaking.md
@@ -1 +1 @@
-`equalize_clahe` and `RandomClahe` now spread the residual of the clipped pixels the way OpenCV does, one count on every `max(256 // residual, 1)`-th histogram bin from bin 0, in the fast, per-sample `clip_limit` and differentiable paths. They used to write the residual into the first `residual` bins, which lifted every tile's lookup table: wherever clipping left a residual, the output was brighter than `cv2.createCLAHE`, by about 40 gray levels on average for 64 x 80 to 96 x 96 images with an 8 x 8 grid and clip limit 2.0, and by 4 to 5 levels at 224 x 224. The output now changes for every tile whose clipping leaves a residual (#5654). On the images we measured (64 x 80 to 512 x 512, 8 x 8 grid, clip limits 2, 4 and 40), the mean signed difference from OpenCV is now within 0.1 gray levels in the fast, per-sample and differentiable paths, with the fast paths rounding each lookup-table entry as OpenCV does (#5659). Smooth images in small tiles can still differ by several levels, because kornia blends neighbouring tiles with different weights (#5698).
+`equalize_clahe` and `RandomClahe` now spread the residual of the clipped pixels the way OpenCV does, one count on every `max(256 // residual, 1)`-th histogram bin from bin 0, in the fast, per-sample `clip_limit` and differentiable paths. They used to write the residual into the first `residual` bins, which lifted every tile's lookup table: wherever clipping left a residual, the output was brighter than `cv2.createCLAHE`, by about 40 gray levels on average for 64 x 80 to 96 x 96 images with an 8 x 8 grid and clip limit 2.0, and by 4 to 5 levels at 224 x 224. The output now changes for every tile whose clipping leaves a residual (#5654). On the images we measured (64 x 80 to 512 x 512, 8 x 8 grid, clip limits 2, 4 and 40), the mean signed difference from OpenCV is now within 0.1 gray levels in the fast, per-sample and differentiable paths, with the fast paths rounding each lookup-table entry as OpenCV does (#5659).
diff --git a/changelog.d/5679.breaking.md b/changelog.d/5679.breaking.md
index e3d4a573c..f649b3d8e 100644
--- a/changelog.d/5679.breaking.md
+++ b/changelog.d/5679.breaking.md
@@ -1 +1 @@
-`equalize_clahe` and `RandomClahe` now round fast-path lookup-table entries to the nearest integer (ties to even), matching OpenCV's LUT quantization, instead of flooring them. In float32 and float64 each fast-path output pixel rises by at most one gray level (1/255), and for the same tile its lookup table now equals OpenCV's (#5659). The tile blending still differs from OpenCV (#5698). The differentiable path is unchanged.
+`equalize_clahe` and `RandomClahe` now round fast-path lookup-table entries to the nearest integer (ties to even), matching OpenCV's LUT quantization, instead of flooring them. In float32 and float64 each fast-path output pixel rises by at most one gray level (1/255), and for the same tile its lookup table now equals OpenCV's (#5659). The differentiable path is unchanged.
diff --git a/changelog.d/5716.breaking.md b/changelog.d/5716.breaking.md
new file mode 100644
index 000000000..35d41bfd5
--- /dev/null
+++ b/changelog.d/5716.breaking.md
@@ -0,0 +1 @@
+`equalize_clahe` and `RandomClahe` now blend neighbouring tile lookup tables with weight steps of `1 / tile_size`, instead of `1 / (tile_size - 1)`, anchored at each tile's actual centre. This changes interpolated output values in both the fast and differentiable paths. Kornia intentionally anchors the blend half a pixel before OpenCV/scikit-image's historical phase to preserve flip symmetry on unpadded grids. For the same tiles and lookup tables, the phase difference is at most half an interpolation step per axis; exact `cv2.createCLAHE` parity is not the contract. On the images we measured with an 8 x 8 grid and even tiles (64 x 80 to 512 x 512, clip limits 2 and 40), the largest difference from `cv2.createCLAHE` fell to between one half and two thirds of what it was (21 instead of 39 gray levels on a smooth 64 x 80 image at clip limit 40, 3 instead of 5 on the 512 x 512 `camera` image), while the mean difference stayed close to what it was (7.0 instead of 7.6 and 0.54 instead of 0.57 levels), because the half-pixel offset from OpenCV's phase changes every blended pixel. Border regions still clamp to the nearest tile along the border axis. LUT calculation and quantization are unchanged; Kornia's existing even-tile sizing and padding can cause additional differences from OpenCV.
diff --git a/kornia/enhance/equalization.py b/kornia/enhance/equalization.py
index d3831200f..741210539 100644
--- a/kornia/enhance/equalization.py
+++ b/kornia/enhance/equalization.py
@@ -360,16 +360,21 @@ def _compute_equalized_tiles(interp_tiles: torch.Tensor, luts: torch.Tensor) ->
tiles_equalized: torch.Tensor = torch.zeros_like(interp_tiles)
# compute the interpolation weights (shapes are 2 x TH x TW because they must be applied to 2 interp tiles)
+ # A tile's centre is at (tile_size - 1) / 2 relative to its first pixel. Each half-tile pair
+ # starts half a pixel past a centre, so the left/top weight is 1 - (k + 0.5) / tile_size.
+ # This keeps the 1 / tile_size step and, unlike OpenCV's half-pixel-later phase, preserves flip symmetry.
ih = (
- torch.arange(2 * th - 1, -1, -1, dtype=interp_tiles.dtype, device=interp_tiles.device)
- .div(2.0 * th - 1)[None]
+ torch.arange(2 * th, 0, -1, dtype=interp_tiles.dtype, device=interp_tiles.device)
+ .sub(0.5)
+ .div(2.0 * th)[None]
.transpose(-2, -1)
.expand(2 * th, tw)
)
ih = ih.unfold(0, th, th).unfold(1, tw, tw) # 2 x 1 x TH x TW
iw = (
- torch.arange(2 * tw - 1, -1, -1, dtype=interp_tiles.dtype, device=interp_tiles.device)
- .div(2.0 * tw - 1)
+ torch.arange(2 * tw, 0, -1, dtype=interp_tiles.dtype, device=interp_tiles.device)
+ .sub(0.5)
+ .div(2.0 * tw)
.expand(th, 2 * tw)
)
iw = iw.unfold(0, th, th).unfold(1, tw, tw) # 1 x 2 x TH x TW
@@ -419,7 +424,13 @@ def equalize_clahe(
.. image:: _static/img/equalize_clahe.png
- NOTE: Lut computation uses the same approach as in OpenCV, in next versions this can change.
+ NOTE: The fast path uses OpenCV-style LUT calculation and round-to-nearest quantization.
+ Both paths blend LUTs with weight steps of ``1 / tile_size``, anchored at each tile's actual centre,
+ with border LUTs clamped. This is half a pixel before OpenCV/scikit-image's historical phase:
+ Kornia intentionally uses ``(p + 0.5) / tile_size - 0.5`` to preserve flip symmetry on unpadded grids.
+ For the same tiles and LUTs, the phase difference is at most half an interpolation step per axis;
+ exact ``cv2.createCLAHE`` parity is not the contract. Tiles are rounded up to even sizes and reflect-padded,
+ which can differ from OpenCV's tile/padding convention, and output values are not rounded to 8-bit integers.
Args:
input: images tensor to equalize with values in the range [0, 1] and shape :math:`(*, C, H, W)`.
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index 79616799f..3ea4b38b9 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -4541,7 +4541,8 @@ class TestRandomChannelShuffle(BaseTester):
class TestRandomClahe(BaseTester):
def test_smoke(self, device, dtype):
img = torch.arange(36, device=device, dtype=dtype).reshape(2, 2, 3, 3) / 36
- expected = torch.tensor(1913 / 85, device=device, dtype=dtype)
+ # Centre-anchored blending of the 2 x 2 tile LUTs, evaluated with the per-pixel reference.
+ expected = torch.tensor(86173 / (16 * 255), device=device, dtype=dtype)
self.assert_close(RandomClahe(p=1.0, grid_size=(2, 2))(img).sum(), expected)
@pytest.mark.parametrize("batch_shape", [(1, 3, 5, 7), (3, 1, 5, 7)])
@@ -4582,10 +4583,12 @@ class TestRandomClahe(BaseTester):
self.assert_close(actual, expected)
if slow_and_differentiable:
weights = torch.rand_like(actual)
- self.assert_close(
- torch.autograd.grad(actual, input_data, weights)[0],
- torch.autograd.grad(expected, input_data, weights)[0],
- )
+ grad_actual = torch.autograd.grad(actual, input_data, weights)[0]
+ grad_expected = torch.autograd.grad(expected, input_data, weights)[0]
+ # The gradients reach a few hundred, so rounding leaves absolute noise above the float32 atol on
+ # entries near zero; compare relative to the largest entry.
+ scale = grad_expected.abs().max()
+ self.assert_close(grad_actual / scale, grad_expected / scale)
def test_clip_limit_on_the_input_device(self, device, dtype):
# The per-sample thresholds are computed in float64, on the CPU for an MPS clip tensor. A single
diff --git a/tests/enhance/test_equalization.py b/tests/enhance/test_equalization.py
index b6ac64cb1..b5205b9ae 100644
--- a/tests/enhance/test_equalization.py
+++ b/tests/enhance/test_equalization.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+import math
from typing import Tuple
import pytest
@@ -28,6 +29,30 @@ from kornia.geometry import rotate
from testing.base import BaseTester
+def _clahe_interpolation_reference(img, luts, tile_size):
+ # A tile spans i*T .. i*T+T-1, so its centre is i*T+(T-1)/2. Evaluate the blend between
+ # these centres pixel by pixel, independently of production's half-tile indexing.
+ values = img.detach().cpu().double()
+ tables = luts.detach().cpu().double()
+ expected = torch.zeros_like(values)
+ gh, gw = tables.shape[1:3]
+ for y in range(values.shape[-2]):
+ fy = (y + 0.5) / tile_size[0] - 0.5
+ y0 = math.floor(fy)
+ ay = fy - y0
+ for x in range(values.shape[-1]):
+ fx = (x + 0.5) / tile_size[1] - 0.5
+ x0 = math.floor(fx)
+ ax = fx - x0
+ # Preserve the input dtype's lookup quantization; this reference tests only interpolation.
+ index = (img[..., y, x] * 255).long().cpu().unsqueeze(-1)
+ for row, wy in [(y0, 1 - ay), (y0 + 1, ay)]:
+ for col, wx in [(x0, 1 - ax), (x0 + 1, ax)]:
+ lut = tables[:, min(max(row, 0), gh - 1), min(max(col, 0), gw - 1)]
+ expected[..., y, x] += wy * wx * lut.gather(-1, index).squeeze(-1) / 255
+ return expected.to(img)
+
+
def _sync(device) -> None:
# MPS dispatches asynchronously, so a kernel error raised by the forward under test would
# otherwise surface inside an unrelated later test.
@@ -36,6 +61,60 @@ def _sync(device) -> None:
class TestEqualization(BaseTester):
+ @pytest.mark.parametrize("grid", [(2, 3), (1, 3), (2, 1), (1, 1)])
+ def test_clahe_interpolation_coordinates_5698(self, grid, device, dtype):
+ # Unequal axis lengths distinguish 1/4 from 1/8; constant LUTs isolate blending from histograms.
+ gh, gw = grid
+ img = torch.zeros(1, 1, gh * 4, gw * 8, device=device, dtype=dtype)
+ rows = torch.arange(gh, device=device, dtype=dtype)[:, None]
+ cols = torch.arange(gw, device=device, dtype=dtype)[None, :]
+ luts = (16 + 96 * rows + 48 * cols)[None, :, :, None, None].expand(1, gh, gw, 1, 256)
+ tiles = enhance.equalization._compute_interpolation_tiles(img, (4, 8))
+ out = enhance.equalization._compute_equalized_tiles(tiles, luts)
+ out = out.permute(0, 3, 1, 4, 2, 5).reshape_as(img)
+ # Every pixel: internal regions, both opposing borders of each axis, and all four corners.
+ self.assert_close(out, _clahe_interpolation_reference(img, luts, (4, 8)))
+ if gh > 1:
+ expected = torch.full_like(out[..., 2:6, 0], 96 / (4 * 255))
+ expected[..., -1] /= 2 # The last interval reaches the clamped border halfway through.
+ self.assert_close(out[..., 3:7, 0] - out[..., 2:6, 0], expected)
+ if gw > 1:
+ start = (gw - 2) * 8 + 4
+ expected = torch.full_like(out[..., 0, start : start + 8], 48 / (8 * 255))
+ expected[..., -1] /= 2
+ self.assert_close(out[..., 0, start + 1 : start + 9] - out[..., 0, start : start + 8], expected)
+
+ @pytest.mark.parametrize("dim", [-1, -2])
+ @pytest.mark.parametrize("slow_and_differentiable", [False, True])
+ @pytest.mark.parametrize("clip", [2.0, 40.0])
+ def test_clahe_flip_symmetry_5698(self, dim, slow_and_differentiable, clip, device, dtype):
+ # Unequal even tile sizes, without padding: reflection must reflect the tile centres and LUTs too.
+ # Dyadic values are exact in every dtype and avoid changing bins through input rounding.
+ y = torch.arange(12, device=device, dtype=dtype)[:, None]
+ x = torch.arange(24, device=device, dtype=dtype)[None, :]
+ img = ((2 * y + x) / 64)[None, None]
+ out = enhance.equalize_clahe(img, clip, (3, 3), slow_and_differentiable)
+ flipped = enhance.equalize_clahe(img.flip(dim), clip, (3, 3), slow_and_differentiable)
+ self.assert_close(flipped, out.flip(dim))
+
+ @pytest.mark.parametrize("slow_and_differentiable", [False, True])
+ @pytest.mark.parametrize("size", [(12, 24), (11, 23)])
+ def test_clahe_interpolation_paths_5698(self, size, slow_and_differentiable, device, dtype):
+ # Smooth dyadic inputs avoid an input quantization discrepancy; the second size needs padding.
+ y = torch.arange(size[0], device=device, dtype=dtype)[:, None]
+ x = torch.arange(size[1], device=device, dtype=dtype)[None, :]
+ img = ((2 * y + x) / 64)[None, None]
+ tiles, _ = _compute_tiles(img, (3, 3), True)
+ luts = enhance.equalization._compute_luts(tiles, clip=2.0, diff=slow_and_differentiable)
+ expected = _clahe_interpolation_reference(img, luts, tiles.shape[-2:])
+ out = enhance.equalize_clahe(img, 2.0, (3, 3), slow_and_differentiable)
+ self.assert_close(out, expected)
+ assert out.shape == img.shape
+ assert out.dtype == dtype
+ assert out.device == img.device
+ assert torch.isfinite(out).all()
+ assert ((out >= 0) & (out <= 1)).all()
+
def test_smoke(self, device, dtype):
C, H, W = 1, 10, 20
img = torch.rand(C, H, W, device=device, dtype=dtype)
@@ -319,24 +398,24 @@ class TestEqualization(BaseTester):
[
0.251,
0.502,
- 0.7490,
- 0.6667,
- 0.5007,
- 0.502,
- 0.7490,
- 0.4993,
- 0.5007,
- 0.251,
- 0.7490,
- 0.4993,
- 0.5007,
- 0.251,
- 0.502,
- 0.4993,
- 0.3333,
- 0.251,
- 0.502,
- 1.0000,
+ 0.6554,
+ 0.625,
+ 0.5319,
+ 0.5642,
+ 0.6554,
+ 0.4681,
+ 0.5319,
+ 0.3446,
+ 0.6554,
+ 0.4681,
+ 0.5319,
+ 0.3446,
+ 0.4392,
+ 0.4681,
+ 0.375,
+ 0.3446,
+ 0.5328,
+ 1.0,
]
]
],
@@ -362,24 +441,24 @@ class TestEqualization(BaseTester):
[
0.1255,
0.1882,
- 0.3137,
- 0.2928,
- 0.3137,
- 0.3765,
- 0.4392,
- 0.4392,
- 0.5007,
- 0.4392,
- 0.6235,
- 0.6248,
- 0.6235,
- 0.6235,
- 0.6863,
- 0.7490,
- 0.7699,
- 0.7490,
- 0.8745,
- 1.0000,
+ 0.2902,
+ 0.2824,
+ 0.3216,
+ 0.3922,
+ 0.4157,
+ 0.4314,
+ 0.5083,
+ 0.4623,
+ 0.6005,
+ 0.6172,
+ 0.6314,
+ 0.6471,
+ 0.6706,
+ 0.7412,
+ 0.7804,
+ 0.7725,
+ 0.8824,
+ 1.0,
]
]
],
@@ -390,26 +469,26 @@ class TestEqualization(BaseTester):
[
[
[
- 0.1250,
+ 0.125,
0.1877,
- 0.2792,
- 0.3111,
- 0.2892,
- 0.3227,
- 0.4302,
- 0.4745,
- 0.4560,
- 0.4620,
- 0.6495,
- 0.5952,
- 0.6414,
- 0.6254,
- 0.7329,
- 0.7803,
- 0.7306,
- 0.8181,
- 0.8471,
- 1.0000,
+ 0.2599,
+ 0.2995,
+ 0.3006,
+ 0.3492,
+ 0.4077,
+ 0.4644,
+ 0.4662,
+ 0.4893,
+ 0.6152,
+ 0.5853,
+ 0.6513,
+ 0.6471,
+ 0.7116,
+ 0.7706,
+ 0.7431,
+ 0.8321,
+ 0.8574,
+ 1.0,
]
]
],
@@ -488,11 +567,11 @@ class TestEqualization(BaseTester):
def test_clahe_non_square_grid(self, device, dtype):
# Pixel values are 0 and powers of two, exact in every dtype. With 4 x 4 tiles every interpolation weight
- # is a multiple of 1/3, so 9 * 255 * output is an integer. The expected integers come from an exact
+ # is a multiple of 1/8, so 64 * 255 * output is an integer. The expected integers come from an exact
# rational evaluation of CLAHE pixel by pixel, independent of this implementation's tile indexing; the
# reference is an exact-Fraction restatement of _compute_tiles/_compute_luts/_compute_equalized_tiles
# (tile size ceil(n/g) rounded up to even, trailing reflect pad, floor(v*256) histogram, clip and
- # redistribute, round(cumsum*255/P), axis blend weight (T-1-k)/(T-1)); the reference posted on #4628 is
+ # redistribute, round(cumsum*255/P), axis blend weight (T-k-0.5)/T); the reference posted on #4628 is
# evaluated here with round-to-even LUTs and the clipped residual spread over every (256 // residual)-th
# bin from bin 0, as OpenCV does.
codes = torch.tensor(
@@ -512,26 +591,31 @@ class TestEqualization(BaseTester):
img = levels[codes][None, None]
expected = torch.tensor(
[
- [288, 432, 1431, 1671, 288, 1575, 288, 1242, 912, 2295, 864, 1431],
- [432, 1719, 1719, 2295, 288, 1008, 1575, 576, 1197, 288, 576, 288],
- [720, 720, 1152, 1197, 1623, 576, 1008, 2295, 1197, 1152, 864, 864],
- [1383, 1152, 336, 1008, 1148, 720, 480, 2295, 320, 768, 1719, 1719],
- [528, 384, 384, 1543, 432, 2295, 2295, 656, 512, 576, 864, 864],
- [2295, 576, 432, 720, 2295, 720, 1152, 336, 480, 1152, 432, 1719],
- [2295, 2295, 576, 720, 2295, 1152, 1152, 576, 384, 864, 2295, 864],
- [576, 1431, 2295, 1479, 960, 1575, 1575, 960, 384, 432, 1431, 432],
+ [2048, 3072, 10304, 11840, 2048, 11328, 2048, 8792, 6528, 16320, 6144, 10176],
+ [3072, 12224, 12096, 16320, 2048, 7168, 11328, 4096, 8552, 2048, 4096, 2048],
+ [5248, 5248, 8265, 8411, 11488, 3760, 6816, 16320, 8411, 8265, 6144, 6144],
+ [9792, 8192, 2384, 7120, 8087, 5056, 3424, 16320, 2288, 5440, 12224, 12224],
+ [3712, 2688, 2608, 11040, 3152, 16320, 16320, 4800, 3616, 3936, 6144, 6144],
+ [16320, 3968, 2832, 5056, 16320, 5712, 8769, 2384, 3424, 7983, 2944, 12224],
+ [16320, 16320, 3840, 4992, 16320, 8312, 8440, 4224, 2688, 6016, 16320, 6144],
+ [4096, 10176, 16320, 10560, 6912, 11072, 11328, 6912, 2688, 2944, 10176, 3072],
],
dtype=torch.float64,
)
if dtype == torch.float16:
- # 9 * 255 / 16 = 143.4375 becomes 143.5 in float16, so its LUT entry rounds to 144, not 143.
- # Propagate that one-level difference through the exact rational interpolation weights.
- expected[0, 7] += 6
- expected[1, 8] += 3
- expected[2, [3, 8]] += 3
- expected[3, 0] += 3
- expected[3, 4] += 4
- expected = expected.div(9 * 255).to(dtype).to(device)[None, None]
+ # LUT values at 143.4375 round to 144 in float16, instead of 143.
+ # Propagate that one-level difference through the centre-anchored weights.
+ expected[0, 7] += 40
+ expected[1, 8] += 24
+ expected[2, 2] += 7
+ expected[2, 3] += 21
+ expected[2, 8] += 21
+ expected[2, 9] += 7
+ expected[3, 0] += 24
+ expected[3, 4] += 25
+ expected[5, 9] += 1
+ expected[6, 5] += 8
+ expected = expected.div(64 * 255).to(dtype).to(device)[None, None]
# Both orientations: the two border regions are indexed by the grid size of different axes.
self.assert_close(enhance.equalize_clahe(img, 40.0, (2, 3)), expected)
self.assert_close(enhance.equalize_clahe(img.transpose(-2, -1), 40.0, (3, 2)), expected.transpose(-2, -1))