π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): spread the CLAHE clipping residual over the histogram like OpenCV (#5655)
εΊζ’η7d8240754964
diff --git a/changelog.d/5655.breaking.md b/changelog.d/5655.breaking.mdnew file mode 100644index 000000000..4672f5240--- /dev/null+++ b/changelog.d/5655.breaking.md@@ -0,0 +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). Over 64 x 80 to 512 x 512 images with an 8 x 8 grid and clip limits 2, 4 and 40, the mean signed difference from OpenCV is now -0.4 to -0.6 gray levels in the fast and per-sample paths, which floor each lookup-table entry where OpenCV rounds it (#5659), and within 0.1 in the differentiable path.diff --git a/kornia/enhance/equalization.py b/kornia/enhance/equalization.pyindex 5b471fd2f..94d43c21a 100644--- a/kornia/enhance/equalization.py+++ b/kornia/enhance/equalization.py@@ -176,6 +176,31 @@ def _tiles_histc(tiles: torch.Tensor, bins: int) -> torch.Tensor:return counts[:, :bins].to(tiles.dtype)+def _spread_residual(residual: torch.Tensor, num_bins: int) -> torch.Tensor:+ r"""Spread the residual of the clipped pixels over the histogram bins the way OpenCV does.++ ``residual`` is the part of the clipped mass of each tile that is left after every bin took the same whole+ share, so it is below ``num_bins``. OpenCV adds one count to every ``step``-th bin starting at bin 0, with+ ``step = max(num_bins // residual, 1)``, until the residual is spent. Writing it into the first ``residual``+ bins instead piles it onto the darkest levels and lifts the whole lookup table, which brightens small tiles+ by tens of levels.++ Args:+ residual: residual per tile, shape :math:`(T,)`, below ``num_bins``.+ num_bins: number of histogram bins.++ Returns:+ the counts to add, 0 or 1 per bin, shape :math:`(T, num_bins)` in the dtype of ``residual``.++ """+ bins: torch.Tensor = torch.arange(num_bins, device=residual.device)+ step: torch.Tensor = torch.div(num_bins, residual.clamp(min=1), rounding_mode="floor").clamp(min=1).unsqueeze(1)+ hit: torch.Tensor = (torch.remainder(bins, step) == 0) & (+ torch.div(bins, step, rounding_mode="floor") < residual.unsqueeze(1)+ )+ return hit.to(residual.dtype)++def _compute_luts(tiles_x_im: torch.Tensor, num_bins: int = 256, clip: Union[float, torch.Tensor] = 40.0, diff: bool = False) -> torch.Tensor:@@ -219,7 +244,7 @@ def _compute_luts(clipped_tensor = torch.relu(histos - max_vals).sum(1) if diff else pixels - limited.sum(1)residual_tensor = torch.remainder(clipped_tensor, num_bins)limited = limited + ((clipped_tensor - residual_tensor) / num_bins).unsqueeze(1)- limited = limited + (torch.arange(num_bins, device=histos.device) < residual_tensor.unsqueeze(1))+ limited = limited + _spread_residual(residual_tensor, num_bins)enabled = (clip > 0).to(device=histos.device).view(b, 1).expand(b, gh * gw * c).reshape(-1, 1)histos = torch.where(enabled, limited, histos)elif clip > 0.0:@@ -232,10 +257,7 @@ def _compute_luts(residual: torch.Tensor = torch.remainder(clipped, num_bins)redist: torch.Tensor = (clipped - residual).div(num_bins)histos += redist[None].transpose(0, 1)- # trick to avoid using a loop to assign the residual- v_range: torch.Tensor = torch.arange(num_bins, device=histos.device)- mat_range: torch.Tensor = v_range.repeat(histos.shape[0], 1)- histos += mat_range < residual[None].transpose(0, 1)+ histos += _spread_residual(residual, num_bins)lut_scale: float = (num_bins - 1) / pixelsluts: torch.Tensor = torch.cumsum(histos, 1) * lut_scalediff --git a/tests/enhance/test_equalization.py b/tests/enhance/test_equalization.pyindex d52b8aecc..6d6f7cfa1 100644--- a/tests/enhance/test_equalization.py+++ b/tests/enhance/test_equalization.py@@ -353,28 +353,31 @@ class TestEqualization(BaseTester):res_diff = enhance.equalize_clahe(img, clip_limit=clip_limit, grid_size=grid_size, slow_and_differentiable=True)# NOTE: for next versions we need to improve the computation of the LUT# and test with a better image+ # Each 4 x 4 tile holds 4 columns of one value each, so with the clip limit of 1 count per bin the 12+ # clipped counts are spread over every 21st bin, as in OpenCV, and the gradient keeps its range; piling+ # them into the first 12 bins mapped every pixel but the first above 0.81.expected = torch.tensor([[[0.1216,- 0.8745,- 0.9373,- 0.9163,- 0.8745,- 0.8745,- 0.9373,- 0.8745,- 0.8745,- 0.8118,- 0.9373,- 0.8745,- 0.8745,- 0.8118,- 0.8745,- 0.8745,- 0.8327,- 0.8118,+ 0.1843,+ 0.3098,+ 0.2889,+ 0.3098,+ 0.3725,+ 0.4353,+ 0.4353,+ 0.4980,+ 0.4353,+ 0.6235,+ 0.6235,+ 0.6235,+ 0.6235,+ 0.6863,+ 0.7490,+ 0.7699,+ 0.7490,0.8745,1.0000,]@@ -388,24 +391,24 @@ class TestEqualization(BaseTester):[[0.1250,- 0.8752,- 0.9042,- 0.9167,- 0.8401,- 0.8852,- 0.9302,- 0.9120,- 0.8750,- 0.8370,- 0.9620,- 0.9077,- 0.8750,- 0.8754,- 0.9204,- 0.9167,- 0.8370,- 0.8806,- 0.9096,+ 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,]]@@ -416,13 +419,50 @@ class TestEqualization(BaseTester):self.assert_close(res[..., 0, :], expected, low_tolerance=True)self.assert_close(res_diff[..., 0, :], exp_diff, low_tolerance=True)+ @pytest.mark.parametrize("clip_as_tensor", [False, True])+ def test_clahe_residual_spread(self, clip_as_tensor, device, dtype):+ # One 2 x 4 tile of 8 pixels, 4 at level 0 and 4 at level 100, clip limit 40: the per-bin limit is+ # max(40 * 8 // 256, 1) = 1, so 6 counts are clipped. OpenCV spreads a residual below the bin count over+ # every (256 // 6) = 42nd bin from bin 0 (bins 0, 42, 84, 126, 168, 210), which leaves 2 counts at or+ # below bin 0 and 5 at or below bin 100: the levels map to floor(2 * 255 / 8) = 63 and+ # floor(5 * 255 / 8) = 159. Piling the residual into bins 0..5 instead gave 8 counts at bin 100 and+ # mapped it to 255.+ img = torch.tensor([[0.0] * 4, [100.0] * 4], device=device, dtype=dtype).div(255.0)[None, None]+ clip = torch.tensor([40.0], device=device, dtype=dtype) if clip_as_tensor else 40.0+ out = enhance.equalization._equalize_clahe(img, clip, (1, 1), False)+ expected = torch.tensor([[63.0] * 4, [159.0] * 4], device=device, dtype=dtype).div(255.0)[None, None]+ self.assert_close(out, expected)++ @pytest.mark.parametrize("clip_as_tensor", [False, True])+ def test_clahe_residual_spread_after_the_whole_share(self, clip_as_tensor, device, dtype):+ # One 32 x 32 tile of 1024 pixels: 342 in bin 0, 341 in bin 100 and 341 in bin 200. Clip limit 40 caps every+ # bin at 40 * 1024 // 256 = 160, so 544 counts are clipped. Every bin takes the whole share 544 // 256 = 2, and+ # the residual 32 goes, as in OpenCV, to every 256 // 32 = 8th bin from bin 0. The LUT is+ # floor(cumsum * 255 / 1024): bin 100 maps to floor(535 * 255 / 1024) = 133 and bin 200 to+ # floor(908 * 255 / 1024) = 226, where writing the residual into bins 0..31 gave 137 and 227.+ if dtype in (torch.float16, torch.bfloat16):+ pytest.skip("the cumulative counts of a 1024-pixel tile are not exact below float32")+ values = torch.cat([torch.zeros(342), torch.full((341,), 100 / 256), torch.full((341,), 200 / 256)])+ tiles = values.to(device, dtype).view(1, 1, 1, 1, 32, 32)+ clip = torch.tensor([40.0], device=device, dtype=dtype) if clip_as_tensor else 40.0+ luts = enhance.equalization._compute_luts(tiles, clip=clip)+ hist = torch.full((256,), 2.0, dtype=torch.float64)+ hist[::8] += 1+ hist[[0, 100, 200]] += 160+ expected = (hist.cumsum(0) * 255 / 1024).floor()+ assert expected[100] == 133+ assert expected[200] == 226+ self.assert_close(luts.view(256), expected.to(device, dtype))+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# 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, floor(cumsum*255/P), axis blend weight (T-1-k)/(T-1)); it is posted in full on #4628.+ # redistribute, floor(cumsum*255/P), axis blend weight (T-1-k)/(T-1)); it is posted in full on #4628, with+ # the residual of the clipped counts now spread over every (256 // residual)-th bin from bin 0 as OpenCV+ # does, instead of over the first ``residual`` bins.codes = torch.tensor([[1, 2, 6, 7, 1, 7, 1, 5, 4, 8, 4, 6],@@ -440,14 +480,14 @@ class TestEqualization(BaseTester):img = levels[codes][None, None]expected = torch.tensor([- [423, 567, 1575, 1815, 423, 1719, 423, 1431, 1143, 2295, 1143, 1719],- [567, 1863, 1863, 2295, 279, 1143, 1719, 759, 1431, 279, 855, 423],- [855, 855, 1287, 1335, 1767, 711, 1143, 2295, 1431, 1431, 1143, 1143],- [1623, 1431, 327, 1255, 1367, 903, 663, 2295, 455, 1143, 1911, 1911],- [855, 519, 375, 1751, 695, 2295, 2295, 967, 839, 951, 1335, 1335],- [2295, 999, 423, 1191, 2295, 999, 1431, 327, 855, 1719, 423, 2007],- [2295, 2295, 999, 1191, 2295, 1431, 1431, 951, 519, 1431, 2295, 1431],- [999, 1719, 2295, 1719, 1335, 1719, 1719, 1335, 375, 423, 1863, 423],+ [279, 423, 1431, 1671, 279, 1575, 279, 1239, 903, 2295, 855, 1431],+ [423, 1719, 1719, 2295, 279, 999, 1575, 567, 1191, 279, 567, 279],+ [711, 711, 1143, 1191, 1623, 567, 999, 2295, 1191, 1143, 855, 855],+ [1383, 1143, 327, 999, 1143, 711, 471, 2295, 311, 759, 1719, 1719],+ [519, 375, 375, 1543, 423, 2295, 2295, 647, 503, 567, 855, 855],+ [2295, 567, 423, 711, 2295, 711, 1143, 327, 471, 1143, 423, 1719],+ [2295, 2295, 567, 711, 2295, 1143, 1143, 567, 375, 855, 2295, 855],+ [567, 1431, 2295, 1479, 951, 1575, 1575, 951, 375, 423, 1431, 423],],dtype=torch.float64,)