PyCOLMAP exposes to Python most capabilities of the COLMAP Structure-from-Motion (SfM) and Multi-View Stereo (MVS) pipeline.
Installation
Pre-built wheels for Linux, macOS, and Windows can be installed using pip:
pip install pycolmap
The wheels are automatically built and pushed to PyPI at each release. To benefit from GPU acceleration, wheels built for CUDA 12 (only for Linux - for now) are available under the package pycolmap-cuda12.
PyCOLMAP can leverage the GPU for feature extraction, matching, and multi-view stereo if COLMAP was compiled with CUDA support. Similarly, PyCOLMAP can run Delaunay Triangulation and Advancing Front Surface Reconstruction if COLMAP was compiled with CGAL support. This requires to build the package from source and is not available with the PyPI wheels.
Configuration Options
All of the above steps are easily configurable with python dicts which are recursively merged into their respective defaults, for example:
We can load and manipulate an existing COLMAP 3D reconstruction:
import pycolmap
reconstruction = pycolmap.Reconstruction("path/to/reconstruction/dir")
print(reconstruction.summary())
for image_id, image in reconstruction.images.items():
print(image_id, image)
for point3D_id, point3D in reconstruction.points3D.items():
print(point3D_id, point3D)
for camera_id, camera in reconstruction.cameras.items():
print(camera_id, camera)
reconstruction.write("path/to/reconstruction/dir/")
Common Operations
The object API mirrors the COLMAP C++ library. The bindings support many operations, for example:
Projecting a 3D point into an image with arbitrary camera model:
Exporting reconstructions to text, PLY, or other formats:
reconstruction.write_text("path/to/new/reconstruction/dir/") # text format
reconstruction.export_PLY("rec.ply") # PLY format
Estimators
We provide robust RANSAC-based estimators for:
Absolute camera pose (single-camera and multi-camera-rig)
Essential matrix
Fundamental matrix
Homography
Two-view relative pose for calibrated cameras
All RANSAC and estimation parameters are exposed as objects that behave similarly as Python dataclasses. The RANSAC options are described in colmap/optim/ransac.h and their default values are:
ransac_options = pycolmap.RANSACOptions(
max_error=4.0, # For example the reprojection error in pixels
min_inlier_ratio=0.01,
confidence=0.9999,
min_num_trials=1000,
max_num_trials=100000,
)
Absolute Pose Estimation
To estimate the absolute pose of a query camera given 2D-3D correspondences:
# Parameters:
# - points2D: Nx2 array; pixel coordinates
# - points3D: Nx3 array; world coordinates
# - camera: pycolmap.Camera
# Optional parameters:
# - estimation_options: dict or pycolmap.AbsolutePoseEstimationOptions
# - refinement_options: dict or pycolmap.AbsolutePoseRefinementOptions
answer = pycolmap.estimate_and_refine_absolute_pose(points2D, points3D, camera)
# Returns: dictionary of estimation outputs or None if failure
2D and 3D points are passed as Numpy arrays or lists. The options are defined in estimators/absolute_pose.cc and can be passed as regular (nested) Python dictionaries:
The TwoViewGeometryOptions control how each model is selected. The output structure contains the geometric model, inlier matches, the relative pose (if options.compute_relative_pose=True), and the type of camera configuration, which is an instance of the enum pycolmap.TwoViewGeometryConfiguration.
Camera Argument
Some estimators expect a COLMAP camera object, which can be created as follows:
camera = pycolmap.Camera(
model=camera_model_name_or_id,
width=width,
height=height,
params=params,
)
The different camera models and their extra parameters are defined in colmap/src/colmap/sensor/models.h. For example for a pinhole camera:
camera = pycolmap.Camera(
model='SIMPLE_PINHOLE',
width=width,
height=height,
params=[focal_length, cx, cy],
)
Alternatively, we can also pass a camera dictionary: