| 1 |
""" |
| 2 |
Convert legacy rotation averaging file formats to a COLMAP database. |
| 3 |
|
| 4 |
This script provides a migration path from deprecated file-based input |
| 5 |
to database-based input for the rotation averaging CLI. |
| 6 |
|
| 7 |
Legacy file formats: |
| 8 |
- Relative poses: IMAGE_NAME_1 IMAGE_NAME_2 QW QX QY QZ TX TY TZ |
| 9 |
- Gravity priors: IMAGE_NAME GX GY GZ (optional) |
| 10 |
|
| 11 |
After conversion, use the database with: |
| 12 |
colmap rotation_averager --database_path database.db --output_path output/ |
| 13 |
|
| 14 |
Usage: |
| 15 |
python legacy_rotation_averaging_io.py \ |
| 16 |
--relpose_path relative_poses.txt \ |
| 17 |
--database_path database.db \ |
| 18 |
[--gravity_path gravity_priors.txt] |
| 19 |
""" |
| 20 |
|
| 21 |
import argparse |
| 22 |
from dataclasses import dataclass |
| 23 |
from pathlib import Path |
| 24 |
|
| 25 |
import numpy as np |
| 26 |
from numpy.typing import NDArray |
| 27 |
|
| 28 |
import pycolmap |
| 29 |
from pycolmap import logging |
| 30 |
|
| 31 |
|
| 32 |
@dataclass |
| 33 |
class RelativePose: |
| 34 |
"""Relative pose between two images.""" |
| 35 |
|
| 36 |
image_name1: str |
| 37 |
image_name2: str |
| 38 |
cam2_from_cam1: pycolmap.Rigid3d |
| 39 |
|
| 40 |
|
| 41 |
@dataclass |
| 42 |
class GravityPrior: |
| 43 |
"""Gravity prior for an image.""" |
| 44 |
|
| 45 |
image_name: str |
| 46 |
gravity: NDArray[np.float64] |
| 47 |
|
| 48 |
|
| 49 |
def read_relative_poses(file_path: Path | str) -> list[RelativePose]: |
| 50 |
"""Read relative poses from a file. |
| 51 |
|
| 52 |
Format: IMAGE_NAME_1 IMAGE_NAME_2 QW QX QY QZ TX TY TZ |
| 53 |
|
| 54 |
Args: |
| 55 |
file_path: Path to the relative poses file. |
| 56 |
|
| 57 |
Returns: |
| 58 |
List of RelativePose objects. |
| 59 |
""" |
| 60 |
file_path = Path(file_path) |
| 61 |
relative_poses = [] |
| 62 |
|
| 63 |
with open(file_path) as f: |
| 64 |
for line in f: |
| 65 |
line = line.strip() |
| 66 |
if not line or line.startswith("#"): |
| 67 |
continue |
| 68 |
|
| 69 |
parts = line.split() |
| 70 |
if len(parts) < 9: |
| 71 |
continue |
| 72 |
|
| 73 |
name1, name2 = parts[0], parts[1] |
| 74 |
qw, qx, qy, qz = map(float, parts[2:6]) |
| 75 |
tx, ty, tz = map(float, parts[6:9]) |
| 76 |
|
| 77 |
# pycolmap.Rotation3d takes quaternion in xyzw format |
| 78 |
rotation = pycolmap.Rotation3d(np.array([qx, qy, qz, qw])) |
| 79 |
translation = np.array([tx, ty, tz]) |
| 80 |
cam2_from_cam1 = pycolmap.Rigid3d(rotation, translation) |
| 81 |
|
| 82 |
relative_poses.append( |
| 83 |
RelativePose( |
| 84 |
image_name1=name1, |
| 85 |
image_name2=name2, |
| 86 |
cam2_from_cam1=cam2_from_cam1, |
| 87 |
) |
| 88 |
) |
| 89 |
|
| 90 |
return relative_poses |
| 91 |
|
| 92 |
|
| 93 |
def read_gravity_priors(file_path: Path | str) -> list[GravityPrior]: |
| 94 |
"""Read gravity priors from a file. |
| 95 |
|
| 96 |
Format: IMAGE_NAME GX GY GZ |
| 97 |
|
| 98 |
Args: |
| 99 |
file_path: Path to the gravity priors file. |
| 100 |
|
| 101 |
Returns: |
| 102 |
List of GravityPrior objects. |
| 103 |
""" |
| 104 |
file_path = Path(file_path) |
| 105 |
gravity_priors = [] |
| 106 |
|
| 107 |
with open(file_path) as f: |
| 108 |
for line in f: |
| 109 |
line = line.strip() |
| 110 |
if not line or line.startswith("#"): |
| 111 |
continue |
| 112 |
|
| 113 |
parts = line.split() |
| 114 |
if len(parts) < 4: |
| 115 |
continue |
| 116 |
|
| 117 |
name = parts[0] |
| 118 |
gx, gy, gz = map(float, parts[1:4]) |
| 119 |
|
| 120 |
gravity_priors.append( |
| 121 |
GravityPrior( |
| 122 |
image_name=name, |
| 123 |
gravity=np.array([gx, gy, gz], dtype=np.float64), |
| 124 |
) |
| 125 |
) |
| 126 |
|
| 127 |
return gravity_priors |
| 128 |
|
| 129 |
|
| 130 |
def get_image_names_from_relative_poses( |
| 131 |
relative_poses: list[RelativePose], |
| 132 |
) -> dict[str, int]: |
| 133 |
"""Extract unique image names from relative poses and assign IDs. |
| 134 |
|
| 135 |
Args: |
| 136 |
relative_poses: List of RelativePose objects. |
| 137 |
|
| 138 |
Returns: |
| 139 |
Dictionary mapping image names to IDs. |
| 140 |
""" |
| 141 |
image_names: dict[str, int] = {} |
| 142 |
next_id = 1 # COLMAP uses 1-based IDs |
| 143 |
|
| 144 |
for pose in relative_poses: |
| 145 |
for name in [pose.image_name1, pose.image_name2]: |
| 146 |
if name not in image_names: |
| 147 |
image_names[name] = next_id |
| 148 |
next_id += 1 |
| 149 |
|
| 150 |
return image_names |
| 151 |
|
| 152 |
|
| 153 |
def create_database_from_relative_poses( |
| 154 |
database_path: Path, |
| 155 |
relative_poses: list[RelativePose], |
| 156 |
gravity_priors: list[GravityPrior] | None = None, |
| 157 |
) -> dict[str, int]: |
| 158 |
"""Create a COLMAP database from relative poses and gravity priors. |
| 159 |
|
| 160 |
Args: |
| 161 |
database_path: Path to the output database. |
| 162 |
relative_poses: List of relative poses between image pairs. |
| 163 |
gravity_priors: Optional list of gravity priors for images. |
| 164 |
|
| 165 |
Returns: |
| 166 |
Dictionary mapping image names to their database image IDs. |
| 167 |
""" |
| 168 |
image_name_to_id = get_image_names_from_relative_poses(relative_poses) |
| 169 |
|
| 170 |
if database_path.exists(): |
| 171 |
database_path.unlink() |
| 172 |
|
| 173 |
# Use Reconstruction to create cameras, rigs, frames, and images |
| 174 |
# with the trivial rig/frame helper methods |
| 175 |
reconstruction = pycolmap.Reconstruction() |
| 176 |
for image_name, image_id in image_name_to_id.items(): |
| 177 |
camera_id = image_id |
| 178 |
|
| 179 |
# Create camera with trivial rig (rig_id = camera_id) |
| 180 |
camera = pycolmap.Camera.create( |
| 181 |
camera_id=camera_id, |
| 182 |
model=pycolmap.CameraModelId.SIMPLE_PINHOLE, |
| 183 |
focal_length=1.0, |
| 184 |
width=1, |
| 185 |
height=1, |
| 186 |
) |
| 187 |
reconstruction.add_camera_with_trivial_rig(camera) |
| 188 |
|
| 189 |
# Create image with trivial frame (frame_id = image_id) |
| 190 |
image = pycolmap.Image() |
| 191 |
image.image_id = image_id |
| 192 |
image.name = image_name |
| 193 |
image.camera_id = camera_id |
| 194 |
reconstruction.add_image_with_trivial_frame(image) |
| 195 |
|
| 196 |
# Write to database |
| 197 |
with pycolmap.Database.open(database_path) as db: |
| 198 |
for camera in reconstruction.cameras.values(): |
| 199 |
db.write_camera(camera, use_camera_id=True) |
| 200 |
|
| 201 |
for rig in reconstruction.rigs.values(): |
| 202 |
db.write_rig(rig, use_rig_id=True) |
| 203 |
|
| 204 |
for frame in reconstruction.frames.values(): |
| 205 |
db.write_frame(frame, use_frame_id=True) |
| 206 |
|
| 207 |
for image in reconstruction.images.values(): |
| 208 |
db.write_image(image, use_image_id=True) |
| 209 |
|
| 210 |
# Write two-view geometries with relative poses |
| 211 |
for rel_pose in relative_poses: |
| 212 |
id1 = image_name_to_id[rel_pose.image_name1] |
| 213 |
id2 = image_name_to_id[rel_pose.image_name2] |
| 214 |
|
| 215 |
two_view_geom = pycolmap.TwoViewGeometry() |
| 216 |
two_view_geom.config = ( |
| 217 |
pycolmap.TwoViewGeometryConfiguration.CALIBRATED |
| 218 |
) |
| 219 |
two_view_geom.cam2_from_cam1 = rel_pose.cam2_from_cam1 |
| 220 |
|
| 221 |
db.write_two_view_geometry(id1, id2, two_view_geom) |
| 222 |
|
| 223 |
# Write gravity priors if provided |
| 224 |
if gravity_priors: |
| 225 |
gravity_by_name = { |
| 226 |
gp.image_name: gp.gravity for gp in gravity_priors |
| 227 |
} |
| 228 |
for image in reconstruction.images.values(): |
| 229 |
if image.name in gravity_by_name: |
| 230 |
pose_prior = pycolmap.PosePrior() |
| 231 |
pose_prior.pose_prior_id = image.image_id |
| 232 |
pose_prior.corr_data_id = image.data_id |
| 233 |
pose_prior.gravity = gravity_by_name[image.name] |
| 234 |
db.write_pose_prior(pose_prior, use_pose_prior_id=True) |
| 235 |
|
| 236 |
return image_name_to_id |
| 237 |
|
| 238 |
|
| 239 |
def main(): |
| 240 |
parser = argparse.ArgumentParser( |
| 241 |
description="Convert rotation averaging files to database format" |
| 242 |
) |
| 243 |
parser.add_argument( |
| 244 |
"--relpose_path", |
| 245 |
type=Path, |
| 246 |
required=True, |
| 247 |
help="Path to relative poses file", |
| 248 |
) |
| 249 |
parser.add_argument( |
| 250 |
"--database_path", |
| 251 |
type=Path, |
| 252 |
required=True, |
| 253 |
help="Path for output database", |
| 254 |
) |
| 255 |
parser.add_argument( |
| 256 |
"--gravity_path", |
| 257 |
type=Path, |
| 258 |
default=None, |
| 259 |
help="Optional path to gravity priors file", |
| 260 |
) |
| 261 |
args = parser.parse_args() |
| 262 |
|
| 263 |
if not args.relpose_path.exists(): |
| 264 |
logging.error(f"Relative poses file not found: {args.relpose_path}") |
| 265 |
return 1 |
| 266 |
|
| 267 |
logging.info(f"Reading relative poses from {args.relpose_path}") |
| 268 |
relative_poses = read_relative_poses(args.relpose_path) |
| 269 |
logging.info(f"Loaded {len(relative_poses)} relative poses") |
| 270 |
|
| 271 |
gravity_priors = None |
| 272 |
if args.gravity_path is not None and args.gravity_path.exists(): |
| 273 |
logging.info(f"Reading gravity priors from {args.gravity_path}") |
| 274 |
gravity_priors = read_gravity_priors(args.gravity_path) |
| 275 |
logging.info(f"Loaded {len(gravity_priors)} gravity priors") |
| 276 |
|
| 277 |
logging.info(f"Creating database at {args.database_path}") |
| 278 |
image_name_to_id = create_database_from_relative_poses( |
| 279 |
args.database_path, relative_poses, gravity_priors |
| 280 |
) |
| 281 |
logging.info(f"Database created with {len(image_name_to_id)} images") |
| 282 |
|
| 283 |
logging.info( |
| 284 |
f"Conversion complete. Use the database with:\n" |
| 285 |
f" colmap rotation_averager --database_path {args.database_path} " |
| 286 |
f"--output_path <output_path>" |
| 287 |
) |
| 288 |
return 0 |
| 289 |
|
| 290 |
|
| 291 |
if __name__ == "__main__": |
| 292 |
exit(main()) |