| 1 |
from pathlib import Path |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import pycolmap |
| 6 |
|
| 7 |
|
| 8 |
def test_view_graph_calibration_options_init(): |
| 9 |
options = pycolmap.ViewGraphCalibrationOptions() |
| 10 |
assert options is not None |
| 11 |
|
| 12 |
|
| 13 |
def test_global_mapper_options_init(): |
| 14 |
options = pycolmap.GlobalMapperOptions() |
| 15 |
assert options is not None |
| 16 |
|
| 17 |
|
| 18 |
def test_global_pipeline_options_init(): |
| 19 |
options = pycolmap.GlobalPipelineOptions() |
| 20 |
assert options is not None |
| 21 |
|
| 22 |
|
| 23 |
def test_global_pipeline_options_min_num_matches(): |
| 24 |
options = pycolmap.GlobalPipelineOptions() |
| 25 |
options.min_num_matches = 20 |
| 26 |
assert options.min_num_matches == 20 |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
"name", |
| 31 |
[ |
| 32 |
"incremental_mapping", |
| 33 |
"global_mapping", |
| 34 |
"hierarchical_mapping", |
| 35 |
"triangulate_points", |
| 36 |
"calibrate_view_graph", |
| 37 |
"bundle_adjustment", |
| 38 |
], |
| 39 |
) |
| 40 |
def test_public_api_callable(name): |
| 41 |
assert callable(getattr(pycolmap, name)) |
| 42 |
|
| 43 |
|
| 44 |
def test_hierarchical_pipeline_options_init(): |
| 45 |
options = pycolmap.HierarchicalPipelineOptions() |
| 46 |
assert options is not None |
| 47 |
|
| 48 |
|
| 49 |
def test_incremental_mapping(tmp_path: Path): |
| 50 |
pycolmap.set_random_seed(0) |
| 51 |
|
| 52 |
database_path = tmp_path / "database.db" |
| 53 |
image_path = tmp_path / "images" |
| 54 |
image_path.mkdir() |
| 55 |
output_path = tmp_path / "sparse" |
| 56 |
output_path.mkdir() |
| 57 |
|
| 58 |
with pycolmap.Database.open(database_path) as database: |
| 59 |
synthetic_dataset_options = pycolmap.SyntheticDatasetOptions() |
| 60 |
synthetic_dataset_options.num_rigs = 2 |
| 61 |
synthetic_dataset_options.num_cameras_per_rig = 1 |
| 62 |
synthetic_dataset_options.num_frames_per_rig = 7 |
| 63 |
synthetic_dataset_options.num_points3D = 50 |
| 64 |
synthetic_dataset_options.camera_has_prior_focal_length = False |
| 65 |
gt_reconstruction = pycolmap.synthesize_dataset( |
| 66 |
synthetic_dataset_options, database |
| 67 |
) |
| 68 |
|
| 69 |
reconstructions = pycolmap.incremental_mapping( |
| 70 |
database_path, image_path, output_path |
| 71 |
) |
| 72 |
|
| 73 |
assert len(reconstructions) == 1 |
| 74 |
assert ( |
| 75 |
reconstructions[0].num_reg_images() |
| 76 |
== gt_reconstruction.num_reg_images() |
| 77 |
) |
| 78 |
assert (output_path / "0").exists() |
| 79 |
|
| 80 |
|
| 81 |
def test_global_mapping(tmp_path: Path): |
| 82 |
pycolmap.set_random_seed(0) |
| 83 |
|
| 84 |
database_path = tmp_path / "database.db" |
| 85 |
image_path = tmp_path / "images" |
| 86 |
image_path.mkdir() |
| 87 |
output_path = tmp_path / "sparse" |
| 88 |
output_path.mkdir() |
| 89 |
|
| 90 |
with pycolmap.Database.open(database_path) as database: |
| 91 |
synthetic_dataset_options = pycolmap.SyntheticDatasetOptions() |
| 92 |
synthetic_dataset_options.num_rigs = 2 |
| 93 |
synthetic_dataset_options.num_cameras_per_rig = 1 |
| 94 |
synthetic_dataset_options.num_frames_per_rig = 7 |
| 95 |
synthetic_dataset_options.num_points3D = 50 |
| 96 |
synthetic_dataset_options.camera_has_prior_focal_length = False |
| 97 |
gt_reconstruction = pycolmap.synthesize_dataset( |
| 98 |
synthetic_dataset_options, database |
| 99 |
) |
| 100 |
|
| 101 |
# Global mapping requires calibrated two-view geometries. |
| 102 |
assert pycolmap.calibrate_view_graph(database_path) |
| 103 |
|
| 104 |
reconstructions = pycolmap.global_mapping( |
| 105 |
database_path, image_path, output_path |
| 106 |
) |
| 107 |
|
| 108 |
assert len(reconstructions) == 1 |
| 109 |
assert ( |
| 110 |
reconstructions[0].num_reg_images() |
| 111 |
== gt_reconstruction.num_reg_images() |
| 112 |
) |
| 113 |
assert (output_path / "0").exists() |
| 114 |
result = pycolmap.compare_reconstructions( |
| 115 |
reconstructions[0], |
| 116 |
gt_reconstruction, |
| 117 |
alignment_error="proj_center", |
| 118 |
max_proj_center_error=1e-4, |
| 119 |
) |
| 120 |
assert result is not None |
| 121 |
for error in result["errors"]: |
| 122 |
assert error.rotation_error_deg < 1e-2 |
| 123 |
assert error.proj_center_error < 1e-4 |
| 124 |
|
| 125 |
|
| 126 |
def test_hierarchical_mapping(tmp_path: Path): |
| 127 |
pycolmap.set_random_seed(0) |
| 128 |
|
| 129 |
database_path = tmp_path / "database.db" |
| 130 |
image_path = tmp_path / "images" |
| 131 |
image_path.mkdir() |
| 132 |
output_path = tmp_path / "sparse" |
| 133 |
output_path.mkdir() |
| 134 |
|
| 135 |
with pycolmap.Database.open(database_path) as database: |
| 136 |
synthetic_dataset_options = pycolmap.SyntheticDatasetOptions() |
| 137 |
synthetic_dataset_options.num_rigs = 2 |
| 138 |
synthetic_dataset_options.num_cameras_per_rig = 1 |
| 139 |
synthetic_dataset_options.num_frames_per_rig = 10 |
| 140 |
synthetic_dataset_options.num_points3D = 100 |
| 141 |
gt_reconstruction = pycolmap.synthesize_dataset( |
| 142 |
synthetic_dataset_options, database |
| 143 |
) |
| 144 |
|
| 145 |
options = pycolmap.HierarchicalPipelineOptions() |
| 146 |
options.clustering_options.leaf_max_num_images = 5 |
| 147 |
options.clustering_options.image_overlap = 3 |
| 148 |
reconstructions = pycolmap.hierarchical_mapping( |
| 149 |
database_path, image_path, output_path, options |
| 150 |
) |
| 151 |
|
| 152 |
assert len(reconstructions) == 1 |
| 153 |
assert ( |
| 154 |
reconstructions[0].num_reg_images() |
| 155 |
== gt_reconstruction.num_reg_images() |
| 156 |
) |
| 157 |
assert (output_path / "0").exists() |