@@ -77,6 +77,7 @@ def undistort_reconstruction(
77
77
78
78
return undistorted_shots
79
79
80
+ _camera_mapping_cache = {}
80
81
81
82
def undistort_reconstruction_with_images (
82
83
tracks_manager : Optional [pymap .TracksManager ],
@@ -86,6 +87,9 @@ def undistort_reconstruction_with_images(
86
87
imageFilter : Callable [[str , np .ndarray ], np .ndarray ] = None ,
87
88
skip_images : bool = False ,
88
89
) -> Dict [pymap .Shot , List [pymap .Shot ]]:
90
+ global _camera_mapping_cache
91
+ _camera_mapping_cache = {}
92
+
89
93
undistorted_shots = undistort_reconstruction (
90
94
tracks_manager , reconstruction , data , udata
91
95
)
@@ -127,7 +131,7 @@ def undistort_image_and_masks(arguments) -> None:
127
131
# Undistort image
128
132
image = data .load_image (shot .id , unchanged = True , anydepth = True )
129
133
if image is not None :
130
- if imageFilter is not None :
134
+ if hasattr ( imageFilter , '__call__' ) :
131
135
image = imageFilter (shot .id , image )
132
136
undistorted = undistort_image (
133
137
shot , undistorted_shots , image , cv2 .INTER_AREA , max_size
@@ -154,6 +158,20 @@ def undistort_image_and_masks(arguments) -> None:
154
158
udata .save_undistorted_segmentation (k , v )
155
159
156
160
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
+
157
175
def undistort_image (
158
176
shot : pymap .Shot ,
159
177
undistorted_shots : List [pymap .Shot ],
@@ -180,7 +198,7 @@ def undistort_image(
180
198
[undistorted_shot ] = undistorted_shots
181
199
new_camera = undistorted_shot .camera
182
200
height , width = original .shape [:2 ]
183
- map1 , map2 = pygeometry . compute_camera_mapping (
201
+ map1 , map2 = compute_camera_mapping_cached (
184
202
shot .camera , new_camera , width , height
185
203
)
186
204
undistorted = cv2 .remap (original , map1 , map2 , interpolation )
0 commit comments