pycolmap: Python Bindings for COLMAP

4.0.3 · active · verified Fri Apr 17

pycolmap provides robust Python bindings for COLMAP, an open-source Structure-from-Motion (SfM) and Multi-View Stereo (MVS) pipeline. It allows programmatic control over COLMAP's feature extraction, matching, and 3D reconstruction functionalities directly from Python. The current version is 4.0.3, with releases typically synchronized with major COLMAP C++ releases, focusing on performance, new features, and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the high-level `pycolmap.Sfm` class to run a full Structure-from-Motion pipeline. It creates dummy images and attempts a reconstruction, which is expected to fail with only two random images, but illustrates the basic API usage.

import os
import pycolmap
import numpy as np
from PIL import Image

# Create a dummy image directory
image_dir = "pycolmap_quickstart_images"
output_dir = "pycolmap_quickstart_output"

os.makedirs(image_dir, exist_ok=True)
os.makedirs(output_dir, exist_ok=True)

# Create two dummy images (COLMAP typically needs more and with overlap)
img_size = (100, 100)
for i in range(2):
    dummy_image = Image.fromarray(np.random.randint(0, 255, img_size + (3,), dtype=np.uint8))
    dummy_image.save(os.path.join(image_dir, f"image_{i+1:02d}.jpg"))

print(f"Dummy images created in {image_dir}")

# Run the high-level SfM reconstruction
# Note: With only two random images, this is expected to fail or produce an empty model.
# A real scenario requires actual images with sufficient overlap and features.
try:
    sfm_pipeline = pycolmap.Sfm(image_dir, output_dir)
    reconstruction = sfm_pipeline.run()
    if reconstruction:
        print(f"Reconstruction successful! Found {len(reconstruction.images)} images and {len(reconstruction.points3D)} 3D points.")
    else:
        print("Reconstruction did not produce a valid model (expected for random, non-overlapping images).")
except Exception as e:
    print(f"Reconstruction failed (expected for random images without overlap/features): {e}")

# Optional: Clean up created directories
# import shutil
# shutil.rmtree(image_dir, ignore_errors=True)
# shutil.rmtree(output_dir, ignore_errors=True)

view raw JSON →