COLMAP

Visualize COLMAP sparse reconstruction outputs. To get demo data, see ../assets/download_assets.sh.

Features:

  • COLMAP sparse reconstruction file parsing

  • Camera frustum visualization with viser.SceneApi.add_camera_frustum()

  • 3D point cloud display from structure-from-motion

  • Interactive camera and point visibility controls

Note

This example requires external assets. To download them, run:

cd /path/to/viser/examples/assets
./download_assets.sh

Source: examples/04_demos/01_colmap_visualizer.py

COLMAP

Code

  1import random
  2import time
  3from pathlib import Path
  4from typing import List
  5
  6import imageio.v3 as iio
  7import numpy as np
  8import tyro
  9from tqdm.auto import tqdm
 10
 11import viser
 12import viser.transforms as vtf
 13from viser.extras.colmap import (
 14    read_cameras_binary,
 15    read_images_binary,
 16    read_points3d_binary,
 17)
 18
 19
 20def main(
 21    colmap_path: Path = Path(__file__).parent / "../assets/colmap_garden/sparse/0",
 22    images_path: Path = Path(__file__).parent / "../assets/colmap_garden/images_8",
 23    downsample_factor: int = 2,
 24    reorient_scene: bool = True,
 25) -> None:
 26    server = viser.ViserServer()
 27    server.gui.configure_theme(titlebar_content=None, control_layout="collapsible")
 28
 29    # Load the colmap info.
 30    cameras = read_cameras_binary(colmap_path / "cameras.bin")
 31    images = read_images_binary(colmap_path / "images.bin")
 32    points3d = read_points3d_binary(colmap_path / "points3D.bin")
 33
 34    points = np.array([points3d[p_id].xyz for p_id in points3d])
 35    colors = np.array([points3d[p_id].rgb for p_id in points3d])
 36
 37    gui_reset_up = server.gui.add_button(
 38        "Reset up direction",
 39        hint="Set the camera control 'up' direction to the current camera's 'up'.",
 40    )
 41
 42    # Let's rotate the scene so the average camera direction is pointing up.
 43    if reorient_scene:
 44        average_up = (
 45            vtf.SO3(np.array([img.qvec for img in images.values()]))
 46            @ np.array([0.0, -1.0, 0.0])  # -y is up in the local frame!
 47        ).mean(axis=0)
 48        average_up /= np.linalg.norm(average_up)
 49        server.scene.set_up_direction((average_up[0], average_up[1], average_up[2]))
 50
 51    @gui_reset_up.on_click
 52    def _(event: viser.GuiEvent) -> None:
 53        client = event.client
 54        assert client is not None
 55        client.camera.up_direction = vtf.SO3(client.camera.wxyz) @ np.array(
 56            [0.0, -1.0, 0.0]
 57        )
 58
 59    gui_points = server.gui.add_slider(
 60        "Max points",
 61        min=1,
 62        max=len(points3d),
 63        step=1,
 64        initial_value=min(len(points3d), 50_000),
 65    )
 66    gui_frames = server.gui.add_slider(
 67        "Max frames",
 68        min=1,
 69        max=len(images),
 70        step=1,
 71        initial_value=min(len(images), 50),
 72    )
 73    gui_point_size = server.gui.add_slider(
 74        "Point size", min=0.01, max=0.1, step=0.001, initial_value=0.02
 75    )
 76
 77    point_mask = np.random.choice(points.shape[0], gui_points.value, replace=False)
 78    point_cloud = server.scene.add_point_cloud(
 79        name="/colmap/pcd",
 80        points=points[point_mask],
 81        colors=colors[point_mask],
 82        point_size=gui_point_size.value,
 83    )
 84    frames: List[viser.FrameHandle] = []
 85
 86    def visualize_frames() -> None:
 87
 88        # Remove existing image frames.
 89        for frame in frames:
 90            frame.remove()
 91        frames.clear()
 92
 93        # Interpret the images and cameras.
 94        img_ids = [im.id for im in images.values()]
 95        random.shuffle(img_ids)
 96        img_ids = sorted(img_ids[: gui_frames.value])
 97
 98        for img_id in tqdm(img_ids):
 99            img = images[img_id]
100            cam = cameras[img.camera_id]
101
102            # Skip images that don't exist.
103            image_filename = images_path / img.name
104            if not image_filename.exists():
105                continue
106
107            T_world_camera = vtf.SE3.from_rotation_and_translation(
108                vtf.SO3(img.qvec), img.tvec
109            ).inverse()
110            frame = server.scene.add_frame(
111                f"/colmap/frame_{img_id}",
112                wxyz=T_world_camera.rotation().wxyz,
113                position=T_world_camera.translation(),
114                axes_length=0.1,
115                axes_radius=0.005,
116            )
117            frames.append(frame)
118
119            # For pinhole cameras, cam.params will be (fx, fy, cx, cy).
120            if cam.model != "PINHOLE":
121                print(f"Expected pinhole camera, but got {cam.model}")
122
123            H, W = cam.height, cam.width
124            fy = cam.params[1]
125            image = iio.imread(image_filename)
126            image = image[::downsample_factor, ::downsample_factor]
127            frustum = server.scene.add_camera_frustum(
128                f"/colmap/frame_{img_id}/frustum",
129                fov=2 * np.arctan2(H / 2, fy),
130                aspect=W / H,
131                scale=0.15,
132                image=image,
133            )
134
135            @frustum.on_click
136            def _(_, frame=frame) -> None:
137                for client in server.get_clients().values():
138                    client.camera.wxyz = frame.wxyz
139                    client.camera.position = frame.position
140
141    need_update = True
142
143    @gui_points.on_update
144    def _(_) -> None:
145        point_mask = np.random.choice(points.shape[0], gui_points.value, replace=False)
146        with server.atomic():
147            point_cloud.points = points[point_mask]
148            point_cloud.colors = colors[point_mask]
149
150    @gui_frames.on_update
151    def _(_) -> None:
152        nonlocal need_update
153        need_update = True
154
155    @gui_point_size.on_update
156    def _(_) -> None:
157        point_cloud.point_size = gui_point_size.value
158
159    while True:
160        if need_update:
161            need_update = False
162            visualize_frames()
163
164        time.sleep(1e-3)
165
166
167if __name__ == "__main__":
168    tyro.cli(main)