Skip to content

modosaic.depth.visualization

modosaic.depth.visualization

DepthVisualizationService

Convert depth arrays into image visualizations.

depth_to_heatmap staticmethod

depth_to_heatmap(depth, size=None, colormap=cv2.COLORMAP_TURBO)

Convert a depth map to a color heatmap.

Parameters:

Name Type Description Default
depth ndarray

Depth map.

required
size tuple[int, int] | None

Optional output (width, height).

None
colormap int

OpenCV color-map identifier.

COLORMAP_TURBO

Returns:

Type Description
Image

RGB PIL heatmap image.

Source code in modosaic/depth/visualization.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@staticmethod
def depth_to_heatmap(
        depth: np.ndarray,
        size: tuple[int, int] | None = None,
        colormap: int = cv2.COLORMAP_TURBO,
) -> Image.Image:
    """Convert a depth map to a color heatmap.

    Args:
        depth: Depth map.
        size: Optional output `(width, height)`.
        colormap: OpenCV color-map identifier.

    Returns:
        RGB PIL heatmap image.
    """
    depth_uint8, _, _ = DepthVisualizationService.normalize_to_uint8(depth)
    if size is not None:
        depth_uint8 = cv2.resize(depth_uint8, size, interpolation=cv2.INTER_CUBIC)

    heatmap_bgr = cv2.applyColorMap(depth_uint8, colormap)
    heatmap_rgb = cv2.cvtColor(heatmap_bgr, cv2.COLOR_BGR2RGB)
    return Image.fromarray(heatmap_rgb)

depth_to_image staticmethod

depth_to_image(depth, size=None)

Convert a depth map to a grayscale PIL image.

Parameters:

Name Type Description Default
depth ndarray

Depth map.

required
size tuple[int, int] | None

Optional output (width, height).

None

Returns:

Type Description
Image

PIL image visualization.

Source code in modosaic/depth/visualization.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@staticmethod
def depth_to_image(
        depth: np.ndarray,
        size: tuple[int, int] | None = None,
) -> Image.Image:
    """Convert a depth map to a grayscale PIL image.

    Args:
        depth: Depth map.
        size: Optional output `(width, height)`.

    Returns:
        PIL image visualization.
    """
    depth_uint8, _, _ = DepthVisualizationService.normalize_to_uint8(depth)
    depth_image = Image.fromarray(depth_uint8)
    if size is not None:
        depth_image = depth_image.resize(size, Image.Resampling.BICUBIC)
    return depth_image

normalize_to_uint8 staticmethod

normalize_to_uint8(depth, eps=1e-08)

Normalize a depth map to an 8-bit grayscale array.

Parameters:

Name Type Description Default
depth ndarray

Depth map.

required
eps float

Minimum denominator used to avoid division by zero.

1e-08

Returns:

Type Description
tuple[ndarray, float, float]

Normalized uint8 array, original minimum, and original maximum.

Source code in modosaic/depth/visualization.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@staticmethod
def normalize_to_uint8(
        depth: np.ndarray,
        eps: float = 1e-8,
) -> tuple[np.ndarray, float, float]:
    """Normalize a depth map to an 8-bit grayscale array.

    Args:
        depth: Depth map.
        eps: Minimum denominator used to avoid division by zero.

    Returns:
        Normalized uint8 array, original minimum, and original maximum.
    """
    depth = depth.astype(np.float32)
    d_min = float(np.nanmin(depth))
    d_max = float(np.nanmax(depth))
    denom = (d_max - d_min) if (d_max - d_min) > eps else eps
    depth_uint8 = ((depth - d_min) / denom * 255.0).clip(0, 255).astype(np.uint8)
    return depth_uint8, d_min, d_max

overlay_depth staticmethod

overlay_depth(image, depth, alpha=0.55)

Blend a depth preview over an RGB image.

Parameters:

Name Type Description Default
image Image

Source image.

required
depth ndarray

Depth map.

required
alpha float

Blend factor for the depth image.

0.55

Returns:

Type Description
Image

Blended RGB image.

Source code in modosaic/depth/visualization.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@staticmethod
def overlay_depth(
        image: Image.Image,
        depth: np.ndarray,
        alpha: float = 0.55,
) -> Image.Image:
    """Blend a depth preview over an RGB image.

    Args:
        image: Source image.
        depth: Depth map.
        alpha: Blend factor for the depth image.

    Returns:
        Blended RGB image.
    """
    base = image.convert("RGB")
    depth_image = DepthVisualizationService.depth_to_image(
        depth,
        size=base.size,
    ).convert("RGB")
    return Image.blend(base, depth_image, alpha=float(np.clip(alpha, 0.0, 1.0)))