Equivalent to mapper.adjust_global_bundle(...)
(
mapper: pycolmap.IncrementalMapper,
mapper_options: pycolmap.IncrementalMapperOptions,
ba_options: pycolmap.BundleAdjustmentOptions,
)
| 34 | |
| 35 | |
| 36 | def adjust_global_bundle( |
| 37 | mapper: pycolmap.IncrementalMapper, |
| 38 | mapper_options: pycolmap.IncrementalMapperOptions, |
| 39 | ba_options: pycolmap.BundleAdjustmentOptions, |
| 40 | ) -> bool: |
| 41 | """Equivalent to mapper.adjust_global_bundle(...)""" |
| 42 | reconstruction = mapper.reconstruction |
| 43 | assert reconstruction is not None |
| 44 | reg_frame_ids = reconstruction.reg_frame_ids() |
| 45 | if len(reg_frame_ids) < 2: |
| 46 | logging.fatal("At least two images must be registered for global BA") |
| 47 | custom_ba_options = copy.deepcopy(ba_options) |
| 48 | |
| 49 | # Use stricter convergence criteria for first registered images |
| 50 | if len(reg_frame_ids) < 10: # kMinNumRegImagesForFastBA = 10 |
| 51 | custom_ba_options.ceres.solver_options.function_tolerance /= 10 |
| 52 | custom_ba_options.ceres.solver_options.gradient_tolerance /= 10 |
| 53 | custom_ba_options.ceres.solver_options.parameter_tolerance /= 10 |
| 54 | custom_ba_options.ceres.solver_options.max_num_iterations *= 2 |
| 55 | custom_ba_options.ceres.solver_options.max_linear_solver_iterations = ( |
| 56 | 200 |
| 57 | ) |
| 58 | |
| 59 | # Avoid degeneracies in bundle adjustment |
| 60 | mapper.observation_manager.filter_observations_with_negative_depth() |
| 61 | |
| 62 | # Configure bundle adjustment |
| 63 | ba_config = pycolmap.BundleAdjustmentConfig() |
| 64 | for frame_id in reg_frame_ids: |
| 65 | frame = reconstruction.frame(frame_id) |
| 66 | for data_id in frame.data_ids: |
| 67 | if data_id.sensor_id.type != pycolmap.SensorType.CAMERA: |
| 68 | continue |
| 69 | ba_config.add_image(data_id.id) |
| 70 | |
| 71 | # Fix the existing images, if option specified |
| 72 | if mapper_options.fix_existing_frames: |
| 73 | for frame_id in reg_frame_ids: |
| 74 | if frame_id in mapper.existing_frame_ids: |
| 75 | ba_config.set_constant_rig_from_world_pose(frame_id) |
| 76 | |
| 77 | for rig_id in mapper_options.constant_rigs: |
| 78 | for sensor_id in reconstruction.rig(rig_id).non_ref_sensors: |
| 79 | ba_config.set_constant_sensor_from_rig_pose(sensor_id) |
| 80 | |
| 81 | for camera_id in mapper_options.constant_cameras: |
| 82 | ba_config.set_constant_cam_intrinsics(camera_id) |
| 83 | |
| 84 | # TODO: Add python support for prior positions |
| 85 | # Fixing the gauge with two cameras leads to a more stable optimization |
| 86 | # with fewer steps as compared to fixing three points. |
| 87 | ba_config.fix_gauge(pycolmap.BundleAdjustmentGauge.TWO_CAMS_FROM_WORLD) |
| 88 | |
| 89 | # Run bundle adjustment |
| 90 | summary = solve_bundle_adjustment( |
| 91 | reconstruction, custom_ba_options, ba_config |
| 92 | ) |
| 93 | logging.info("Global Bundle Adjustment") |
no test coverage detected