Skip to main content

COLMAP bindings

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pycolmap-0.5.0-cp311-cp311-win_amd64.whl (12.9 MB view details)

Uploaded CPython 3.11 Windows x86-64

pycolmap-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pycolmap-0.5.0-cp311-cp311-macosx_11_0_arm64.whl (7.8 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pycolmap-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl (9.0 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pycolmap-0.5.0-cp310-cp310-win_amd64.whl (12.9 MB view details)

Uploaded CPython 3.10 Windows x86-64

pycolmap-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pycolmap-0.5.0-cp310-cp310-macosx_11_0_arm64.whl (7.8 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pycolmap-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl (9.0 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pycolmap-0.5.0-cp39-cp39-win_amd64.whl (12.9 MB view details)

Uploaded CPython 3.9 Windows x86-64

pycolmap-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pycolmap-0.5.0-cp39-cp39-macosx_11_0_arm64.whl (7.8 MB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pycolmap-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl (9.0 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pycolmap-0.5.0-cp38-cp38-win_amd64.whl (13.0 MB view details)

Uploaded CPython 3.8 Windows x86-64

pycolmap-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pycolmap-0.5.0-cp38-cp38-macosx_11_0_arm64.whl (7.8 MB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pycolmap-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl (9.0 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

Details for the file pycolmap-0.5.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pycolmap-0.5.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 12.9 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pycolmap-0.5.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 26802f88d90d0c9405f7ab9b7cad8ab6f9935d6309220d53f3725a5810ec83e4
MD5 520d58d699376e2f6f40965f1d1353d4
BLAKE2b-256 ec86d691a383f8c5d189cebf1484d9de3e45eee426d69e24cae4d010e54db48b

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4e0ca1d0e10afbd5cf38c3cbb343bd86725c88a5bbf5ad1f5df859e521844f68
MD5 f3d5810f3e66babb90cdad1ee499d32c
BLAKE2b-256 489b1705ce938827020801e9a96339d38465d2e24de5896ab95130b42caf5c6d

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c7ffef64b29db97734f065a28f1f00dedd72503fe742d39339738ef39e1942f7
MD5 88885f8808c877fb18e69f238d7f3da1
BLAKE2b-256 a34acb620cb36e696ce7f50e5de8ae786586d6defe6de26ae26d806394e8e521

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2c346c4a0601df5a344e91afcbfa55e27068470ce0f098be1bbea22ee5277d7b
MD5 1db3b048e79d14b2183f0d04f0c00724
BLAKE2b-256 1143b3dbcdf4b22d12e63b3990e241bf8fe0738db81daed2e21d3dbd67427096

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pycolmap-0.5.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 12.9 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pycolmap-0.5.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c9ff56af4c90f47907cf92d3ae67deb76966095ced7f2a34e46d076563603749
MD5 71344c6f4d7b95c43cb0d8ddc18f91af
BLAKE2b-256 b862907ff9140d7c4a46967767980c10afc2fe4db949bb21054f5c62124d2564

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 19a29f3b4b5db34bc9b29f1109e91212f62aa34532edd4f6cb4ed6a07d40c075
MD5 0eb4c4db06a9712708e52a37cc881e1c
BLAKE2b-256 2044ec75a271c6c20717319fb7aa5e76fc11dde4c35df42ec20998c2634cdf09

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0e4677ad1ad460d849464f30dff21e2d019648def6c0b8b5ddf841ffbb117cad
MD5 5669211edd294da093585b50599ecea8
BLAKE2b-256 669606e243fc5ccd347c45879e136bb02bfe23221e6c3dcaa2a8818d10accd6f

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c16780c5b9547b804458b56928574609e1ca0a266e581b344f97d31b51d9c89a
MD5 0e245cc464216cf7d7c2e0ab8a1e66f8
BLAKE2b-256 1d11e5312dc426f197adef50e75f877732ea3ecb61f899ef4df74498f0490519

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pycolmap-0.5.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 12.9 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pycolmap-0.5.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 62e4c563a05a3a60962ec733746a1efe17c84d686209287c0c8c5cf611c79c9d
MD5 c74f3431ebdf989bdc28c4713c280c3b
BLAKE2b-256 a5d7dc9a3ce038a64df5d41d8ff26f69fa6a2131184da30d15f6c9878639a2ea

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 42d7321dc8aea6b7a65695ceb4b2b3641f1286711e537bea58553a0efb555cc7
MD5 3f13f28f32a1cea825df753b9b806e85
BLAKE2b-256 bd07be8aa6fd678236e353eac57a6a6d10c4167e0575fe7a97aba46492b69221

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7a7f7f2abd5f4617d3caec7eb4f33505f22a74c38ff97b92bea7db53b8f46076
MD5 bbcaaac9be7edd88f615fddfb065b50e
BLAKE2b-256 e3a1bcc99b73acb03f15cc5cfab8f4356e29a7526fdb58aba82df478a3b971d3

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a33928f97ab6b13dfe278cd47baa06439f51afb987acb9fd9fc495f7deb3abac
MD5 a5a29e7a19d16c7424086cd54f9c6c8a
BLAKE2b-256 def487d6f95751f4b91fc62e483d7b178230ab686b2dab8f3074ca702323e0bd

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pycolmap-0.5.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 13.0 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pycolmap-0.5.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 24c0d43c78711a0444e9e6a9a542a435c8b0c8bf8929499aa50084f8a7b45809
MD5 1dbdd4c4ae26988fc9cee13754afd0e2
BLAKE2b-256 73eb12103f2f1831fb4c2dcb37ccb979b82e2f55b11f9ce117eaa93772cf7ad1

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 de110135263a1bd5d03dfbf894add03c6adb5dce30168c2788ed8c957e92ffbd
MD5 5101e43691be53072ca3f6205db88e13
BLAKE2b-256 45f06ba3bb5277434b1a626b901eacbedbf05125fc85df5fbca44bb694447fbe

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a7f37bce87be5222278856e77842d02736ead41bcf4a08bdc347e782d434c52d
MD5 bf4cfdf05bdb5534902fac28154d7c07
BLAKE2b-256 e9086feb3a564dbb4e6d4918c9c907596bbda2f15e70bd39c812f0c120752518

See more details on using hashes here.

File details

Details for the file pycolmap-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pycolmap-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a2608e50d6335e9d1d5b502ef6fd8210af31a59684ccbab10e73c3e9d591f0a4
MD5 0e66e15b9cab1d79f3723ca5d75c44c2
BLAKE2b-256 48607604843ce25e63e55b81a70ec1a1b8f73726a052195ed7abff06afd711e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page