sfmtool 0.2__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. sfmtool/__init__.py +42 -0
  2. sfmtool/_align.py +226 -0
  3. sfmtool/_align_by_cameras.py +74 -0
  4. sfmtool/_align_by_points.py +201 -0
  5. sfmtool/_camera_setup.py +82 -0
  6. sfmtool/_cameras.py +213 -0
  7. sfmtool/_cli_group.py +59 -0
  8. sfmtool/_cli_utils.py +29 -0
  9. sfmtool/_colmap_db.py +575 -0
  10. sfmtool/_colmap_io.py +608 -0
  11. sfmtool/_commands/__init__.py +48 -0
  12. sfmtool/_commands/align.py +154 -0
  13. sfmtool/_commands/compare.py +56 -0
  14. sfmtool/_commands/densify.py +236 -0
  15. sfmtool/_commands/discontinuity.py +176 -0
  16. sfmtool/_commands/epipolar.py +365 -0
  17. sfmtool/_commands/flow.py +175 -0
  18. sfmtool/_commands/from_colmap_bin.py +137 -0
  19. sfmtool/_commands/heatmap.py +249 -0
  20. sfmtool/_commands/init.py +145 -0
  21. sfmtool/_commands/inspect.py +194 -0
  22. sfmtool/_commands/insv2rig.py +115 -0
  23. sfmtool/_commands/match.py +926 -0
  24. sfmtool/_commands/merge.py +94 -0
  25. sfmtool/_commands/pano2rig.py +96 -0
  26. sfmtool/_commands/sift.py +269 -0
  27. sfmtool/_commands/solve.py +484 -0
  28. sfmtool/_commands/to_colmap_bin.py +53 -0
  29. sfmtool/_commands/to_colmap_db.py +130 -0
  30. sfmtool/_commands/undistort.py +68 -0
  31. sfmtool/_commands/xform.py +503 -0
  32. sfmtool/_compare.py +574 -0
  33. sfmtool/_densify.py +743 -0
  34. sfmtool/_discontinuity.py +1309 -0
  35. sfmtool/_epipolar_viz.py +631 -0
  36. sfmtool/_extract_sift_colmap.py +245 -0
  37. sfmtool/_extract_sift_opencv.py +181 -0
  38. sfmtool/_filenames.py +208 -0
  39. sfmtool/_flow_viz.py +718 -0
  40. sfmtool/_gsfm.py +176 -0
  41. sfmtool/_histogram_utils.py +130 -0
  42. sfmtool/_image_pair_graph.py +124 -0
  43. sfmtool/_inspect_depth.py +87 -0
  44. sfmtool/_inspect_graphs.py +159 -0
  45. sfmtool/_inspect_images.py +501 -0
  46. sfmtool/_inspect_metrics.py +238 -0
  47. sfmtool/_inspect_summary.py +169 -0
  48. sfmtool/_insv2rig.py +255 -0
  49. sfmtool/_isfm.py +176 -0
  50. sfmtool/_merge.py +387 -0
  51. sfmtool/_merge_correspondences.py +326 -0
  52. sfmtool/_merge_pose_refinement.py +293 -0
  53. sfmtool/_multi_align.py +415 -0
  54. sfmtool/_pano2rig.py +384 -0
  55. sfmtool/_point_correspondence.py +59 -0
  56. sfmtool/_rectification.py +200 -0
  57. sfmtool/_rig_config.py +63 -0
  58. sfmtool/_rig_frames.py +100 -0
  59. sfmtool/_sfm_reconstruction.py +115 -0
  60. sfmtool/_sfmtool.cp313-win_amd64.pyd +0 -0
  61. sfmtool/_sift_file.py +827 -0
  62. sfmtool/_to_colmap_db.py +247 -0
  63. sfmtool/_undistort_images.py +142 -0
  64. sfmtool/_workspace.py +135 -0
  65. sfmtool/cli.py +103 -0
  66. sfmtool/feature_match/__init__.py +17 -0
  67. sfmtool/feature_match/_core.py +199 -0
  68. sfmtool/feature_match/_flow_matching.py +389 -0
  69. sfmtool/feature_match/_geometric_filter.py +47 -0
  70. sfmtool/feature_match/_geometry.py +132 -0
  71. sfmtool/feature_match/_polar_sweep.py +167 -0
  72. sfmtool/feature_match/_rectified_sweep.py +167 -0
  73. sfmtool/visualization/__init__.py +14 -0
  74. sfmtool/visualization/_colormap.py +146 -0
  75. sfmtool/visualization/_heatmap_renderer.py +281 -0
  76. sfmtool/xform/__init__.py +50 -0
  77. sfmtool/xform/_align_to.py +69 -0
  78. sfmtool/xform/_align_to_input.py +72 -0
  79. sfmtool/xform/_apply.py +31 -0
  80. sfmtool/xform/_bundle_adjust.py +155 -0
  81. sfmtool/xform/_filter_by_image_range.py +256 -0
  82. sfmtool/xform/_filter_by_reprojection_error.py +35 -0
  83. sfmtool/xform/_interface.py +20 -0
  84. sfmtool/xform/_remove_isolated.py +81 -0
  85. sfmtool/xform/_remove_large_features.py +92 -0
  86. sfmtool/xform/_remove_narrow_tracks.py +48 -0
  87. sfmtool/xform/_remove_short_tracks.py +35 -0
  88. sfmtool/xform/_rotate.py +25 -0
  89. sfmtool/xform/_scale.py +22 -0
  90. sfmtool/xform/_scale_by_measurements.py +384 -0
  91. sfmtool/xform/_similarity.py +26 -0
  92. sfmtool/xform/_translate.py +26 -0
  93. sfmtool-0.2.dist-info/METADATA +14 -0
  94. sfmtool-0.2.dist-info/RECORD +98 -0
  95. sfmtool-0.2.dist-info/WHEEL +4 -0
  96. sfmtool-0.2.dist-info/entry_points.txt +2 -0
  97. sfmtool-0.2.dist-info/licenses/LICENSE +201 -0
  98. sfmtool-0.2.dist-info/sboms/sfmtool-py.cyclonedx.json +8794 -0
