Skip to main content

Python bindings for COLMAP

This repository exposes to Python most capabilities of COLMAP for Structure-from-Motion and Multiview-stereo, such as reconstruction pipelines & objects and geometric estimators.

Installation

Wheels for Python 8/9/10 on Linux, macOS 10/11/12 (both Intel and Apple Silicon), and Windows can be install using pip:

pip install pycolmap

The wheels are automatically built and pushed to PyPI at each release. They are currently not built with CUDA support, which requires building from source.

[Building PyCOLMAP from source - click to expand]
  1. Install COLMAP from source following the official guide. Use COLMAP 3.8/3.9 for PyCOLMAP 0.4.0/0.5.0.

  2. Clone the PyCOLMAP repository:

git clone -b 0.5.0 https://github.com/colmap/pycolmap.git
cd pycolmap
  1. Build:
  • On Linux and macOS:
python -m pip install .
  • On Windows, after installing COLMAP via VCPKG, run in powershell:
py -m pip install . `
    --cmake.define.CMAKE_TOOLCHAIN_FILE="$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake" `
    --cmake.define.VCPKG_TARGET_TRIPLET="x64-windows"

Reconstruction pipeline

PyCOLMAP provides bindings for multiple steps of the standard reconstruction pipeline:

  • extracting and matching SIFT features
  • importing an image folder into a COLMAP database
  • inferring the camera parameters from the EXIF metadata of an image file
  • running two-view geometric verification of matches on a COLMAP database
  • triangulating points into an existing COLMAP model
  • running incremental reconstruction from a COLMAP database
  • dense reconstruction with multi-view stereo

Sparse & Dense reconstruction from a folder of images can be performed with:

output_path: pathlib.Path
image_dir: pathlib.Path

output_path.mkdir()
mvs_path = output_path / "mvs"
database_path = output_path / "database.db"

pycolmap.extract_features(database_path, image_dir)
pycolmap.match_exhaustive(database_path)
maps = pycolmap.incremental_mapping(database_path, image_dir, output_path)
maps[0].write(output_path)
# dense reconstruction
pycolmap.undistort_images(mvs_path, output_path, image_dir)
pycolmap.patch_match_stereo(mvs_path)  # requires compilation with CUDA
pycolmap.stereo_fusion(mvs_path / "dense.ply", mvs_path)

PyCOLMAP can leverage the GPU for feature extraction, matching, and multi-view stereo if COLMAP was compiled with CUDA support. Similarly, PyCOLMAP can run Delauney Triangulation if COLMAP was compiled with CGAL support. This requires to build the package from source and is not available with the PyPI wheels.

All of the above steps are easily configurable with python dicts which are recursively merged into their respective defaults, for example:

pycolmap.extract_features(database_path, image_dir, sift_options={"max_num_features": 512})
# equivalent to
ops = pycolmap.SiftExtractionOptions()
ops.max_num_features = 512
pycolmap.extract_features(database_path, image_dir, sift_options=ops)

To list available options and their default parameters:

help(pycolmap.SiftExtractionOptions)

For another example of usage, see hloc/reconstruction.py.

Reconstruction object

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/")

The object API mirrors the COLMAP C++ library. The bindings support many other operations, for example:

  • projecting a 3D point into an image with arbitrary camera model:
uv = camera.img_from_cam(image.cam_from_world * point3D.xyz)
  • aligning two 3D reconstructions by their camera poses:
rec2_from_rec1 = pycolmap.align_reconstructions_via_reprojections(reconstruction1, reconstrution2)
reconstruction1.transform(rec2_from_rec1)
print(rec2_from_rec1.scale, rec2_from_rec1.rotation, rec2_from_rec1.translation)
  • 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, and 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,  # reprojection error in pixels
    min_inlier_ratio=0.01,
    confidence=0.9999,
    min_num_trials=1000,
    max_num_trials=100000,
)

Absolute pose estimation

