| 1 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are met: |
| 6 |
# |
| 7 |
# * Redistributions of source code must retain the above copyright |
| 8 |
# notice, this list of conditions and the following disclaimer. |
| 9 |
# |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# |
| 14 |
# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of |
| 15 |
# its contributors may be used to endorse or promote products derived |
| 16 |
# from this software without specific prior written permission. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
# POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
import argparse |
| 31 |
|
| 32 |
import numpy as np |
| 33 |
import numpy.typing as npt |
| 34 |
import open3d |
| 35 |
|
| 36 |
import pycolmap |
| 37 |
|
| 38 |
|
| 39 |
class Model: |
| 40 |
def __init__(self) -> None: |
| 41 |
self.reconstruction: pycolmap.Reconstruction |
| 42 |
self.visualizer: open3d.visualization.Visualizer |
| 43 |
|
| 44 |
def read_model(self, path: str) -> None: |
| 45 |
self.reconstruction = pycolmap.Reconstruction(path) |
| 46 |
|
| 47 |
def add_points( |
| 48 |
self, min_track_len: int = 3, remove_statistical_outlier: bool = True |
| 49 |
) -> None: |
| 50 |
pcd = open3d.geometry.PointCloud() |
| 51 |
|
| 52 |
xyz = [] |
| 53 |
rgb = [] |
| 54 |
for point in self.reconstruction.points3D.values(): |
| 55 |
if point.track.length() < min_track_len: |
| 56 |
continue |
| 57 |
xyz.append(point.xyz) |
| 58 |
rgb.append(point.color / 255) |
| 59 |
|
| 60 |
pcd.points = open3d.utility.Vector3dVector(xyz) |
| 61 |
pcd.colors = open3d.utility.Vector3dVector(rgb) |
| 62 |
|
| 63 |
# remove obvious outliers |
| 64 |
if remove_statistical_outlier: |
| 65 |
[pcd, _] = pcd.remove_statistical_outlier( |
| 66 |
nb_neighbors=20, std_ratio=2.0 |
| 67 |
) |
| 68 |
|
| 69 |
# open3d.visualization.draw_geometries([pcd]) |
| 70 |
self.visualizer.add_geometry(pcd) |
| 71 |
self.visualizer.poll_events() |
| 72 |
self.visualizer.update_renderer() |
| 73 |
|
| 74 |
def add_cameras(self, scale: float = 1) -> None: |
| 75 |
frustums = [] |
| 76 |
for img in self.reconstruction.images.values(): |
| 77 |
# extrinsics |
| 78 |
world_from_cam = img.cam_from_world().inverse() |
| 79 |
R = world_from_cam.rotation.matrix() |
| 80 |
t = world_from_cam.translation |
| 81 |
|
| 82 |
# intrinsics |
| 83 |
cam = img.camera |
| 84 |
if cam.model in ( |
| 85 |
pycolmap.CameraModelId.SIMPLE_PINHOLE, |
| 86 |
pycolmap.CameraModelId.SIMPLE_RADIAL, |
| 87 |
pycolmap.CameraModelId.RADIAL, |
| 88 |
): |
| 89 |
fx = fy = cam.params[0] |
| 90 |
cx = cam.params[1] |
| 91 |
cy = cam.params[2] |
| 92 |
elif cam.model in ( |
| 93 |
pycolmap.CameraModelId.PINHOLE, |
| 94 |
pycolmap.CameraModelId.OPENCV, |
| 95 |
pycolmap.CameraModelId.OPENCV_FISHEYE, |
| 96 |
pycolmap.CameraModelId.FULL_OPENCV, |
| 97 |
): |
| 98 |
fx = cam.params[0] |
| 99 |
fy = cam.params[1] |
| 100 |
cx = cam.params[2] |
| 101 |
cy = cam.params[3] |
| 102 |
else: |
| 103 |
raise Exception("Camera model not supported") |
| 104 |
|
| 105 |
# intrinsics |
| 106 |
K = np.identity(3) |
| 107 |
K[0, 0] = fx |
| 108 |
K[1, 1] = fy |
| 109 |
K[0, 2] = cx |
| 110 |
K[1, 2] = cy |
| 111 |
|
| 112 |
# create axis, plane and pyramid geometries that will be drawn |
| 113 |
cam_model = draw_camera(K, R, t, cam.width, cam.height, scale) |
| 114 |
frustums.extend(cam_model) |
| 115 |
|
| 116 |
# add geometries to visualizer |
| 117 |
for i in frustums: |
| 118 |
self.visualizer.add_geometry(i) |
| 119 |
|
| 120 |
def create_window(self) -> None: |
| 121 |
self.visualizer = open3d.visualization.Visualizer() |
| 122 |
self.visualizer.create_window() |
| 123 |
|
| 124 |
def show(self) -> None: |
| 125 |
self.visualizer.poll_events() |
| 126 |
self.visualizer.update_renderer() |
| 127 |
self.visualizer.run() |
| 128 |
self.visualizer.destroy_window() |
| 129 |
|
| 130 |
|
| 131 |
def draw_camera( |
| 132 |
K: npt.NDArray[np.float64], |
| 133 |
R: npt.NDArray[np.float64], |
| 134 |
t: npt.NDArray[np.float64], |
| 135 |
w: int, |
| 136 |
h: int, |
| 137 |
scale: float = 1, |
| 138 |
color: list[float] | None = None, |
| 139 |
) -> list[open3d.geometry.Geometry]: |
| 140 |
"""Create axis, plane and pyramed geometries in Open3D format. |
| 141 |
:param K: calibration matrix (camera intrinsics) |
| 142 |
:param R: rotation matrix |
| 143 |
:param t: translation |
| 144 |
:param w: image width |
| 145 |
:param h: image height |
| 146 |
:param scale: camera model scale |
| 147 |
:param color: color of the image plane and pyramid lines |
| 148 |
:return: camera model geometries (axis, plane and pyramid) |
| 149 |
""" |
| 150 |
|
| 151 |
if color is None: |
| 152 |
color = [0.8, 0.2, 0.8] |
| 153 |
|
| 154 |
# intrinsics |
| 155 |
K = K.copy() / scale |
| 156 |
Kinv = np.linalg.inv(K) |
| 157 |
|
| 158 |
# 4x4 transformation |
| 159 |
T = np.column_stack((R, t)) |
| 160 |
T = np.vstack((T, (0, 0, 0, 1))) |
| 161 |
|
| 162 |
# axis |
| 163 |
axis = open3d.geometry.TriangleMesh.create_coordinate_frame( |
| 164 |
size=0.5 * scale |
| 165 |
) |
| 166 |
axis.transform(T) |
| 167 |
|
| 168 |
# points in pixel |
| 169 |
points_pixel = [ |
| 170 |
[0, 0, 0], |
| 171 |
[0, 0, 1], |
| 172 |
[w, 0, 1], |
| 173 |
[0, h, 1], |
| 174 |
[w, h, 1], |
| 175 |
] |
| 176 |
|
| 177 |
# pixel to camera coordinate system |
| 178 |
points = [Kinv @ p for p in points_pixel] |
| 179 |
|
| 180 |
# image plane |
| 181 |
width = abs(points[1][0]) + abs(points[3][0]) |
| 182 |
height = abs(points[1][1]) + abs(points[3][1]) |
| 183 |
plane = open3d.geometry.TriangleMesh.create_box(width, height, depth=1e-6) |
| 184 |
plane.paint_uniform_color(color) |
| 185 |
plane.translate([points[1][0], points[1][1], scale]) |
| 186 |
plane.transform(T) |
| 187 |
|
| 188 |
# pyramid |
| 189 |
points_in_world = [(R @ p + t) for p in points] |
| 190 |
lines = [ |
| 191 |
[0, 1], |
| 192 |
[0, 2], |
| 193 |
[0, 3], |
| 194 |
[0, 4], |
| 195 |
] |
| 196 |
colors = [color for i in range(len(lines))] |
| 197 |
line_set = open3d.geometry.LineSet( |
| 198 |
points=open3d.utility.Vector3dVector(points_in_world), |
| 199 |
lines=open3d.utility.Vector2iVector(lines), |
| 200 |
) |
| 201 |
line_set.colors = open3d.utility.Vector3dVector(colors) |
| 202 |
|
| 203 |
# return as list in Open3D format |
| 204 |
return [axis, plane, line_set] |
| 205 |
|
| 206 |
|
| 207 |
def parse_args() -> argparse.Namespace: |
| 208 |
parser = argparse.ArgumentParser( |
| 209 |
description="Visualize COLMAP binary and text models" |
| 210 |
) |
| 211 |
parser.add_argument( |
| 212 |
"--input_model", required=True, help="path to input model folder" |
| 213 |
) |
| 214 |
args = parser.parse_args() |
| 215 |
return args |
| 216 |
|
| 217 |
|
| 218 |
def main() -> None: |
| 219 |
args = parse_args() |
| 220 |
|
| 221 |
# read COLMAP model |
| 222 |
model = Model() |
| 223 |
model.read_model(args.input_model) |
| 224 |
|
| 225 |
print("num_cameras:", model.reconstruction.num_cameras()) |
| 226 |
print("num_images:", model.reconstruction.num_images()) |
| 227 |
print("num_points3D:", model.reconstruction.num_points3D()) |
| 228 |
|
| 229 |
# display using Open3D visualization tools |
| 230 |
model.create_window() |
| 231 |
model.add_points() |
| 232 |
model.add_cameras(scale=0.25) |
| 233 |
model.show() |
| 234 |
|
| 235 |
|
| 236 |
if __name__ == "__main__": |
| 237 |
main() |