Skip to content

Commit 5c9fe5b

Browse files
committed
Cache compute camera mapping
1 parent 90f52f0 commit 5c9fe5b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

opensfm/undistort.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def undistort_reconstruction(
7777

7878
return undistorted_shots
7979

80+
_camera_mapping_cache = {}
8081

8182
def undistort_reconstruction_with_images(
8283
tracks_manager: Optional[pymap.TracksManager],
@@ -86,6 +87,9 @@ def undistort_reconstruction_with_images(
8687
imageFilter: Callable[[str, np.ndarray], np.ndarray] = None,
8788
skip_images: bool = False,
8889
) -> Dict[pymap.Shot, List[pymap.Shot]]:
90+
global _camera_mapping_cache
91+
_camera_mapping_cache = {}
92+
8993
undistorted_shots = undistort_reconstruction(
9094
tracks_manager, reconstruction, data, udata
9195
)
@@ -127,7 +131,7 @@ def undistort_image_and_masks(arguments) -> None:
127131
# Undistort image
128132
image = data.load_image(shot.id, unchanged=True, anydepth=True)
129133
if image is not None:
130-
if imageFilter is not None:
134+
if hasattr(imageFilter, '__call__'):
131135
image = imageFilter(shot.id, image)
132136
undistorted = undistort_image(
133137
shot, undistorted_shots, image, cv2.INTER_AREA, max_size
@@ -154,6 +158,20 @@ def undistort_image_and_masks(arguments) -> None:
154158
udata.save_undistorted_segmentation(k, v)
155159

156160

161+
def compute_camera_mapping_cached(camera, new_camera, width, height):
162+
global _camera_mapping_cache
163+
key = "%s-%s-%s-%s" % (camera.id, new_camera.id, width, height)
164+
165+
if key in _camera_mapping_cache:
166+
return _camera_mapping_cache[key]
167+
168+
map1, map2 = pygeometry.compute_camera_mapping(
169+
camera, new_camera, width, height
170+
)
171+
_camera_mapping_cache[key] = (map1, map2)
172+
return _camera_mapping_cache[key]
173+
174+
157175
def undistort_image(
158176
shot: pymap.Shot,
159177
undistorted_shots: List[pymap.Shot],
@@ -180,7 +198,7 @@ def undistort_image(
180198
[undistorted_shot] = undistorted_shots
181199
new_camera = undistorted_shot.camera
182200
height, width = original.shape[:2]
183-
map1, map2 = pygeometry.compute_camera_mapping(
201+
map1, map2 = compute_camera_mapping_cached(
184202
shot.camera, new_camera, width, height
185203
)
186204
undistorted = cv2.remap(original, map1, map2, interpolation)

0 commit comments

Comments
 (0)