Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix bucket off-by-one-error (#2582)

Sean Aubin59e3dada5823
diff --git a/kornia/color/colormap.py b/kornia/color/colormap.py
index 0d402eb29..676ac30cb 100644
--- a/kornia/color/colormap.py
+++ b/kornia/color/colormap.py
@@ -159,7 +159,7 @@ def apply_colormap(input_tensor: Tensor, colormap: ColorMap) -> Tensor:
elif len(input_tensor.shape) == 3 and input_tensor.shape[0] == 1: # if (1 X H x W)
input_tensor = input_tensor[0, ...] # (H x W)
- keys = torch.arange(0, len(colormap), dtype=input_tensor.dtype, device=input_tensor.device) # (num_colors)
+ keys = torch.arange(0, len(colormap) - 1, dtype=input_tensor.dtype, device=input_tensor.device) # (num_colors)
index = torch.bucketize(input_tensor, keys) # shape equals <input_tensor>: (B x H x W) or (H x W)
diff --git a/test/color/test_colormap.py b/test/color/test_colormap.py
index a7d62eb05..48f227fff 100644
--- a/test/color/test_colormap.py
+++ b/test/color/test_colormap.py
@@ -44,6 +44,23 @@ class TestApplyColorMap(BaseTester):
self.assert_close(actual, expected_tensor)
+ def test_eye(self, device, dtype):
+ input_tensor = torch.stack(
+ [torch.eye(2, dtype=dtype, device=device) * 255, torch.eye(2, dtype=dtype, device=device) * 150]
+ ).view(2, -1, 2, 2)
+
+ expected_tensor = tensor(
+ [
+ [[[1.0, 1.0], [1.0, 1.0]], [[1.0, 0.0], [0.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]]],
+ [[[1.0, 1.0], [1.0, 1.0]], [[1.0, 0.0], [0.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]]],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ actual = apply_colormap(input_tensor, AUTUMN(device=device, dtype=dtype))
+ self.assert_close(actual, expected_tensor)
+
def test_exception(self, device, dtype):
cm = AUTUMN(device=device, dtype=dtype)
with pytest.raises(TypeError):