Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): avoid Otsu bin overflow on extreme ranges (#5479)

Puttala Jeevan Kumar050ac77f7cc1
diff --git a/changelog.d/5479.fixed.md b/changelog.d/5479.fixed.md
new file mode 100644
index 000000000..7f953c4cd
--- /dev/null
+++ b/changelog.d/5479.fixed.md
@@ -0,0 +1 @@
+`otsu_threshold` on the default path now finds the right split for float32 and bfloat16 planes whose range times `nbins` exceeds the float32 maximum (above about 1.3e36 for 256 bins). Such planes used to get a wrong threshold and mask, or a NaN threshold once the range itself overflowed.
diff --git a/kornia/filters/otsu_thresholding.py b/kornia/filters/otsu_thresholding.py
index a254adf2d..08408dae8 100644
--- a/kornia/filters/otsu_thresholding.py
+++ b/kornia/filters/otsu_thresholding.py
@@ -63,6 +63,17 @@ class OtsuThreshold(torch.nn.Module):
min_values = values.amin(dim=1, keepdim=True)
max_values = values.amax(dim=1, keepdim=True)
widths = max_values - min_values
+ # Preserve histc's operation order on safe planes, including its rounding at bin boundaries. If the
+ # range or its product with bins overflows, use bounded units as in `_upper_edge` before subtracting:
+ # finite extrema then lie in [-1, 1], so no infinite bin coordinate reaches the integer conversion.
+ scale = torch.where(
+ (widths * bins).isfinite(),
+ torch.ones_like(widths),
+ torch.maximum(min_values.abs(), max_values.abs()).clamp_min(torch.finfo(edge_dtype).tiny),
+ )
+ values = values / scale
+ min_values, max_values = min_values / scale, max_values / scale
+ widths = max_values - min_values
safe_widths = torch.where(widths > 0, widths, torch.ones_like(widths))
indices = ((values - min_values) * bins / safe_widths).to(torch.int64).clamp(0, bins - 1)
histograms = values.new_zeros((values.shape[0], bins)).scatter_add(1, indices, torch.ones_like(values))
@@ -76,7 +87,7 @@ class OtsuThreshold(torch.nn.Module):
torch.addcmul(min_values, positions, steps),
torch.addcmul(max_values, positions - bins, steps),
)
- return histograms / histograms.sum(dim=1, keepdim=True), bin_edges, indices
+ return histograms / histograms.sum(dim=1, keepdim=True), bin_edges * scale, indices
@staticmethod
def _kde_histogram(coords: torch.Tensor, bins: int, bandwidth: float) -> torch.Tensor:
diff --git a/tests/filters/test_otsu_thresholding.py b/tests/filters/test_otsu_thresholding.py
index 26cb569ef..3c07f868a 100644
--- a/tests/filters/test_otsu_thresholding.py
+++ b/tests/filters/test_otsu_thresholding.py
@@ -374,6 +374,63 @@ class TestOtsuThreshold(BaseTester):
expected_edges = torch.linspace(low, high, nbins + 1, device=device, dtype=stats_dtype)
self.assert_close(edges[i], expected_edges)
+ @pytest.mark.parametrize("nbins", [2, 256])
+ @pytest.mark.parametrize("scale", [1.0, 1e35, 6e35, 7e35, 1e36, 1.6e38, 3.4e38])
+ def test_large_finite_range_split_5471(self, scale, nbins, device, dtype):
+ if dtype in (torch.float16, torch.bfloat16):
+ pytest.skip("issue fixtures require float32 or float64; dtype extrema are covered separately")
+ values = (
+ [-3.4e38, -1e38, 0.0, 2e38, 3.4e38]
+ if scale == 3.4e38
+ else [-scale, -0.3125 * scale, 0.0, 0.625 * scale, scale]
+ )
+ image = torch.tensor([values], device=device, dtype=dtype)
+ mask, threshold = otsu_threshold(image, nbins=nbins, return_mask=True)
+ assert threshold.isfinite().all()
+ # At 256 bins the optimal split is after the zero pixel's bin (128), whose upper edge is scale / 128.
+ # With two bins, zero belongs to the foreground, so the membership guard puts the threshold below zero.
+ assert mask.tolist() == [[False, False, nbins == 2, True, True]]
+ if nbins == 256:
+ self.assert_close(threshold / scale, image.new_tensor([1 / 128]), rtol=2e-5, atol=0)
+ else:
+ assert image[0, 1] <= threshold.item() < 0
+ self.assert_close(mask, image > threshold)
+
+ @pytest.mark.parametrize("nbins", [2, 17, 256])
+ def test_mixed_ordinary_and_extreme_planes_5471(self, nbins, device, dtype):
+ ordinary = torch.tensor([[-1.0, -0.75, -0.5, 0.5, 0.75, 1.0]], device=device, dtype=dtype)
+ extreme = ordinary * torch.finfo(dtype).max
+ constant = torch.full_like(ordinary, torch.finfo(dtype).max)
+ image = torch.stack([ordinary, extreme, constant]).requires_grad_(True)
+ mask, threshold = otsu_threshold(image, nbins=nbins, return_mask=True)
+ assert threshold.dtype == dtype
+ assert threshold.device == image.device
+ assert threshold.isfinite().all()
+ assert not threshold.requires_grad
+ assert mask.tolist() == [[[False, False, False, True, True, True]]] * 2 + [[[False] * 6]]
+ expected_mask, expected_threshold = otsu_threshold(ordinary, nbins=nbins, return_mask=True)
+ self.assert_close(mask[0], expected_mask)
+ self.assert_close(threshold[:1], expected_threshold, rtol=0, atol=0)
+ # Bin floor(nbins / 4) holds the largest background pixel (-scale / 2).
+ edge = -1 + 2 * (nbins // 4 + 1) / nbins
+ self.assert_close(threshold[1:2] / torch.finfo(dtype).max, image.new_tensor([edge]))
+ self.assert_close(threshold[2:3], constant.flatten()[:1], rtol=0, atol=0)
+ out, _ = otsu_threshold(image, nbins=nbins)
+ (grad,) = torch.autograd.grad(out.sum(), image)
+ self.assert_close(grad, mask.to(dtype))
+
+ @pytest.mark.parametrize("sign", [1.0, -1.0])
+ @pytest.mark.parametrize("nbins", [2, 256])
+ def test_one_sided_extreme_range_5471(self, sign, nbins, device, dtype):
+ # One extreme is zero: the bounded unit must be the larger magnitude, not the minimum's or the maximum's alone.
+ unit = sign * torch.tensor([[0.0, 0.125, 0.25, 0.75, 1.0]], device=device, dtype=dtype)
+ image = unit * torch.finfo(dtype).max
+ mask, threshold = otsu_threshold(image, nbins=nbins, return_mask=True)
+ expected_mask, expected_threshold = otsu_threshold(unit, nbins=nbins, return_mask=True)
+ assert threshold.isfinite().all()
+ assert mask.tolist() == expected_mask.tolist()
+ self.assert_close(threshold / torch.finfo(dtype).max, expected_threshold)
+
def test_integer_histogram_matches_histc_with_offsets_5425(self, device):
image = torch.stack([torch.arange(-128, 129), torch.arange(1000, 1257)]).to(device=device, dtype=torch.int16)
histograms, _, _ = OtsuThreshold._OtsuThreshold__histogram(image, 17)