Debian Codegraph @

benchmark/reconstruction/evaluation/covisibility_test.py

raw
1 # Copyright (c), ETH Zurich and UNC Chapel Hill.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 #
14 # * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 # its contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30 import numpy as np
31
32 import pycolmap
33
34 from .covisibility import (
35 Frustum,
36 _build_image_point3D_sets,
37 _estimate_depth_ranges,
38 _is_pair_covisible_by_frustum,
39 _sample_frustum_points,
40 )
41
42
43 def _make_camera() -> pycolmap.Camera:
44 cam = pycolmap.Camera()
45 cam.model = pycolmap.CameraModelId.PINHOLE
46 cam.width = 640
47 cam.height = 480
48 cam.params = [500, 500, 320, 240]
49 cam.camera_id = 1
50 return cam
51
52
53 def _add_image(
54 recon: pycolmap.Reconstruction,
55 image_id: int,
56 name: str,
57 position: np.ndarray,
58 rotation: pycolmap.Rotation3d | None = None,
59 num_points2D: int = 0,
60 ) -> None:
61 if rotation is None:
62 rotation = pycolmap.Rotation3d()
63 translation = -rotation.matrix() @ position
64
65 points2d = [pycolmap.Point2D() for _ in range(num_points2D)]
66 for i, p in enumerate(points2d):
67 p.xy = np.array([100.0 + i * 10, 200.0])
68
69 frame = pycolmap.Frame()
70 frame.rig_id = 1
71 frame.frame_id = image_id
72
73 img = pycolmap.Image()
74 img.name = name
75 img.camera_id = 1
76 img.frame_id = image_id
77 img.image_id = image_id
78 if num_points2D > 0:
79 img.points2D = pycolmap.Point2DList(points2d)
80
81 frame.add_data_id(img.data_id)
82 frame.rig_from_world = pycolmap.Rigid3d(rotation, translation)
83 recon.add_frame(frame)
84 recon.add_image(img)
85
86
87 class TestEstimateDepthRanges:
88 def test_single_image_with_points(self):
89 recon = pycolmap.Reconstruction()
90 cam = _make_camera()
91 recon.add_camera_with_trivial_rig(cam)
92 _add_image(recon, 1, "img1.jpg", np.array([0, 0, 0]), num_points2D=20)
93
94 depths = []
95 for i in range(20):
96 depth = 2.0 + i * 0.5
97 depths.append(depth)
98 recon.add_point3D(
99 np.array([0.0, 0.0, depth]),
100 pycolmap.Track([pycolmap.TrackElement(1, i)]),
101 )
102
103 ranges = _estimate_depth_ranges(recon)
104 assert 1 in ranges
105 near, far = ranges[1]
106 np.testing.assert_almost_equal(near, np.percentile(depths, 2.0))
107 np.testing.assert_almost_equal(far, np.percentile(depths, 98.0))
108
109 def test_insufficient_points_returns_default(self):
110 recon = pycolmap.Reconstruction()
111 cam = _make_camera()
112 recon.add_camera_with_trivial_rig(cam)
113 _add_image(recon, 1, "img1.jpg", np.array([0, 0, 0]), num_points2D=5)
114
115 for i in range(5):
116 recon.add_point3D(
117 np.array([0.0, 0.0, float(i + 1)]),
118 pycolmap.Track([pycolmap.TrackElement(1, i)]),
119 )
120
121 ranges = _estimate_depth_ranges(recon)
122 assert ranges[1] == (0.1, 100.0)
123
124 def test_per_image_ranges_differ(self):
125 recon = pycolmap.Reconstruction()
126 cam = _make_camera()
127 recon.add_camera_with_trivial_rig(cam)
128 _add_image(recon, 1, "img1.jpg", np.array([0, 0, 0]), num_points2D=20)
129 _add_image(recon, 2, "img2.jpg", np.array([0, 0, 100]), num_points2D=20)
130
131 # Points near image 1 (depths 1-10 from image 1)
132 for i in range(20):
133 depth = 1.0 + i * 0.5
134 recon.add_point3D(
135 np.array([0.0, 0.0, depth]),
136 pycolmap.Track([pycolmap.TrackElement(1, i)]),
137 )
138
139 # Points near image 2 (depths 1-10 from image 2, so z ~ 90-99)
140 for i in range(20):
141 depth = 1.0 + i * 0.5
142 recon.add_point3D(
143 np.array([0.0, 0.0, 100.0 + depth]),
144 pycolmap.Track([pycolmap.TrackElement(2, i)]),
145 )
146
147 ranges = _estimate_depth_ranges(recon)
148 near1, far1 = ranges[1]
149 near2, far2 = ranges[2]
150 # Image 1 sees nearby points, image 2 sees distant points
151 assert near1 < 15
152 assert near2 < 15
153
154
155 class TestSampleFrustumPoints:
156 def test_identity_camera_shape(self):
157 recon = pycolmap.Reconstruction()
158 cam = _make_camera()
159 recon.add_camera_with_trivial_rig(cam)
160 _add_image(recon, 1, "img.jpg", np.array([0, 0, 0]))
161
162 verts = _sample_frustum_points(recon.images[1], cam, 1.0, 10.0)
163 # num_steps=5 default: 5*5 grid at 5 depths = 125 points
164 assert verts.shape == (125, 3)
165
166 def test_custom_num_steps(self):
167 recon = pycolmap.Reconstruction()
168 cam = _make_camera()
169 recon.add_camera_with_trivial_rig(cam)
170 _add_image(recon, 1, "img.jpg", np.array([0, 0, 0]))
171
172 verts = _sample_frustum_points(
173 recon.images[1], cam, 1.0, 10.0, num_steps=3
174 )
175 assert verts.shape == (27, 3)
176
177 def test_vertices_depth_range(self):
178 recon = pycolmap.Reconstruction()
179 cam = _make_camera()
180 recon.add_camera_with_trivial_rig(cam)
181 # Identity pose: camera at origin looking +z
182 _add_image(recon, 1, "img.jpg", np.array([0, 0, 0]))
183
184 near, far = 2.0, 20.0
185 verts = _sample_frustum_points(recon.images[1], cam, near, far)
186 # In world coords, z should span [near, far] for identity pose
187 np.testing.assert_almost_equal(verts[:, 2].min(), near)
188 np.testing.assert_almost_equal(verts[:, 2].max(), far)
189
190 def test_translated_camera(self):
191 recon = pycolmap.Reconstruction()
192 cam = _make_camera()
193 recon.add_camera_with_trivial_rig(cam)
194 offset = np.array([10.0, 20.0, 30.0])
195 _add_image(recon, 1, "img.jpg", offset)
196
197 verts = _sample_frustum_points(
198 recon.images[1], cam, 1.0, 10.0, num_steps=2
199 )
200 # Center of near plane should be at camera position + [0,0,near]
201 center = verts.mean(axis=0)
202 np.testing.assert_allclose(center[:2], offset[:2], atol=1e-10)
203 assert center[2] > offset[2]
204
205
206 class TestCheckFrustumCovisibility:
207 def _build_two_camera_recon(
208 self, pos1, pos2, rot1=None, rot2=None
209 ) -> pycolmap.Reconstruction:
210 recon = pycolmap.Reconstruction()
211 cam = _make_camera()
212 recon.add_camera_with_trivial_rig(cam)
213 _add_image(recon, 1, "img1.jpg", np.array(pos1), rot1)
214 _add_image(recon, 2, "img2.jpg", np.array(pos2), rot2)
215 return recon
216
217 def _check(self, recon, max_angle=90.0, near=1.0, far=100.0):
218 cam = recon.cameras[1]
219 imgs = [recon.images[1], recon.images[2]]
220 frustums = {
221 img.image_id: Frustum(
222 points=_sample_frustum_points(img, cam, near, far),
223 depth_range=(near, far),
224 )
225 for img in imgs
226 }
227 return _is_pair_covisible_by_frustum(
228 imgs[0], imgs[1], recon, frustums, max_angle
229 )
230
231 def test_covisible_side_by_side(self):
232 recon = self._build_two_camera_recon([-0.5, 0, -5], [0.5, 0, -5])
233 assert self._check(recon)
234
235 def test_not_covisible_opposite_directions(self):
236 rot_180_y = pycolmap.Rotation3d(np.array([0, 1, 0, 0]))
237 recon = self._build_two_camera_recon(
238 [0, 0, -5], [0, 0, 5], rot2=rot_180_y
239 )
240 assert not self._check(recon)
241
242 def test_rejected_by_viewing_angle(self):
243 # 90 deg rotation around y-axis
244 rot = pycolmap.Rotation3d(
245 np.array([0, 1 / np.sqrt(2), 0, 1 / np.sqrt(2)])
246 )
247 recon = self._build_two_camera_recon([0, 0, -5], [5, 0, -5], rot2=rot)
248 # Viewing angle is 90 deg, threshold is 45 -> should be rejected
249 assert not self._check(recon, max_angle=45.0)
250
251 def test_covisible_converging_cameras(self):
252 # Two cameras angled inward looking at the same point
253 # Camera 1 at (-2,0,0) looking at +x (toward origin)
254 rot1 = pycolmap.Rotation3d(
255 np.array([0, -1 / np.sqrt(2), 0, 1 / np.sqrt(2)])
256 )
257 # Camera 2 at (2,0,0) looking at -x (toward origin)
258 rot2 = pycolmap.Rotation3d(
259 np.array([0, 1 / np.sqrt(2), 0, 1 / np.sqrt(2)])
260 )
261 recon = self._build_two_camera_recon(
262 [-5, 0, 0], [5, 0, 0], rot1=rot1, rot2=rot2
263 )
264 assert self._check(recon, max_angle=180.0)
265
266 def test_not_covisible_far_apart_narrow_fov(self):
267 # Two cameras far apart, both looking +z, with small frustum depth
268 recon = self._build_two_camera_recon([0, 0, 0], [1000, 0, 0])
269 assert self._check(recon, near=1.0, far=2.0) is False
270
271 def test_not_covisible_depth_out_of_range(self):
272 # Two cameras side by side looking +z. Their frustums project into
273 # each other's images, but the depth ranges don't overlap: camera 1
274 # expects depths 1-5 while camera 2 expects 50-100. Each camera's
275 # frustum points land outside the other's depth range.
276 recon = self._build_two_camera_recon([-0.5, 0, 0], [0.5, 0, 0])
277 cam = recon.cameras[1]
278 img1, img2 = recon.images[1], recon.images[2]
279 frustums = {
280 img1.image_id: Frustum(
281 points=_sample_frustum_points(img1, cam, 1.0, 5.0),
282 depth_range=(1.0, 5.0),
283 ),
284 img2.image_id: Frustum(
285 points=_sample_frustum_points(img2, cam, 50.0, 100.0),
286 depth_range=(50.0, 100.0),
287 ),
288 }
289 assert not _is_pair_covisible_by_frustum(
290 img1, img2, recon, frustums, 90.0
291 )
292 # Sanity: with matching depth ranges, these cameras are covisible.
293 assert self._check(recon, near=1.0, far=100.0)
294
295 def test_covisible_identical_cameras(self):
296 recon = self._build_two_camera_recon([0, 0, 0], [0, 0, 0])
297 assert self._check(recon)
298
299
300 def _make_recon_with_tracks(
301 num_images: int,
302 num_points2D_per_image: int,
303 tracks: list[list[tuple[int, int]]],
304 ) -> pycolmap.Reconstruction:
305 """Build a reconstruction with images and 3D point tracks.
306
307 Args:
308 num_images: Number of images to create.
309 num_points2D_per_image: Number of 2D points per image.
310 tracks: List of tracks, where each track is a list of
311 (image_id, point2D_idx) tuples.
312 """
313 recon = pycolmap.Reconstruction()
314 cam = _make_camera()
315 recon.add_camera_with_trivial_rig(cam)
316
317 for i in range(1, num_images + 1):
318 _add_image(
319 recon,
320 i,
321 f"img{i}.jpg",
322 np.array([i * 2.0, 0, 0]),
323 num_points2D=num_points2D_per_image,
324 )
325
326 for track in tracks:
327 xyz = np.array([0.0, 0.0, 5.0])
328 elements = [pycolmap.TrackElement(img_id, idx) for img_id, idx in track]
329 recon.add_point3D(xyz, pycolmap.Track(elements))
330
331 return recon
332
333
334 class TestBuildImagePoint3DSets:
335 def test_basic_tracks(self):
336 # 2 images, 5 points2D each, 3 shared tracks
337 recon = _make_recon_with_tracks(
338 num_images=2,
339 num_points2D_per_image=5,
340 tracks=[
341 [(1, 0), (2, 0)],
342 [(1, 1), (2, 1)],
343 [(1, 2), (2, 2)],
344 ],
345 )
346 sets = _build_image_point3D_sets(recon)
347 assert len(sets[1]) == 3
348 assert len(sets[2]) == 3
349 assert len(sets[1] & sets[2]) == 3
350
351 def test_no_shared_tracks(self):
352 recon = _make_recon_with_tracks(
353 num_images=2,
354 num_points2D_per_image=5,
355 tracks=[
356 [(1, 0)],
357 [(1, 1)],
358 [(2, 0)],
359 [(2, 1)],
360 ],
361 )
362 sets = _build_image_point3D_sets(recon)
363 assert len(sets[1] & sets[2]) == 0
364
365 def test_no_tracks(self):
366 recon = _make_recon_with_tracks(
367 num_images=2,
368 num_points2D_per_image=5,
369 tracks=[],
370 )
371 sets = _build_image_point3D_sets(recon)
372 assert len(sets[1]) == 0
373 assert len(sets[2]) == 0
374
375 def test_partial_overlap(self):
376 # 3 images: img1-img2 share 5 points
377 # img2-img3 share 2, img1-img3 share 0
378 recon = _make_recon_with_tracks(
379 num_images=3,
380 num_points2D_per_image=10,
381 tracks=[
382 [(1, 0), (2, 0)],
383 [(1, 1), (2, 1)],
384 [(1, 2), (2, 2)],
385 [(1, 3), (2, 3)],
386 [(1, 4), (2, 4)],
387 [(2, 5), (3, 0)],
388 [(2, 6), (3, 1)],
389 ],
390 )
391 sets = _build_image_point3D_sets(recon)
392 assert len(sets[1] & sets[2]) == 5
393 assert len(sets[2] & sets[3]) == 2
394 assert len(sets[1] & sets[3]) == 0