Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(color): resample a ColorMap palette from its first colour to its last (#6557)

Saksham Sharma43ad6c214da1
diff --git a/changelog.d/6557.fixed.md b/changelog.d/6557.fixed.md
new file mode 100644
index 000000000..71025e8af
--- /dev/null
+++ b/changelog.d/6557.fixed.md
@@ -0,0 +1 @@
+`kornia.color.ColorMap` resamples its palette from the first colour of the base to the last, with entry `i` at `i / (num_colors - 1)` along it. With a `num_colors` other than the base length, it used to repeat the end colours when upsampling (`viridis` with 256 colours started and ended with two equal entries) and drop them when downsampling (`viridis` with 8 colours started and ended inside the map). The default 64-colour built-in maps are unchanged.
diff --git a/kornia/color/colormap.py b/kornia/color/colormap.py
index 32f8d4878..d1a5ee1ee 100644
--- a/kornia/color/colormap.py
+++ b/kornia/color/colormap.py
@@ -106,6 +106,9 @@ class ColorMap:
Convention:
colors has shape (3, num_colors) in RGB channel order. Built-in names are case-insensitive;
a custom base is an ordered list of RGB triples and is linearly resampled to num_colors.
+ For ``num_colors > 1``, entry ``i`` sits at ``i / (num_colors - 1)`` along the base, so the first and
+ last colours of the base are the first and last of the map. With ``num_colors == 1``, the map holds the
+ base's first colour.
Args:
base: A list of RGB colors to define a new custom colormap or the name of a built-in colormap as str or
@@ -119,15 +122,15 @@ class ColorMap:
Examples:
>>> ColorMap(base='viridis', num_colors=8).colors
- tensor([[0.2813, 0.2621, 0.2013, 0.1505, 0.1210, 0.2463, 0.5259, 0.8557],
- [0.0842, 0.2422, 0.3836, 0.5044, 0.6258, 0.7389, 0.8334, 0.8886],
- [0.4072, 0.5207, 0.5543, 0.5574, 0.5334, 0.4519, 0.2880, 0.0989]])
+ tensor([[0.2670, 0.2752, 0.2124, 0.1534, 0.1223, 0.2889, 0.6266, 0.9932],
+ [0.0049, 0.1949, 0.3597, 0.4970, 0.6332, 0.7584, 0.8546, 0.9062],
+ [0.3294, 0.4960, 0.5517, 0.5577, 0.5304, 0.4284, 0.2234, 0.1439]])
Create a color map from the first color (RGB with range[0-1]) to the last one with num_colors length.
>>> ColorMap(base=[[0., 0.5 , 1.0], [1., 0.5, 0.]], num_colors=8).colors
- tensor([[0.0000, 0.0000, 0.1250, 0.3750, 0.6250, 0.8750, 1.0000, 1.0000],
+ tensor([[0.0000, 0.1429, 0.2857, 0.4286, 0.5714, 0.7143, 0.8571, 1.0000],
[0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000],
- [1.0000, 1.0000, 0.8750, 0.6250, 0.3750, 0.1250, 0.0000, 0.0000]])
+ [1.0000, 0.8571, 0.7143, 0.5714, 0.4286, 0.2857, 0.1429, 0.0000]])
"""
@@ -174,7 +177,9 @@ class ColorMap:
"""
dtype = self._dtype if self._dtype is not None else torch.get_default_dtype()
tensor_colors = torch.tensor(list(base_colormap), dtype=dtype, device=self._device).T
- return interpolate(tensor_colors[None, ...], size=num_colors, mode="linear")[0, ...]
+ # align_corners=True keeps the base's end colours at the ends of the map (#6556): cell-centre sampling
+ # repeated them when upsampling and dropped them when downsampling.
+ return interpolate(tensor_colors[None, ...], size=num_colors, mode="linear", align_corners=True)[0, ...]
def __len__(self) -> int:
r"""Return the number of colors in the colormap.
diff --git a/tests/color/test_colormap.py b/tests/color/test_colormap.py
index fa11a3177..0fb934b8e 100644
--- a/tests/color/test_colormap.py
+++ b/tests/color/test_colormap.py
@@ -63,6 +63,29 @@ class TestColorMap(BaseTester):
assert actual.colors.dtype == dtype
self.assert_close(actual.colors, expected)
+ @pytest.mark.parametrize("num_colors", [2, 4, 5, 8, 256])
+ def test_ramp_resamples_evenly_6556(self, device, dtype, num_colors):
+ actual = ColorMap(base=[[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]], num_colors=num_colors, device=device, dtype=dtype)
+ ramp = torch.arange(num_colors, device=device, dtype=dtype) / (num_colors - 1)
+ self.assert_close(actual.colors, ramp.expand(3, -1))
+
+ @pytest.mark.parametrize("name", ["viridis", "jet"])
+ def test_one_color_map_holds_first_color_6556(self, device, dtype, name):
+ base = ColorMap(base=name, device=device, dtype=dtype).colors
+ actual = ColorMap(base=name, num_colors=1, device=device, dtype=dtype).colors
+ self.assert_close(actual, base[:, :1], atol=0.0, rtol=0.0)
+
+ @pytest.mark.parametrize("num_colors", [8, 63, 65, 256])
+ @pytest.mark.parametrize("name", ["viridis", "jet", "autumn"])
+ def test_resampled_map_keeps_end_colors_6556(self, device, dtype, name, num_colors):
+ base = ColorMap(base=name, device=device, dtype=dtype).colors
+ actual = ColorMap(base=name, num_colors=num_colors, device=device, dtype=dtype).colors
+ assert actual.shape == (3, num_colors)
+ self.assert_close(actual[:, 0], base[:, 0], atol=0.0, rtol=0.0)
+ self.assert_close(actual[:, -1], base[:, -1], atol=0.0, rtol=0.0)
+ assert not torch.equal(actual[:, 0], actual[:, 1])
+ assert not torch.equal(actual[:, -2], actual[:, -1])
+
class TestApplyColorMap(BaseTester):
def test_smoke(self, device, dtype):