sfmtool/__init__.py ADDED
@@ -0,0 +1,42 @@
1
+ # Copyright The SfM Tool Authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ from sfmtool._sfmtool import * # noqa: F401, F403
5
+ from sfmtool._filenames import ( # noqa: F401
6
+ expand_paths,
7
+ normalize_workspace_path,
8
+ number_from_filename,
9
+ )
10
+ from sfmtool._workspace import ( # noqa: F401
11
+ find_workspace_for_path,
12
+ init_workspace,
13
+ load_workspace_config,
14
+ )
15
+ from sfmtool._sift_file import ( # noqa: F401
16
+ SiftExtractionError,
17
+ SiftReader,
18
+ compute_orientation,
19
+ draw_sift_features,
20
+ feature_size,
21
+ feature_size_x,
22
+ feature_size_y,
23
+ get_feature_tool_xxh128,
24
+ get_feature_type_for_tool,
25
+ get_sift_path_for_image,
26
+ get_used_features_from_reconstruction,
27
+ image_files_to_sift_files,
28
+ image_files_to_sift_files_opencv,
29
+ print_sift_summary,
30
+ write_sift,
31
+ xxh128_of_file,
32
+ )
33
+ from sfmtool._extract_sift_colmap import ( # noqa: F401
34
+ extract_sift_with_colmap,
35
+ get_colmap_feature_options,
36
+ read_colmap_db_sift,
37
+ )
38
+ from sfmtool._extract_sift_opencv import ( # noqa: F401
39
+ extract_sift_with_opencv,
40
+ get_default_opencv_feature_options,
41
+ opencv_keypoint_to_affine_shape,
42
+ )
sfmtool/_align.py ADDED
@@ -0,0 +1,226 @@
1
+ # Copyright The SfM Tool Authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """Core alignment functionality for multiple SfM reconstructions."""
5
+
6
+ from dataclasses import dataclass
7
+
8
+ import numpy as np
9
+
10
+ from ._sfmtool import RotQuaternion, Se3Transform
11
+
12
+
13
+ @dataclass
14
+ class ImageMatch:
15
+ """Information about a matched image between two reconstructions."""
16
+
17
+ image_name: str
18
+ source_index: int
19
+ target_index: int
20
+ source_quat: RotQuaternion
21
+ source_camera_center: np.ndarray
22
+ target_quat: RotQuaternion
23
+ target_camera_center: np.ndarray
24
+ quality: float = 1.0
25
+
26
+
27
+ class AlignmentResult:
28
+ """Result of aligning one reconstruction to another."""
29
+
30
+ def __init__(
31
+ self,
32
+ source_id: str,
33
+ target_id: str,
34
+ transform: Se3Transform,
35
+ matches: list[ImageMatch],
36
+ ):
37
+ self.source_id = source_id
38
+ self.target_id = target_id
39
+ self.transform = transform
40
+ self.matches = matches
41
+ self._compute_residuals()
42
+
43
+ def _compute_residuals(self):
44
+ """Compute per-image residuals for matched poses."""
45
+ self.per_image_errors = []
46
+ self.total_rms_error = 0.0
47
+
48
+ if not self.matches:
49
+ return
50
+
51
+ errors = []
52
+ for match in self.matches:
53
+ source_pos = match.source_camera_center
54
+ source_rot = match.source_quat
55
+
56
+ aligned_pos = self.transform @ source_pos
57
+ aligned_rot_matrix = (
58
+ self.transform.rotation.to_rotation_matrix()
59
+ @ source_rot.to_rotation_matrix()
60
+ )
61
+
62
+ trans_error = np.linalg.norm(aligned_pos - match.target_camera_center)
63
+ rot_matrix_error = (
64
+ match.target_quat.to_rotation_matrix() - aligned_rot_matrix
65
+ )
66
+ rot_error = np.linalg.norm(rot_matrix_error) / np.sqrt(3)
67
+
68
+ combined_error = trans_error + 0.1 * rot_error
69
+ errors.append(combined_error)
70
+ self.per_image_errors.append(combined_error)
71
+
72
+ self.total_rms_error = np.sqrt(np.mean(np.array(errors) ** 2))
73
+ self.confidence = max(0.0, 1.0 - self.total_rms_error / 0.1)
74
+
75
+
76
+ def estimate_similarity_with_orientations(
77
+ matches: list[ImageMatch],
78
+ weights: np.ndarray | None = None,
79
+ ) -> Se3Transform:
80
+ """Estimate similarity transform using both camera positions AND orientations.
81
+
82
+ Args:
83
+ matches: List of matched cameras with positions and orientations
84
+ weights: Optional per-match weights (default: uniform)
85
+
86
+ Returns:
87
+ Se3Transform representing the estimated similarity transform
88
+ """
89
+ n = len(matches)
90
+ if n < 1:
91
+ raise ValueError("Need at least 1 match")
92
+
93
+ if weights is None:
94
+ weights = np.ones(n)
95
+ weights = weights / np.sum(weights)
96
+
97
+ # Step 1: Estimate rotation from camera orientations
98
+ if n == 1:
99
+ match = matches[0]
100
+ R_s = match.source_quat.to_rotation_matrix()
101
+ R_t = match.target_quat.to_rotation_matrix()
102
+ rotation_matrix = R_t.T @ R_s
103
+ else:
104
+ # Multiple matches: weighted quaternion averaging
105
+ rotation_quats = []
106
+ for match in matches:
107
+ R_s = match.source_quat.to_rotation_matrix()
108
+ R_t = match.target_quat.to_rotation_matrix()
109
+ R_rel = R_t.T @ R_s
110
+ q_rel = RotQuaternion.from_rotation_matrix(R_rel)
111
+ rotation_quats.append(q_rel)
112
+
113
+ # Weighted quaternion averaging (Markley et al.)
114
+ Q = np.zeros((4, 4))
115
+ for q, w in zip(rotation_quats, weights):
116
+ q_array = np.asarray(q.to_wxyz_array())
117
+ Q += w * np.outer(q_array, q_array)
118
+
119
+ eigenvalues, eigenvectors = np.linalg.eigh(Q)
120
+ avg_quat_array = eigenvectors[:, -1]
121
+ avg_quat = RotQuaternion.from_wxyz_array(avg_quat_array)
122
+ rotation_matrix = avg_quat.to_rotation_matrix()
123
+
124
+ # Step 2: Given rotation R, solve for scale s and translation t
125
+ source_positions = np.array([m.source_camera_center for m in matches])
126
+ target_positions = np.array([m.target_camera_center for m in matches])
127
+
128
+ source_rotated = source_positions @ rotation_matrix.T
129
+ source_rotated_centroid = np.average(source_rotated, axis=0, weights=weights)
130
+ target_centroid = np.average(target_positions, axis=0, weights=weights)
131
+
132
+ source_rotated_centered = source_rotated - source_rotated_centroid
133
+ target_centered = target_positions - target_centroid
134
+
135
+ numerator = np.sum(
136
+ weights[:, np.newaxis] * target_centered * source_rotated_centered
137
+ )
138
+ denominator = np.sum(
139
+ weights[:, np.newaxis] * source_rotated_centered * source_rotated_centered
140
+ )
141
+
142
+ if denominator <= 0:
143
+ scale = 1.0
144
+ else:
145
+ scale = numerator / denominator
146
+
147
+ translation = target_centroid - scale * source_rotated_centroid
148
+ rotation_quat = RotQuaternion.from_rotation_matrix(rotation_matrix)
149
+ return Se3Transform(rotation=rotation_quat, translation=translation, scale=scale)
150
+
151
+
152
+ def estimate_pairwise_alignment(
153
+ matches: list[ImageMatch],
154
+ confidence_threshold: float = 0.7,
155
+ source_id: str | None = None,
156
+ target_id: str | None = None,
157
+ ) -> AlignmentResult:
158
+ """Estimate SE(3) alignment between two reconstructions.
159
+
160
+ Args:
161
+ matches: List of matched images between reconstructions
162
+ confidence_threshold: Minimum quality score to include match
163
+ source_id: ID of source reconstruction
164
+ target_id: ID of target reconstruction
165
+
166
+ Returns:
167
+ AlignmentResult with estimated transform
168
+ """
169
+ if not matches:
170
+ raise ValueError("No matches provided for alignment")
171
+
172
+ valid_matches = [m for m in matches if m.quality >= confidence_threshold]
173
+ if not valid_matches:
174
+ raise ValueError(
175
+ f"No matches above confidence threshold {confidence_threshold}"
176
+ )
177
+
178
+ if len(valid_matches) == 1:
179
+ match = valid_matches[0]
180
+ source_quat = match.source_quat
181
+ target_quat = match.target_quat
182
+ rotation_quat = target_quat.conjugate() * source_quat
183
+ rotation_matrix = rotation_quat.to_rotation_matrix()
184
+
185
+ source_pos = np.array(match.source_camera_center)
186
+ target_pos = np.array(match.target_camera_center)
187
+ translation = target_pos - rotation_matrix @ source_pos
188
+ scale = 1.0
189
+
190
+ transform = Se3Transform(
191
+ rotation=rotation_quat, translation=translation, scale=scale
192
+ )
193
+ else:
194
+ weights = np.array([m.quality for m in valid_matches])
195
+ transform = estimate_similarity_with_orientations(valid_matches, weights)
196
+
197
+ if source_id is None:
198
+ source_id = "unknown_source"
199
+ if target_id is None:
200
+ target_id = "unknown_target"
201
+
202
+ return AlignmentResult(source_id, target_id, transform, valid_matches)
203
+
204
+
205
+ def kabsch_algorithm(
206
+ source_points: np.ndarray,
207
+ target_points: np.ndarray,
208
+ ) -> Se3Transform:
209
+ """Kabsch algorithm for finding optimal SE(3) transformation.
210
+
211
+ Finds the rotation, translation, and scale that best aligns
212
+ source_points to target_points.
213
+
214
+ Args:
215
+ source_points: (N, 3) array of source points
216
+ target_points: (N, 3) array of target points
217
+
218
+ Returns:
219
+ Se3Transform representing the optimal similarity transform
220
+ """
221
+ if source_points.shape != target_points.shape or source_points.shape[0] < 2:
222
+ raise ValueError("Need at least 2 matching points with same shape")
223
+
224
+ from ._sfmtool import kabsch_algorithm_rs
225
+
226
+ return kabsch_algorithm_rs(source_points, target_points)
@@ -0,0 +1,74 @@
1
+ # Copyright The SfM Tool Authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """Camera-based alignment for SfM reconstructions."""
5
+
6
+ import numpy as np
7
+
8
+ from ._align import ImageMatch
9
+ from ._sfmtool import RotQuaternion, SfmrReconstruction
10
+
11
+
12
+ def get_reconstruction_poses(
13
+ recon: SfmrReconstruction,
14
+ ) -> tuple[dict[int, RotQuaternion], dict[int, np.ndarray]]:
15
+ """Get camera poses from a SfmrReconstruction.
16
+
17
+ Returns:
18
+ (quaternions_dict, camera_centers_dict) mapping image indices to poses.
19
+ Camera centers are the actual camera positions in world coordinates.
20
+ """
21
+ quaternions = {}
22
+ camera_centers = {}
23
+
24
+ for i, (quat_wxyz, trans) in enumerate(
25
+ zip(recon.quaternions_wxyz, recon.translations)
26
+ ):
27
+ quat = RotQuaternion.from_wxyz_array(quat_wxyz)
28
+ quaternions[i] = quat
29
+ camera_centers[i] = quat.camera_center(trans)
30
+
31
+ return quaternions, camera_centers
32
+
33
+
34
+ def build_image_matches(
35
+ shared_images: set[str],
36
+ source_images: dict[int, str],
37
+ source_quaternions: dict[int, RotQuaternion],
38
+ source_camera_centers: dict[int, np.ndarray],
39
+ target_images: dict[int, str],
40
+ target_quaternions: dict[int, RotQuaternion],
41
+ target_camera_centers: dict[int, np.ndarray],
42
+ ) -> list[ImageMatch]:
43
+ """Build ImageMatch objects for shared images between two reconstructions."""
44
+ matches = []
45
+
46
+ for img_name in shared_images:
47
+ source_idx = None
48
+ for idx, name in source_images.items():
49
+ if name == img_name:
50
+ source_idx = idx
51
+ break
52
+
53
+ target_idx = None
54
+ for idx, name in target_images.items():
55
+ if name == img_name:
56
+ target_idx = idx
57
+ break
58
+
59
+ if source_idx is None or target_idx is None:
60
+ continue
61
+
62
+ match = ImageMatch(
63
+ image_name=img_name,
64
+ source_index=source_idx,
65
+ target_index=target_idx,
66
+ source_quat=source_quaternions[source_idx],
67
+ source_camera_center=source_camera_centers[source_idx],
68
+ target_quat=target_quaternions[target_idx],
69
+ target_camera_center=target_camera_centers[target_idx],
70
+ quality=0.95,
71
+ )
72
+ matches.append(match)
73
+
74
+ return matches
@@ -0,0 +1,201 @@
1
+ # Copyright The SfM Tool Authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """Point-based alignment for SfM reconstructions."""
5
+
6
+ import numpy as np
7
+
8
+ from ._align import AlignmentResult, kabsch_algorithm
9
+ from ._point_correspondence import find_point_correspondences
10
+ from ._sfmtool import SfmrReconstruction
11
+
12
+
13
+ def estimate_alignment_from_points(
14
+ source_recon: SfmrReconstruction,
15
+ target_recon: SfmrReconstruction,
16
+ shared_images: list[tuple[int, int]],
17
+ min_points: int = 10,
18
+ use_ransac: bool = True,
19
+ ransac_iterations: int = 1000,
20
+ ransac_percentile: float = 95.0,
21
+ ) -> AlignmentResult:
22
+ """Estimate SE3 transform by aligning corresponding 3D points.
23
+
24
+ Finds 3D points that appear in both reconstructions (via shared feature
25
+ observations), then computes the optimal similarity transform using
26
+ the Kabsch algorithm with optional RANSAC outlier rejection.
27
+
28
+ Args:
29
+ source_recon: Source SfmrReconstruction object
30
+ target_recon: Target SfmrReconstruction object
31
+ shared_images: List of (source_img_idx, target_img_idx) pairs
32
+ min_points: Minimum number of point correspondences required
33
+ use_ransac: Apply RANSAC for outlier rejection
34
+ ransac_iterations: Number of RANSAC iterations
35
+ ransac_percentile: Percentile of correspondence distances to use as RANSAC
36
+ threshold (default: 95.0).
37
+
38
+ Returns:
39
+ AlignmentResult with transform from source to target
40
+ """
41
+ correspondences, source_positions, target_positions = find_point_correspondences(
42
+ source_recon, target_recon, shared_images
43
+ )
44
+
45
+ n_points = len(source_positions)
46
+ if n_points < min_points:
47
+ raise ValueError(
48
+ f"Insufficient point correspondences: found {n_points}, "
49
+ f"need at least {min_points}. "
50
+ f"This may indicate incompatible reconstructions or "
51
+ f"insufficient shared features."
52
+ )
53
+
54
+ computed_threshold = None
55
+ distances_for_stats = None
56
+
57
+ if use_ransac and n_points > min_points:
58
+ prelim_transform = kabsch_algorithm(source_positions, target_positions)
59
+ transformed = prelim_transform.apply_to_points(source_positions)
60
+ distances_for_stats = np.linalg.norm(transformed - target_positions, axis=1)
61
+
62
+ computed_threshold = float(
63
+ np.percentile(distances_for_stats, ransac_percentile)
64
+ )
65
+
66
+ from ._sfmtool import ransac_alignment_rs
67
+
68
+ inlier_mask = ransac_alignment_rs(
69
+ source_positions,
70
+ target_positions,
71
+ max_iterations=ransac_iterations,
72
+ threshold=computed_threshold,
73
+ seed=42,
74
+ )
75
+ source_positions = source_positions[inlier_mask]
76
+ target_positions = target_positions[inlier_mask]
77
+
78
+ n_inliers = np.sum(inlier_mask)
79
+
80
+ if n_inliers < min_points:
81
+ raise ValueError(
82
+ f"RANSAC rejected too many points: {n_inliers} inliers "
83
+ f"remaining (rejected {n_points - n_inliers} outliers), "
84
+ f"need at least {min_points}."
85
+ )
86
+ else:
87
+ n_inliers = n_points
88
+
89
+ transform = kabsch_algorithm(source_positions, target_positions)
90
+
91
+ transformed_source = transform.apply_to_points(source_positions)
92
+ distances = np.linalg.norm(transformed_source - target_positions, axis=1)
93
+ rms_error = np.sqrt(np.mean(distances**2))
94
+
95
+ result = AlignmentResult(
96
+ source_id="source",
97
+ target_id="target",
98
+ transform=transform,
99
+ matches=[],
100
+ )
101
+
102
+ result.total_rms_error = float(rms_error)
103
+ result.confidence = max(0.0, 1.0 - rms_error / 0.1)
104
+ result.n_point_correspondences = n_points
105
+ result.n_inliers = n_inliers
106
+ result.point_rms_error = float(rms_error)
107
+ result.computed_threshold = computed_threshold
108
+ result.ransac_percentile = ransac_percentile
109
+ result.distances_for_stats = distances_for_stats
110
+
111
+ return result
112
+
113
+
114
+ def estimate_alignment_from_points_with_logging(
115
+ source_recon: SfmrReconstruction,
116
+ target_recon: SfmrReconstruction,
117
+ shared_images: list[tuple[int, int]],
118
+ min_points: int = 10,
119
+ use_ransac: bool = True,
120
+ ransac_iterations: int = 1000,
121
+ ransac_percentile: float = 95.0,
122
+ verbose: bool = True,
123
+ ) -> AlignmentResult:
124
+ """Estimate alignment with progress logging.
125
+
126
+ Wrapper around estimate_alignment_from_points that provides
127
+ detailed logging for CLI usage.
128
+ """
129
+ import click
130
+
131
+ if verbose:
132
+ click.echo(" Finding 3D point correspondences...")
133
+
134
+ result = estimate_alignment_from_points(
135
+ source_recon=source_recon,
136
+ target_recon=target_recon,
137
+ shared_images=shared_images,
138
+ min_points=min_points,
139
+ use_ransac=use_ransac,
140
+ ransac_iterations=ransac_iterations,
141
+ ransac_percentile=ransac_percentile,
142
+ )
143
+
144
+ if verbose:
145
+ click.echo(f" Found {result.n_point_correspondences} point correspondences")
146
+
147
+ if use_ransac and result.distances_for_stats is not None:
148
+ from ._histogram_utils import print_histogram
149
+
150
+ distances = result.distances_for_stats
151
+ click.echo("\n Correspondence distance statistics:")
152
+ click.echo(f" Min: {np.min(distances):.6f}")
153
+ click.echo(f" Max: {np.max(distances):.6f}")
154
+ click.echo(f" Mean: {np.mean(distances):.6f}")
155
+ click.echo(f" Median: {np.median(distances):.6f}")
156
+
157
+ percentiles = [50, 75, 90, 95, 99]
158
+ click.echo("\n Percentiles:")
159
+ for p in percentiles:
160
+ val = np.percentile(distances, p)
161
+ marker = (
162
+ " <-- threshold" if abs(p - result.ransac_percentile) < 0.1 else ""
163
+ )
164
+ click.echo(f" {p:3d}th: {val:.6f}{marker}")
165
+
166
+ click.echo()
167
+ print_histogram(
168
+ distances,
169
+ title="Distance distribution",
170
+ num_buckets=60,
171
+ show_stats=False,
172
+ )
173
+
174
+ if result.computed_threshold is not None:
175
+ click.echo(
176
+ f"\n Using {result.ransac_percentile:.1f}th percentile as threshold: {result.computed_threshold:.6f}"
177
+ )
178
+ click.echo()
179
+
180
+ if use_ransac:
181
+ n_outliers = result.n_point_correspondences - result.n_inliers
182
+ inlier_ratio = (
183
+ result.n_inliers / result.n_point_correspondences
184
+ if result.n_point_correspondences > 0
185
+ else 0
186
+ )
187
+ click.echo(f" RANSAC: {result.n_inliers} inliers, {n_outliers} outliers")
188
+ click.echo(f" Inlier ratio: {inlier_ratio * 100:.1f}%")
189
+
190
+ if inlier_ratio < 0.5:
191
+ click.echo(
192
+ click.style(
193
+ f" WARNING: Low inlier ratio ({inlier_ratio * 100:.1f}%)! "
194
+ "Alignment quality may be poor.",
195
+ fg="yellow",
196
+ bold=True,
197
+ )
198
+ )
199
+ click.echo(f" Point RMS error: {result.point_rms_error:.4f}")
200
+
201
+ return result
@@ -0,0 +1,82 @@
1
+ # Copyright The SfM Tool Authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """Camera model inference and descriptor wrapping for COLMAP database setup."""
5
+
6
+ from pathlib import Path
7
+
8
+ import numpy as np
9
+ import pycolmap
10
+
11
+
12
+ def _wrap_descriptors(descriptors):
13
+ """Wrap numpy descriptors as FeatureDescriptors for pycolmap 4.x."""
14
+ if isinstance(descriptors, np.ndarray):
15
+ return pycolmap.FeatureDescriptors(
16
+ data=descriptors, type=pycolmap.FeatureExtractorType.SIFT
17
+ )
18
+ return descriptors
19
+
20
+
21
+ def _infer_camera(
22
+ image_path: str | Path, camera_model: str | None = None
23
+ ) -> pycolmap.Camera:
24
+ """Infer a camera from an image, optionally overriding the camera model."""
25
+ cam = pycolmap.infer_camera_from_image(str(image_path))
26
+ if camera_model is not None:
27
+ target_model = getattr(pycolmap.CameraModelId, camera_model.upper())
28
+ # Build new params for the target model using the inferred focal length and principal point
29
+ old_params = cam.params
30
+ if cam.model.name in (
31
+ "SIMPLE_PINHOLE",
32
+ "SIMPLE_RADIAL",
33
+ "SIMPLE_RADIAL_FISHEYE",
34
+ ):
35
+ f, cx, cy = old_params[0], old_params[1], old_params[2]
36
+ fx, fy = f, f
37
+ else:
38
+ fx, fy, cx, cy = old_params[0], old_params[1], old_params[2], old_params[3]
39
+
40
+ # For fisheye models, use a more appropriate initial focal length.
41
+ # The default EXIF-based estimate (1.2x image size) assumes rectilinear projection,
42
+ # which is far too large for equidistant fisheye lenses.
43
+ # A ~190 deg FOV equidistant fisheye has f ~ image_size / pi.
44
+ if target_model.name in (
45
+ "OPENCV_FISHEYE",
46
+ "SIMPLE_RADIAL_FISHEYE",
47
+ "RADIAL_FISHEYE",
48
+ "THIN_PRISM_FISHEYE",
49
+ "RAD_TAN_THIN_PRISM_FISHEYE",
50
+ ):
51
+ fisheye_f = min(cam.width, cam.height) / 3.14159
52
+ fx, fy = fisheye_f, fisheye_f
53
+
54
+ new_cam = pycolmap.Camera()
55
+ new_cam.model = target_model
56
+ new_cam.width = cam.width
57
+ new_cam.height = cam.height
58
+
59
+ model_name = target_model.name
60
+ if model_name in ("SIMPLE_RADIAL_FISHEYE", "SIMPLE_RADIAL"):
61
+ new_cam.params = [fx, cx, cy, 0]
62
+ elif model_name in ("RADIAL_FISHEYE", "RADIAL"):
63
+ new_cam.params = [fx, cx, cy, 0, 0]
64
+ elif model_name == "OPENCV_FISHEYE":
65
+ new_cam.params = [fx, fy, cx, cy, 0, 0, 0, 0]
66
+ elif model_name == "OPENCV":
67
+ new_cam.params = [fx, fy, cx, cy, 0, 0, 0, 0]
68
+ elif model_name == "SIMPLE_PINHOLE":
69
+ new_cam.params = [fx, cx, cy]
70
+ elif model_name == "PINHOLE":
71
+ new_cam.params = [fx, fy, cx, cy]
72
+ elif model_name == "THIN_PRISM_FISHEYE":
73
+ new_cam.params = [fx, fy, cx, cy, 0, 0, 0, 0, 0, 0, 0, 0]
74
+ elif model_name == "RAD_TAN_THIN_PRISM_FISHEYE":
75
+ new_cam.params = [fx, fy, cx, cy, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
76
+ elif model_name == "FULL_OPENCV":
77
+ new_cam.params = [fx, fy, cx, cy, 0, 0, 0, 0, 0, 0, 0, 0]
78
+ else:
79
+ raise ValueError(f"Unsupported camera model: {model_name}")
80
+
81
+ cam = new_cam
82
+ return cam