Colormap#
Render a single-channel image with a color map, e.g. to visualize depth, heat or edge maps.
Functions#
- kornia.color.apply_colormap(input_tensor, colormap)[source]#
Color each channel of an image with a colormap.
out = apply_colormap(input_tensor, colormap)replaces each value of each channel with a color ofcolormap.
- Convention:
Accepts a rank-3 (C, H, W) or rank-4 (B, C, H, W) tensor and returns (B, 3*C, H, W), with B = 1 for rank-3 input; input channel c becomes output channels 3c to 3c + 2. Integer inputs use [0, 255]. Floating inputs use [0, 1] or [0, 255] independently for each sample and channel, based on that channel’s maximum. A value x in [0, 1] selects entry floor(x * N) of the N palette colors, and x = 1 selects the last, as in matplotlib, so every color covers an equal share of the range. Values below or above the range select the first or the last color.
- Parameters:
- Return type:
- Returns:
Color image with shape
(B, 3 * C, H, W)and the dtype of the palette.
See also
ApplyColorMap: the module form.ColorMap: builds the palette.
Example
>>> input_tensor = torch.tensor([[[0, 1, 2], [15, 25, 33], [128, 158, 188]]]) >>> colormap = ColorMap(base=ColorMapType.autumn) >>> apply_colormap(input_tensor, colormap) tensor([[[[1.0000, 1.0000, 1.0000], [1.0000, 1.0000, 1.0000], [1.0000, 1.0000, 1.0000]], [[0.0000, 0.0000, 0.0000], [0.0476, 0.0952, 0.1270], [0.5079, 0.6190, 0.7460]], [[0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000]]]])
Modules#
- class kornia.color.ApplyColorMap(colormap)[source]#
Color each channel of an image with a colormap (module form).
cmap = ApplyColorMap(colormap)stores the palette as the buffercolors, andout = cmap(input_tensor)isapply_colormap()with it.
- Convention:
See the convention block of
apply_colormap().
- Parameters:
colormap (
ColorMap) – palette to apply.
- Shape:
input_tensor:
(C, H, W)or(B, C, H, W)output:
(B, 3 * C, H, W)
See also
apply_colormap(): the one-call form.ColorMap: builds the palette.
Example
>>> input_tensor = torch.tensor([[[0, 1, 2], [15, 25, 33], [128, 158, 188]]]) >>> colormap = ColorMap(base=ColorMapType.autumn) >>> ApplyColorMap(colormap=colormap)(input_tensor) tensor([[[[1.0000, 1.0000, 1.0000], [1.0000, 1.0000, 1.0000], [1.0000, 1.0000, 1.0000]], [[0.0000, 0.0000, 0.0000], [0.0476, 0.0952, 0.1270], [0.5079, 0.6190, 0.7460]], [[0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000]]]])
- class kornia.color.ColorMap(base, num_colors=64, device=None, dtype=None)[source]#
Color palette for
apply_colormap().cmap = ColorMap(base, num_colors)builds a built-in colormap, or one from your own RGB triples, withnum_colorsentries and stores it ascmap.colors.- 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 in \([0, 1]\) and is linearly resampled to num_colors. For
num_colors > 1, entryisits ati / (num_colors - 1)along the base, so the first and last colors of the base are the first and last of the map. Withnum_colors == 1, the map holds the base’s first color.
- Parameters:
base (
Union[list[List[float]],str,ColorMapType]) – list of RGB triples, or a built-in colormap as aColorMapTypeor its name.num_colors (
int, optional) – number of palette entries. Default:64device (
Optional[device], optional) – device of the palette. Default:Nonedtype (
Optional[dtype], optional) – dtype of the palette. Defaults totorch.get_default_dtype(). Default:None
See also
ColorMapType: the built-in colormaps.apply_colormap(): colors an image with the palette.ApplyColorMap: the module form ofapply_colormap().
Examples
>>> ColorMap(base='viridis', num_colors=8).colors 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]])
>>> ColorMap(base=[[0., 0.5 , 1.0], [1., 0.5, 0.]], num_colors=8).colors 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, 0.8571, 0.7143, 0.5714, 0.4286, 0.2857, 0.1429, 0.0000]])
Color maps available:
- class kornia.color.ColorMapType(value)[source]#
Names of the built-in colormaps.
ColorMapType.viridis, or its name'viridis', selects a built-in colormap inColorMap.list()returns all names.
See also
ColorMap: builds the palette of a member.
- autumn = 1#
- bone = 2#
- jet = 3#
- winter = 4#
- rainbow = 5#
- ocean = 6#
- summer = 7#
- spring = 8#
- cool = 9#
- hsv = 10#
- brg = 11#
- pink = 12#
- hot = 13#
- plasma = 14#
- viridis = 15#
- cividis = 16#
- twilight = 17#
- turbo = 18#
- seismic = 19#