mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-25 18:39:55 +08:00
1 parent
e9bba85b08
commit
b1ddb9e695
6 files changed
+211
-116
No files matched your search
@@ -14,6 +14,7 @@
|
||||
|
||||
""" Helper functions for visualizing outputs """
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Literal, Optional
|
||||
|
||||
import matplotlib
|
||||
@@ -25,13 +26,25 @@ from nerfstudio.utils import colors
|
||||
Colormaps = Literal["default", "turbo", "viridis", "magma", "inferno", "cividis"]
|
||||
|
||||
|
||||
@dataclass
|
||||
class ColormapOptions:
|
||||
"""Options for colormap"""
|
||||
|
||||
colormap: Colormaps = "default"
|
||||
""" The colormap to use """
|
||||
normalize: bool = False
|
||||
""" Whether to normalize the input tensor image """
|
||||
colormap_min: float = 0
|
||||
""" Minimum value for the output colormap """
|
||||
colormap_max: float = 1
|
||||
""" Maximum value for the output colormap """
|
||||
invert: bool = False
|
||||
""" Whether to invert the output colormap """
|
||||
|
||||
|
||||
def apply_colormap(
|
||||
image: TensorType["bs":..., 1],
|
||||
colormap: Colormaps = "turbo",
|
||||
normalize: bool = False,
|
||||
colormap_min: float = 0,
|
||||
colormap_max: float = 1,
|
||||
invert: bool = False,
|
||||
colormap_options: ColormapOptions = ColormapOptions(),
|
||||
eps: float = 1e-9,
|
||||
) -> TensorType["bs":..., "rgb":3]:
|
||||
"""
|
||||
@@ -39,11 +52,6 @@ def apply_colormap(
|
||||
|
||||
Args:
|
||||
image: Input tensor image.
|
||||
colormap: Colormap to apply.
|
||||
normalize: Whether to normalize the input tensor image.
|
||||
colormap_min: Minimum value for the output colormap.
|
||||
colormap_max: Maximum value for the output colormap.
|
||||
invert: Whether to invert the output colormap.
|
||||
eps: Epsilon value for numerical stability.
|
||||
|
||||
Returns:
|
||||
@@ -57,14 +65,16 @@ def apply_colormap(
|
||||
# rendering depth outputs
|
||||
if image.shape[-1] == 1 and torch.is_floating_point(image):
|
||||
output = image
|
||||
if normalize:
|
||||
if colormap_options.normalize:
|
||||
output = output - torch.min(output)
|
||||
output = output / (torch.max(output) + eps)
|
||||
output = output * (colormap_max - colormap_min) + colormap_min
|
||||
output = (
|
||||
output * (colormap_options.colormap_max - colormap_options.colormap_min) + colormap_options.colormap_min
|
||||
)
|
||||
output = torch.clip(output, 0, 1)
|
||||
if invert:
|
||||
if colormap_options.invert:
|
||||
output = 1 - output
|
||||
return apply_float_colormap(output, colormap=colormap)
|
||||
return apply_float_colormap(output, colormap=colormap_options.colormap)
|
||||
|
||||
# rendering boolean outputs
|
||||
if image.dtype == torch.bool:
|
||||
@@ -104,7 +114,7 @@ def apply_depth_colormap(
|
||||
accumulation: Optional[TensorType["bs":..., 1]] = None,
|
||||
near_plane: Optional[float] = None,
|
||||
far_plane: Optional[float] = None,
|
||||
colormap: Colormaps = "turbo",
|
||||
colormap_options: ColormapOptions = ColormapOptions(),
|
||||
) -> TensorType["bs":..., "rgb":3]:
|
||||
"""Converts a depth image to color for easier analysis.
|
||||
|
||||
@@ -126,7 +136,7 @@ def apply_depth_colormap(
|
||||
depth = torch.clip(depth, 0, 1)
|
||||
# depth = torch.nan_to_num(depth, nan=0.0) # TODO(ethan): remove this
|
||||
|
||||
colored_image = apply_colormap(depth, colormap=colormap)
|
||||
colored_image = apply_colormap(depth, colormap_options=colormap_options)
|
||||
|
||||
if accumulation is not None:
|
||||
colored_image = colored_image * accumulation + (1 - accumulation)
|
||||
|
||||
Reference in new issue
Block a user