Convert single channel to a color img. Args: img (torch.Tensor): (..., 1) float32 single channel image. colormap (str): Colormap for img. Returns: (..., 3) colored img with colors in [0, 1].
(img: torch.Tensor, colormap: str = "turbo")
| 174 | |
| 175 | |
| 176 | def apply_float_colormap(img: torch.Tensor, colormap: str = "turbo") -> torch.Tensor: |
| 177 | """Convert single channel to a color img. |
| 178 | |
| 179 | Args: |
| 180 | img (torch.Tensor): (..., 1) float32 single channel image. |
| 181 | colormap (str): Colormap for img. |
| 182 | |
| 183 | Returns: |
| 184 | (..., 3) colored img with colors in [0, 1]. |
| 185 | """ |
| 186 | img = torch.nan_to_num(img, 0) |
| 187 | if colormap == "gray": |
| 188 | return img.repeat(1, 1, 3) |
| 189 | img_long = (img * 255).long() |
| 190 | img_long_min = torch.min(img_long) |
| 191 | img_long_max = torch.max(img_long) |
| 192 | assert img_long_min >= 0, f"the min value is {img_long_min}" |
| 193 | assert img_long_max <= 255, f"the max value is {img_long_max}" |
| 194 | return torch.tensor( |
| 195 | colormaps[colormap].colors, # type: ignore |
| 196 | device=img.device, |
| 197 | )[img_long[..., 0]] |
| 198 | |
| 199 | |
| 200 | def apply_depth_colormap( |
no outgoing calls
no test coverage detected
searching dependent graphs…