Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(contrib): keep histogram_matching output in the template range (#5794)

Rızgar Ozan30ed313eed2d
diff --git a/changelog.d/5794.breaking.md b/changelog.d/5794.breaking.md
new file mode 100644
index 000000000..521379575
--- /dev/null
+++ b/changelog.d/5794.breaking.md
@@ -0,0 +1 @@
+`kornia.contrib.histogram_matching` no longer extrapolates below the template minimum. When the template's darkest value covers a larger fraction of its pixels than the source's darkest value does (for example a template with clipped blacks), the source pixels whose quantile is below that fraction used to map below the template minimum, to negative intensities for a template whose minimum is 0; they now map to the template minimum, as in `skimage.exposure.match_histograms`. A constant template used to return NaN everywhere and now returns that constant.
diff --git a/kornia/contrib/histogram_matching.py b/kornia/contrib/histogram_matching.py
index af4cd406b..82aeae05f 100644
--- a/kornia/contrib/histogram_matching.py
+++ b/kornia/contrib/histogram_matching.py
@@ -55,7 +55,11 @@ def histogram_matching(source: torch.Tensor, template: torch.Tensor) -> torch.Te
t_quantiles = t_quantiles / t_quantiles[-1]
# interpolate linearly to find the pixel values in the template image
- # that correspond most closely to the quantiles in the source image
+ # that correspond most closely to the quantiles in the source image.
+ # Anchor the template CDF at quantile 0 so a source quantile below the template's lowest one maps to the
+ # template minimum (as np.interp does) instead of being extrapolated below it.
+ t_quantiles = torch.cat([t_quantiles.new_zeros(1), t_quantiles])
+ t_values = torch.cat([t_values[:1], t_values])
interp_t_values = interp(s_quantiles, t_quantiles, t_values)
return interp_t_values[bin_idx].reshape(oldshape)
diff --git a/tests/contrib/test_hist_match.py b/tests/contrib/test_hist_match.py
index 3880525d2..2698dcb07 100644
--- a/tests/contrib/test_hist_match.py
+++ b/tests/contrib/test_hist_match.py
@@ -53,6 +53,35 @@ class TestHistMatch(BaseTester):
assert exp.shape == out.shape
self.assert_close(out, exp, rtol=1e-4, atol=1e-4)
+ def test_histmatch_stays_in_template_range(self, device, dtype):
+ # The lowest source quantile (1/4) is below the template's (3/4): it maps to the template minimum, as np.interp
+ # does in the referenced implementation, instead of being extrapolated below it.
+ src = torch.tensor([0.0, 1.0, 2.0, 3.0], device=device, dtype=dtype)
+ dst = torch.tensor([10.0, 10.0, 10.0, 20.0], device=device, dtype=dtype)
+ out = kornia.contrib.histogram_matching(src, dst)
+ exp = torch.tensor([10.0, 10.0, 10.0, 20.0], device=device, dtype=dtype)
+ self.assert_close(out, exp)
+
+ def test_histmatch_constant_template(self, device, dtype):
+ src = torch.tensor([0.0, 1.0, 2.0, 3.0], device=device, dtype=dtype)
+ dst = torch.full((5,), 7.0, device=device, dtype=dtype)
+ out = kornia.contrib.histogram_matching(src, dst)
+ self.assert_close(out, torch.full_like(src, 7.0))
+
+ def test_histmatch_clipped_black_template(self, device, dtype):
+ # An 8-bit template with crushed shadows: 60 of its 256 pixels are 0. The darkest source pixels map to 0, as in
+ # skimage.exposure.match_histograms (60 zeros); they used to be extrapolated below it (57 negative outputs).
+ yy, xx = torch.meshgrid(
+ torch.linspace(0, 1, 16, device=device, dtype=dtype),
+ torch.linspace(0, 1, 16, device=device, dtype=dtype),
+ indexing="ij",
+ )
+ src = ((0.1 + 0.5 * xx + 0.3 * yy) * 255).round() / 255
+ dst = ((1.2 * xx + 0.3 * yy - 0.4).clamp(0, 1) * 255).round() / 255
+ out = kornia.contrib.histogram_matching(src, dst)
+ assert out.min() == 0
+ assert int((out == 0).sum()) == 60
+
@pytest.mark.skip(reason="not differentiable now.")
def test_grad(self, device):
B, C, H, W = 1, 3, 32, 32