For instance, 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.absolute_pose_estimation(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:

pycolmap.absolute_pose_estimation(
    points2D, points3D, camera,
    estimation_options=dict(ransac=dict(max_error=12.0)),
    refinement_options=dict(refine_focal_length=True),
)

Absolute Pose Refinement

# Parameters:
# - cam_from_world: pycolmap.Rigid3d, initial pose
# - points2D: Nx2 array; pixel coordinates
# - points3D: Nx3 array; world coordinates
# - inlier_mask: array of N bool; inlier_mask[i] is true if correpondence i is an inlier
# - camera: pycolmap.Camera
# Optional parameters:
# - refinement_options: dict or pycolmap.AbsolutePoseRefinementOptions
answer = pycolmap.pose_refinement(cam_from_world, points2D, points3D, inlier_mask, camera)
# Returns: dictionary of refinement outputs or None if failure

Essential matrix estimation

# Parameters:
# - points1: Nx2 array; 2D pixel coordinates in image 1
# - points2: Nx2 array; 2D pixel coordinates in image 2
# - camera1: pycolmap.Camera of image 1
# - camera2: pycolmap.Camera of image 2
# Optional parameters:
# - options: dict or pycolmap.RANSACOptions (default inlier threshold is 4px)
answer = pycolmap.essential_matrix_estimation(points1, points2, camera1, camera2)
# Returns: dictionary of estimation outputs or None if failure

Fundamental matrix estimation

answer = pycolmap.fundamental_matrix_estimation(
    points1,
    points2,
    [options],       # optional dict or pycolmap.RANSACOptions
)

Homography estimation

answer = pycolmap.homography_matrix_estimation(
    points1,
    points2,
    [max_error_px],  # optional RANSAC inlier threshold in pixels
    [options],       # optional dict or pycolmap.RANSACOptions
)

Two-view geometry estimation

COLMAP can also estimate a relative pose between two calibrated cameras by estimating both E and H and accounting for the degeneracies of each model.

# Parameters:
# - camera1: pycolmap.Camera of image 1
# - points1: Nx2 array; 2D pixel coordinates in image 1
# - camera2: pycolmap.Camera of image 2
# - points2: Nx2 array; 2D pixel coordinates in image 2
# Optional parameters:
# - matches: Nx2 integer array; correspondences across images
# - options: dict or pycolmap.TwoViewGeometryOptions
answer = pycolmap.estimate_calibrated_two_view_geometry(camera1, points1, camera2, points2)
# Returns: pycolmap.TwoViewGeometry

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 follow:

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:

camera_dict = {
    'model': COLMAP_CAMERA_MODEL_NAME_OR_ID,
    'width': IMAGE_WIDTH,
    'height': IMAGE_HEIGHT,
    'params': EXTRA_CAMERA_PARAMETERS_LIST
}

SIFT feature extraction

import numpy as np
import pycolmap
from PIL import Image, ImageOps

# Input should be grayscale image with range [0, 1].
img = Image.open('image.jpg').convert('RGB')
img = ImageOps.grayscale(img)
img = np.array(img).astype(np.float) / 255.

# Optional parameters:
# - options: dict or pycolmap.SiftExtractionOptions
# - device: default pycolmap.Device.auto uses the GPU if available
sift = pycolmap.Sift()

# Parameters:
# - image: HxW float array
keypoints, descriptors = sift.extract(img)
# Returns:
# - keypoints: Nx4 array; format: x (j), y (i), scale, orientation
# - descriptors: Nx128 array; L2-normalized descriptors

TODO

  • Add documentation
  • Add more detailed examples
  • Add unit tests for reconstruction bindings

Created and maintained by Mihai Dusmanu, Philipp Lindenberger, John Lambert, Paul-Edouard Sarlin, and other contributors.

Release files for pycolmap 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for pycolmap 0.6.0
File
pycolmap-0.6.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
pycolmap-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
pycolmap-0.6.0-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
pycolmap-0.6.0-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
pycolmap-0.6.0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
pycolmap-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
pycolmap-0.6.0-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
pycolmap-0.6.0-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details
pycolmap-0.6.0-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
pycolmap-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details
pycolmap-0.6.0-cp39-cp39-macosx_11_0_arm64.whl CPython 3.9 CPython 3.9 macOS 11.0+ ARM64 Details
pycolmap-0.6.0-cp39-cp39-macosx_10_9_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.9+ x86-64 Details
pycolmap-0.6.0-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
pycolmap-0.6.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details
pycolmap-0.6.0-cp38-cp38-macosx_11_0_arm64.whl CPython 3.8 CPython 3.8 macOS 11.0+ ARM64 Details
pycolmap-0.6.0-cp38-cp38-macosx_10_9_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.9+ x86-64 Details

Total release size: 167.7 MB

Release files / pycolmap-0.6.0-cp311-cp311-win_amd64.whl

Download URL pycolmap-0.6.0-cp311-cp311-win_amd64.whl
Size 13.0 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
3c31fb0d8ff67ece39f1f5035fc1687013ade8dff10cc44e5cdcc42246e19479
BLAKE2b-256 checksum
How to use checksums
c77cba47281508901c72144afdf78bb441a711a0b05da916986ee8b3e6e33138
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pycolmap-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 11.9 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
a4f23986f001a264ccaa43bc926a4d353bd735a64a533b39f2443ab158468116
BLAKE2b-256 checksum
How to use checksums
66f67911d72a9b5b5d41da9826db33e9e657cb8f03ea87eabc0b550166eb0219
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp311-cp311-macosx_11_0_arm64.whl

Download URL pycolmap-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Size 7.9 MB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
414da130d2002a57c4c88359fd37a9fa5084cc37ca2cbbb694123f1c3919f411
BLAKE2b-256 checksum
How to use checksums
6b42e7613e7d4ee580fb9f24c7c07755c209487ef35e94d3cfc871551a8eed53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp311-cp311-macosx_10_9_x86_64.whl

Download URL pycolmap-0.6.0-cp311-cp311-macosx_10_9_x86_64.whl
Size 9.1 MB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ecc163880ec397765d96bcfe80fed913bd32b4dd571c1e529e8296ecea81e567
BLAKE2b-256 checksum
How to use checksums
b0b17c208b95bd4bab3dfe5c274020ba781a08af50497021646c2a7bb3c616d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp310-cp310-win_amd64.whl

Download URL pycolmap-0.6.0-cp310-cp310-win_amd64.whl
Size 13.0 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
743d1334b2c3febb0f7653892861094a223d7e738ce98151dc4080f2c36bad7e
BLAKE2b-256 checksum
How to use checksums
f62d98a84435f16e1e115cd12254abe0c7a09fbb48a5a84f4cd8df9861441c8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pycolmap-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 11.9 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
2711b8077fab6055ac13216a3e57b93601a8300cbdb248f8de99d1d7ed9fe28e
BLAKE2b-256 checksum
How to use checksums
c3e0180fb3fcd2424785cc08ddcaf648421d551962636be8d048868bf51dfc22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp310-cp310-macosx_11_0_arm64.whl

Download URL pycolmap-0.6.0-cp310-cp310-macosx_11_0_arm64.whl
Size 7.9 MB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fe820344befaa089f326657fcf9a9ad36bac8c0d56f9f9ebddf8daa610bb8794
BLAKE2b-256 checksum
How to use checksums
2b07e0c037af54525df2c40d896dba327ae348bd3439e28a0ae04d59d220851a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp310-cp310-macosx_10_9_x86_64.whl

Download URL pycolmap-0.6.0-cp310-cp310-macosx_10_9_x86_64.whl
Size 9.1 MB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
eaae50ab199b8097f74d66499e5728652586353c34fa435664925cb8158553c5
BLAKE2b-256 checksum
How to use checksums
b781e7e06f5c8b0eda445b84079df0b754751af64815cc7692ad6a6ddbbb4b1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp39-cp39-win_amd64.whl

Download URL pycolmap-0.6.0-cp39-cp39-win_amd64.whl
Size 13.0 MB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
28d35afbc62d96b93af8200490022169856c27f1ef32e75c91c845acdb48d8e8
BLAKE2b-256 checksum
How to use checksums
416602e7459c7f3993fb8bddebc381d119e4731a585733a0b4d4a33241430c62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pycolmap-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 11.9 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
d26a79cbfc1e880a8b55afb356f6c4d2d70836e0f9499af7f39daa2a6a3481ff
BLAKE2b-256 checksum
How to use checksums
6f44d5a3b3b74773804b9fc50657160243332b921185c0a45dcf73bdc8e7ca9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp39-cp39-macosx_11_0_arm64.whl

Download URL pycolmap-0.6.0-cp39-cp39-macosx_11_0_arm64.whl
Size 7.9 MB
Tags CPython 3.9 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ccdebf07afcdf2eb7cb1f3f0a7cfae4ef858de3e6bf7d74b7b2e4014e6222f85
BLAKE2b-256 checksum
How to use checksums
0062599321cc51cf7b22aff55382f97b0b4343afd71a4a34f4404adf322c6857
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp39-cp39-macosx_10_9_x86_64.whl

Download URL pycolmap-0.6.0-cp39-cp39-macosx_10_9_x86_64.whl
Size 9.1 MB
Tags CPython 3.9 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
9878d8982fc18a9f86e15d91903ecabd4392ab68fad98208302c38b15cff40b9
BLAKE2b-256 checksum
How to use checksums
b317d298e54b532cd8ecccae5fafa08893a953fe2acfba560d7522fd57ff9b55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp38-cp38-win_amd64.whl

Download URL pycolmap-0.6.0-cp38-cp38-win_amd64.whl
Size 13.0 MB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
53a1e4f9d83d6bac92edc558772cc825f749a1fff8a888abb7951a179f7a76f7
BLAKE2b-256 checksum
How to use checksums
2ea7bc244d5c5d593a92b6b3fe551744eb9978e1d7429ec17370dac64cb53956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pycolmap-0.6.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 11.9 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
00d1f82b5b349f22d5a3a9e128a376115f3270f2881005ed57f9c75756b23799
BLAKE2b-256 checksum
How to use checksums
0ab9b44b3dc4e4b58948ee9511f50aaea869ebc804781c839cd21fee39476903
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp38-cp38-macosx_11_0_arm64.whl

Download URL pycolmap-0.6.0-cp38-cp38-macosx_11_0_arm64.whl
Size 7.9 MB
Tags CPython 3.8 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ae7a713cfe8b2d2dfa34fe99ba76c2c801bbebcc49b65a8e63dd57ed07b8e51b
BLAKE2b-256 checksum
How to use checksums
0c19fc4eae591228b03086dde26dc1263a5f3ef164c2cb7e9f556ae9c99884c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pycolmap-0.6.0-cp38-cp38-macosx_10_9_x86_64.whl

Download URL pycolmap-0.6.0-cp38-cp38-macosx_10_9_x86_64.whl
Size 9.1 MB
Tags CPython 3.8 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
dadc61330fcd146e8aa0f5bb7a15e8e2e4debbe697cc6416866921eaddf68fbb
BLAKE2b-256 checksum
How to use checksums
621d8ebd86aa22dee2a910242c49ff36aa829483a21ca86759f386029cae9d4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page