116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 | def generate_sfm_data(images_dir: Path, camera_options: dict):
"""
Generates Structure-from-Motion (SfM) data in the OpenMVG format.
This function was inspired from [PySfMUtils](https://gitlab.com/educelab/sfm-utils/-/blob/develop/sfm_utils/openmvg.py?ref_type=heads) to create an OpenMVG-compatible JSON data structure, including image information, camera intrinsics, and views.
Args:
images_dir (Path): Path to the directory containing source images.
camera_options (dict): Camera configuration options from 'config/cameras.yaml'.
Returns:
dict: A dictionary containing the generated SfM data structure.
"""
"""
images_dir : path to directory containing all the images
camera_options : dictionary with all the options from config/cameras.yaml
"""
# Emulate the Cereal pointer counter
__ptr_cnt = 2147483649
def open_mvg_view(
id: int, img_name: str, images_dir: Path, images_cameras: dict
) -> dict:
"""
OpenMVG View struct
images_cameras : dictionary with image names as key and camera id as value
"""
image_path = images_dir / img_name
image = Image.open(image_path)
width, height = image.size
nonlocal __ptr_cnt
d = {
"key": id,
"value": {
"polymorphic_id": 1073741824,
"ptr_wrapper": {
"id": __ptr_cnt,
"data": {
"local_path": "",
"filename": img_name,
"width": width,
"height": height,
"id_view": id,
"id_intrinsic": images_cameras[img_name],
"id_pose": id,
},
},
},
}
__ptr_cnt += 1
return d
def open_mvg_intrinsic(intrinsic: dict) -> dict:
"""
OpenMVG Intrinsic struct
"""
nonlocal __ptr_cnt
d = {
"key": intrinsic["cam_id"],
"value": {
"polymorphic_id": 2147483649,
"polymorphic_name": intrinsic["camera_model"],
"ptr_wrapper": {
"id": __ptr_cnt,
"data": {
"width": intrinsic["width"],
"height": intrinsic["height"],
"focal_length": intrinsic["focal"],
"principal_point": [
intrinsic["width"] / 2.0,
intrinsic["height"] / 2.0,
],
},
},
},
}
__ptr_cnt += 1
if intrinsic["dist_params"] is not None:
dist_name = __OPENMVG_DIST_NAME_MAP[intrinsic["camera_model"]]
d["value"]["ptr_wrapper"]["data"][dist_name] = intrinsic["dist_params"]
return d
def assign_intrinsics(images_dir: Path, img: str, cam: int, camera_model: str):
image_path = images_dir / img
focal = get_focal(image_path)
image = Image.open(image_path)
width, height = image.size
if camera_model == "pinhole":
params = None
elif camera_model == "pinhole_radial_k3":
params = [0.0, 0.0, 0.0]
elif camera_model == "pinhole_brown_t2":
params = [0.0, 0.0, 0.0, 0.0, 0.0]
d = {
"cam_id": cam,
"camera_model": camera_model,
"width": width,
"height": height,
"focal": focal,
"dist_params": params,
}
return d
def parse_camera_options(images_dir: Path, images: list, camera_options: dict):
single_camera = camera_options["general"]["single_camera"]
views_and_cameras = {}
intrinsics = {}
# Check assigned images exists
for key in list(camera_options.keys()):
if key != "general":
imgs = camera_options[key]["images"]
imgs = imgs.split(",")
if imgs[0] != "":
for img in imgs:
if img not in images:
logger.error(
"Check images assigned to cameras in config/cameras.yaml. Image names or extensions are wrong."
)
quit()
def single_main_camera():
cam = 0
# Assign camera defined in 'general' to all images
for img in images:
views_and_cameras[img] = cam
# intrinsics[cam] = {
# "cam_id": cam,
# "camera_model": camera_options["general"]["camera_model"],
# }
intrinsics[cam] = assign_intrinsics(
images_dir, img, cam, camera_options["general"]["openmvg_camera_model"]
)
# Assign a camera to images defined in 'camx'
other_cam = 1
for key in list(camera_options.keys()):
if key != "general":
imgs = camera_options[key]["images"]
imgs = imgs.split(",")
if imgs[0] != "":
for img in imgs:
views_and_cameras[img] = other_cam
# intrinsics[other_cam] = {
# "cam_id": other_cam,
# "camera_model": camera_options[key]["camera_model"],
# }
intrinsics[other_cam] = assign_intrinsics(
images_dir,
img,
other_cam,
camera_options["general"]["openmvg_camera_model"],
)
other_cam += 1
return intrinsics, views_and_cameras
def no_main_camera():
cam = 0
# Assign a camera to images defined in 'camx'
for key in list(camera_options.keys()):
if key != "general":
imgs = camera_options[key]["images"]
imgs = imgs.split(",")
if imgs[0] != "":
for img in imgs:
views_and_cameras[img] = cam
# intrinsics[cam] = {
# "cam_id": cam,
# "camera_model": camera_options[key]["camera_model"],
# }
intrinsics[cam] = assign_intrinsics(
images_dir,
img,
cam,
camera_options["general"]["openmvg_camera_model"],
)
cam += 1
# For images not defined in 'camx' assign the camera defined in 'general'
grouped_imgs = list(views_and_cameras.keys())
for img in images:
if img not in grouped_imgs:
views_and_cameras[img] = cam
# intrinsics[cam] = {
# "cam_id": cam,
# "camera_model": camera_options["general"]["camera_model"],
# }
intrinsics[cam] = assign_intrinsics(
images_dir,
img,
cam,
camera_options["general"]["openmvg_camera_model"],
)
cam += 1
return intrinsics, views_and_cameras
if single_camera is True:
intrinsics, views_and_cameras = single_main_camera()
elif single_camera is False:
intrinsics, views_and_cameras = no_main_camera()
return intrinsics, views_and_cameras
# Construct OpenMVG struct
images = os.listdir(images_dir)
intrinsics, views_and_cameras = parse_camera_options(
images_dir, images, camera_options
)
data = {
"sfm_data_version": "0.3",
"root_path": str(images_dir),
"views": [
open_mvg_view(i, img, images_dir, views_and_cameras)
for i, img in enumerate(images)
],
"intrinsics": [
open_mvg_intrinsic(intrinsics[c]) for c in list(intrinsics.keys())
],
"extrinsics": [],
"structure": [],
"control_points": [],
}
return data
|