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 installed 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 or 3.9.1 for PyCOLMAP 0.4.0 or 0.5.0/0.6.0.

  2. Clone the PyCOLMAP repository:

git clone -b 0.6.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 example.py or 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,  # 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

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,
    [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.6.1-cp311-cp311-win_amd64.whl (13.0 MB view details)

Uploaded CPython 3.11 Windows x86-64

pycolmap-0.6.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pycolmap-0.6.1-cp311-cp311-macosx_11_0_arm64.whl (7.9 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pycolmap-0.6.1-cp311-cp311-macosx_10_9_x86_64.whl (9.1 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pycolmap-0.6.1-cp310-cp310-win_amd64.whl (13.0 MB view details)

Uploaded CPython 3.10 Windows x86-64

pycolmap-0.6.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pycolmap-0.6.1-cp310-cp310-macosx_11_0_arm64.whl (7.9 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pycolmap-0.6.1-cp310-cp310-macosx_10_9_x86_64.whl (9.1 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pycolmap-0.6.1-cp39-cp39-win_amd64.whl (13.0 MB view details)

Uploaded CPython 3.9 Windows x86-64

pycolmap-0.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pycolmap-0.6.1-cp39-cp39-macosx_11_0_arm64.whl (7.9 MB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pycolmap-0.6.1-cp39-cp39-macosx_10_9_x86_64.whl (9.1 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

pycolmap-0.6.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pycolmap-0.6.1-cp38-cp38-macosx_11_0_arm64.whl (7.9 MB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pycolmap-0.6.1-cp38-cp38-macosx_10_9_x86_64.whl (9.1 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: pycolmap-0.6.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 13.0 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.6.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3906ec5d7a1b264501193354bc90bf1f55e7ab7a364b60e58187b51dd5b8a422
MD5 158acfc260ca6d199b44c766583102f1
BLAKE2b-256 f7222be3306a0402e1d8014f7adbe03253a81403f48f7909f55ba2146f475a23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9118d7e3e982a4f648c06aff2bdfb46a4113730ee701007e0ed3edeb4ef121c1
MD5 490ffda7492863e8d28520c17eecf54a
BLAKE2b-256 b86a4a63daf3c13cfb394283b9bf5ed590ce1f1fb5194194aef0703108f8f524

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7e4350b2b9604e1e41bf1f2939c0d81b5ba4d1d045f123d17a0f2770be6ccb3c
MD5 77f291f0daaad8b506a4c0bb22a0cbfd
BLAKE2b-256 681280b4bca3bda3370e6033bb57cf89b234ad7d5ae894b6b22b9cbbcfe435db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 99b355b1a03239922c0cb7ff564f333000c474fcf39a6b00bf7d31682abb43b7
MD5 5cf90212b22051e130be3ae6475eb3b0
BLAKE2b-256 0d11772caf882d1b802bec7da4fedf33d9b1f91ac41b005561be54cf35a6c2ac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pycolmap-0.6.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 13.0 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.6.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 bb8dbb2ea0e4abd1335368f23cad3275b3a5195cea825afa0b4f4d2533e43e4e
MD5 38a0620f923efbde15e20fa270b23304
BLAKE2b-256 09907ad78962529de93753435d07b5d7f95e1b0f8dc800d15234fe01ec7cd5cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3bce081ff00d0b51c031f43568211ea216c74e6c69d5906aa5a196f58f0f7b8e
MD5 b969f13344b351947887276661c6c74a
BLAKE2b-256 fb038bd3111b9d5a98d528e5953f9a687c33579e40d269b5c48551923dcf9c36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e0f72506a96f83f4255a95be0f5c37cf4ce3c1f8f7062ea6cdaaba4d89965bb
MD5 c5c9bf93a7e4093e903b1ef6b70b78b5
BLAKE2b-256 5a64feb949264c68f14381deab724df7ab5bfe74121167c86e152b7ab9b2a6c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a93c1883279a3462a6a7ceda0c04a737326114f1942d3c07bdc3e9d9e2bb48db
MD5 48b283f2ccf54b1e2c254112e7953a4d
BLAKE2b-256 26805ffb34caec93dfa051bd6119748b2e6790585d69fa29f841923848ab427d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pycolmap-0.6.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 13.0 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.6.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c1da78b2281601197b709dc0f01c3a770fd8ead29a5a1511629782aa255a8eac
MD5 bb4b235ba07ce7cbe2eb25631459986e
BLAKE2b-256 ad830a55f4c23f6f230f5cdd3cf95fcbbc33b265f05c386f9e178d8d2d61991b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 81f4d9c4818e55a59dee5b9254c6903ee5186e6c1e85b643b873c4d0607b897c
MD5 e438c699f3e7e9fbb3de82db04fcb62e
BLAKE2b-256 b1716888ee311756b662146ccd94395467506ae2fbc10660fbf69983ebcc13e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3867d0eeb0ec29412a4106aaa5da69a544aad1e0836095ddc373fd583600291d
MD5 65b60746832ddfd029a13655a864f32d
BLAKE2b-256 0384393e51bc390b3669fe9c37943df82942f67f365e49826c4952cb17046016

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9949c1a7e1bf69b724ceab9c91c1e065d31ca1bf652ee2213e15e5531739aee4
MD5 14c9d0e399743131244a39267a6b9555
BLAKE2b-256 b5073e46960c669f9174791c5819874ecb8be3698a518340c88770cc2a4483d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pycolmap-0.6.1-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.6.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 3c46cb885d16b893e20cb9ce536adee4e75393c81dbc3fd4d87f37b46bfabd27
MD5 1ccf62777fbb2587a90c52addaeefcd2
BLAKE2b-256 22b9aa3a7c4fcaaed26c61610b282ff17224de2d90c7b990f9ce4dcac7052a9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1bee6b892aa4e78c9a058d96ad90d86adee0ff6cdf3d8572b8d2ea0e7ba8158c
MD5 bb01eb6987f9ce30386a876e356ae10c
BLAKE2b-256 cfade2b342c9491202b7b4b76e8774c4f9ac66ff6f8f1129d22b90624f60d678

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2fa92ed92c3401a87820f48b4b10a675155067cf1a66de01ab9462e940d87eeb
MD5 21962f9f224c66ca32b557b6bd788b78
BLAKE2b-256 75d95f750222b9c4a872487237246f15b4fb7d3d3f2645e351bb555d03cedfc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycolmap-0.6.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c1ea165b851381519f034b781c704dccda486502324cb52e3040e51dd5495d9e
MD5 673ed40c3a19dd3c8e52026c6230773c
BLAKE2b-256 5bae354868e52d72d778c17e9c2decca9fe86c518066e3595ceb5d39038842b1

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