Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(enhance): preserve CLAHE clipping equivariance (#4637)

NiteeshGowda72c643312a3e66
diff --git a/changelog.d/4637.fixed.md b/changelog.d/4637.fixed.md
new file mode 100644
index 000000000..8e54a6a07
--- /dev/null
+++ b/changelog.d/4637.fixed.md
@@ -0,0 +1 @@
+Fix `equalize_clahe` transpose equivariance in the slow differentiable path when histogram clipping is enabled. The fast histogram path preserves its existing redistribution behavior, including for marginally out-of-range input values.
diff --git a/kornia/enhance/equalization.py b/kornia/enhance/equalization.py
index 67e79b7c4..520f0b96a 100644
--- a/kornia/enhance/equalization.py
+++ b/kornia/enhance/equalization.py
@@ -198,8 +198,11 @@ def _compute_luts(
if clip > 0.0:
max_val: float = max(clip * pixels // num_bins, 1)
+ if diff:
+ clipped: torch.Tensor = torch.relu(histos - max_val).sum(1)
histos.clamp_(max=max_val)
- clipped: torch.Tensor = pixels - histos.sum(1)
+ if not diff:
+ clipped = pixels - histos.sum(1)
residual: torch.Tensor = torch.remainder(clipped, num_bins)
redist: torch.Tensor = (clipped - residual).div(num_bins)
histos += redist[None].transpose(0, 1)
diff --git a/tests/enhance/test_equalization.py b/tests/enhance/test_equalization.py
index 86de5feb5..53cc21534 100644
--- a/tests/enhance/test_equalization.py
+++ b/tests/enhance/test_equalization.py
@@ -102,6 +102,15 @@ class TestEqualization(BaseTester):
with pytest.raises(TypeError):
enhance.equalize_clahe([1, 2, 3])
+ def test_clahe_preserves_out_of_range_fast_path(self):
+ x = torch.linspace(0, 1, 64).reshape(1, 1, 8, 8)
+ x.view(-1)[0] = 1.0000001
+
+ y = enhance.equalize_clahe(x, 40.0, (1, 1))
+
+ assert y.sum().item() == pytest.approx(32.87843322753906)
+ assert y.max().item() == pytest.approx(1.0)
+
@pytest.mark.parametrize("grid_size", [(1, 1), (1, 2), (2, 1), (2, 2)])
def test_single_tile_on_the_differentiable_path(self, grid_size, device, dtype):
# A (1, 1) grid is global equalization with the clip limit applied, and the slow path raised an
@@ -387,11 +396,28 @@ class TestEqualization(BaseTester):
@pytest.mark.parametrize("grid_size", [(1, 2), (2, 3), (3, 4), (2, 6), (6, 2)])
def test_clahe_non_square_grid_transpose(self, grid_size, slow_and_differentiable, device, dtype):
# Transposing the image and the grid transposes the output. Every grid tiles 12 x 24 without padding.
- # The slow path runs unclipped: below float64 its clip step can turn a rounding residue of the soft
- # histogram sum into a whole count, which moves the output under transposition on square grids too.
- clip_limit = 0.0 if slow_and_differentiable else 40.0
+ # Clipping must preserve transpose equivariance for both the fast and slow differentiable paths.
torch.manual_seed(2531)
img = torch.rand(1, 2, 12, 24, device=device, dtype=dtype)
- out = enhance.equalize_clahe(img, clip_limit, grid_size, slow_and_differentiable)
- out_t = enhance.equalize_clahe(img.transpose(-2, -1), clip_limit, grid_size[::-1], slow_and_differentiable)
+ out = enhance.equalize_clahe(img, 40.0, grid_size, slow_and_differentiable)
+ out_t = enhance.equalize_clahe(img.transpose(-2, -1), 40.0, grid_size[::-1], slow_and_differentiable)
self.assert_close(out_t, out.transpose(-2, -1))
+
+ def test_clahe_transpose_equivariance_4632(self):
+ torch.manual_seed(28)
+ img = torch.rand(1, 1, 12, 12, dtype=torch.float32)
+
+ output = enhance.equalize_clahe(
+ img,
+ clip_limit=40.0,
+ grid_size=(2, 2),
+ slow_and_differentiable=True,
+ )
+ output_transposed = enhance.equalize_clahe(
+ img.transpose(-2, -1),
+ clip_limit=40.0,
+ grid_size=(2, 2),
+ slow_and_differentiable=True,
+ ).transpose(-2, -1)
+
+ self.assert_close(output, output_transposed, low_tolerance=True)