π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): select last colormap color at upper endpoint (#5339)
NiteeshGowda726c28d4c3ea76
diff --git a/changelog.d/5339.breaking.md b/changelog.d/5339.breaking.mdnew file mode 100644index 000000000..00d374eed--- /dev/null+++ b/changelog.d/5339.breaking.md@@ -0,0 +1,6 @@+`apply_colormap` and `ApplyColorMap` map the top of the input range to the last palette color: `1.0` for unit-range+input and `255` for integer or byte-range input. Previously, with three or more palette colors, the top of the range+selected the second-to-last color, so no input in the documented range reached the last one: the palette+`[0, 0.25, 0.75, 1]` mapped `1.0` to `0.75`, and now maps it to `1`. Every other input keeps its color, so with+`num_colors=256` a `uint8` value `v` now selects palette color `v` for every `v` (`255` used to select color `254`).+(#5307)diff --git a/kornia/color/colormap.py b/kornia/color/colormap.pyindex 3dc048a8e..63fa029ba 100644--- a/kornia/color/colormap.py+++ b/kornia/color/colormap.py@@ -250,7 +250,9 @@ def apply_colormap(input_tensor: torch.Tensor, colormap: ColorMap) -> torch.Tenscolors = colormap.colors.permute(1, 0)num_colors, channels_cmap = colors.shapekeys = torch.linspace(0.0, 1.0, num_colors - 1, device=input_tensor.device, dtype=input_tensor.dtype)- indices = torch.bucketize(input_tensor, keys).unsqueeze(-1).expand(-1, -1, -1, 3)+ indices = torch.bucketize(input_tensor, keys)+ indices = torch.where(input_tensor == 1.0, num_colors - 1, indices)+ indices = indices.unsqueeze(-1).expand(-1, -1, -1, 3)output = torch.gather(colors.expand(B, C, -1, -1), 2, indices)# (B, C, H*W, channels_cmap) -> (B, C*channels_cmap, H, W)diff --git a/tests/color/test_colormap.py b/tests/color/test_colormap.pyindex efc3728b0..4d853ae39 100644--- a/tests/color/test_colormap.py+++ b/tests/color/test_colormap.py@@ -71,6 +71,29 @@ class TestApplyColorMap(BaseTester):self.assert_close(actual, expected_tensor)+ def test_upper_endpoint_selects_last_color(self, device, dtype):+ cm = ColorMap(+ base=[[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]],+ num_colors=4,+ device=device,+ dtype=dtype,+ )+ input_tensor = torch.ones(1, 1, 1, device=device, dtype=dtype)++ actual = apply_colormap(input_tensor, cm)+ expected = cm.colors[..., -1].reshape(1, 3, 1, 1)++ self.assert_close(actual, expected)++ def test_uint8_selects_color_by_value(self, device, dtype):+ # With 256 colors every byte value v selects palette color v, including 255 (the last color).+ cm = ColorMap(base=[[i / 255.0] * 3 for i in range(256)], num_colors=256, device=device, dtype=dtype)+ input_tensor = torch.arange(256, device=device).to(torch.uint8).reshape(1, 1, 1, 256)++ actual = apply_colormap(input_tensor, cm)++ self.assert_close(actual, cm.colors.reshape(1, 3, 1, 256), rtol=0.0, atol=0.0)+def test_exception(self, device, dtype):cm = ColorMap(base="autumn", device=device, dtype=dtype)with pytest.raises(Exception):