|
| 1 | +Python usage |
| 2 | +============ |
| 3 | + |
| 4 | +This package exposes a single function, `easy_sba`, for running sparse bundle |
| 5 | +adjustment from Python. |
| 6 | + |
| 7 | +Quickstart |
| 8 | +---------- |
| 9 | + |
| 10 | +```python |
| 11 | +import numpy as np |
| 12 | +from easysba import easy_sba |
| 13 | + |
| 14 | +num_points = 10 |
| 15 | +num_cams = 4 |
| 16 | +rng = np.random.default_rng(0) |
| 17 | + |
| 18 | +image_uv = rng.normal(scale=0.5, size=(num_points, num_cams, 2)).astype(np.float64) |
| 19 | +visibility_mask = np.ones((num_points, num_cams), dtype=np.uint8) |
| 20 | +world_xyz = rng.normal(scale=2.0, size=(num_points, 3)).astype(np.float64) |
| 21 | + |
| 22 | +# 12-parameter cameras: [fu, u0, v0, ar, skew, qw, qx, qy, qz, tx, ty, tz] |
| 23 | +camera_params = np.zeros((num_cams, 12), dtype=np.float64) |
| 24 | +camera_params[:, 0] = 1.0 # fu |
| 25 | +camera_params[:, 3] = 1.0 # aspect ratio |
| 26 | +camera_params[:, 5] = 1.0 # quaternion w |
| 27 | +camera_params[:, 9:] = rng.normal(scale=0.1, size=(num_cams, 3)) |
| 28 | + |
| 29 | +world_out, cams_out, info = easy_sba( |
| 30 | + image_uv, |
| 31 | + visibility_mask, |
| 32 | + world_xyz, |
| 33 | + camera_params, |
| 34 | + intrinsics_fixed=5, |
| 35 | + distortion_fixed=-1, |
| 36 | + max_iter=50, |
| 37 | + verbose=False, |
| 38 | +) |
| 39 | + |
| 40 | +print(info) |
| 41 | +``` |
| 42 | + |
| 43 | +Function signature |
| 44 | +------------------ |
| 45 | + |
| 46 | +```python |
| 47 | +world_out, cams_out, info = easy_sba( |
| 48 | + image_uv, |
| 49 | + visibility_mask, |
| 50 | + world_xyz, |
| 51 | + camera_params, |
| 52 | + intrinsics_fixed=-1, |
| 53 | + distortion_fixed=-1, |
| 54 | + max_iter=150, |
| 55 | + verbose=False, |
| 56 | +) |
| 57 | +``` |
| 58 | + |
| 59 | +Inputs |
| 60 | +------ |
| 61 | + |
| 62 | +- `image_uv` |
| 63 | + - Either shape `(num_points, num_cams, 2)` or `(num_points, num_cams * 2)`. |
| 64 | + - Must be `float64`. |
| 65 | + - For invisible measurements, set `visibility_mask` to 0 and store NaNs |
| 66 | + at the corresponding `image_uv` location. |
| 67 | + |
| 68 | +- `visibility_mask` |
| 69 | + - Shape `(num_points, num_cams)`. |
| 70 | + - `uint8`, with nonzero values for visible projections. |
| 71 | + |
| 72 | +- `world_xyz` |
| 73 | + - Shape `(num_points, 3)`. |
| 74 | + - `float64`. |
| 75 | + |
| 76 | +- `camera_params` |
| 77 | + - Shape `(num_cams, num_camera_params)`. |
| 78 | + - `float64`. |
| 79 | + - `num_camera_params` must be 7, 12, or 17. |
| 80 | + |
| 81 | +Camera parameter layouts |
| 82 | +------------------------ |
| 83 | + |
| 84 | +- 7 parameters (default intrinsics): |
| 85 | + - `[qw, qx, qy, qz, tx, ty, tz]` |
| 86 | + - Intrinsics are assumed to be `[fu=1, u0=0, v0=0, ar=1, skew=0]`. |
| 87 | + |
| 88 | +- 12 parameters (fixed intrinsics in array): |
| 89 | + - `[fu, u0, v0, ar, skew, qw, qx, qy, qz, tx, ty, tz]` |
| 90 | + |
| 91 | +- 17 parameters (intrinsics + distortion): |
| 92 | + - `[fu, u0, v0, ar, skew, k1, k2, p1, p2, k3, qw, qx, qy, qz, tx, ty, tz]` |
| 93 | + |
| 94 | +Notes: |
| 95 | +- Quaternions are normalized internally. A zero-norm quaternion raises an error. |
| 96 | +- Translation is in the camera coordinate system used by the projection model |
| 97 | + in the SBA implementation. |
| 98 | + |
| 99 | +Options |
| 100 | +------- |
| 101 | + |
| 102 | +- `intrinsics_fixed` |
| 103 | + - Controls which intrinsics are held fixed. |
| 104 | + - Must be `-1` for 7-parameter cameras (intrinsics not provided). |
| 105 | + - For 12- or 17-parameter cameras, use values 0..5 as defined by SBA. |
| 106 | + - Common choice is `5` to fix all intrinsics. |
| 107 | + |
| 108 | +- `distortion_fixed` |
| 109 | + - Controls which distortion parameters are held fixed for 17-parameter cameras. |
| 110 | + - Use `-1` to free all distortion params, or 0..5 to fix subsets. |
| 111 | + - For 7- or 12-parameter cameras, use `-1`. |
| 112 | + |
| 113 | +- `max_iter` |
| 114 | + - Maximum LM iterations. |
| 115 | + |
| 116 | +- `verbose` |
| 117 | + - Print iteration details. |
| 118 | + |
| 119 | +Outputs |
| 120 | +------- |
| 121 | + |
| 122 | +- `world_out`: optimized 3D point array, shape `(num_points, 3)`. |
| 123 | +- `cams_out`: optimized camera parameters, same shape as `camera_params`. |
| 124 | +- `info`: dict with keys like `return_code`, `initial_error`, `final_error`, |
| 125 | + `num_iterations`. |
| 126 | + |
| 127 | +Tips |
| 128 | +---- |
| 129 | + |
| 130 | +- Provide reasonable initial guesses. BA is non-convex and sensitive to |
| 131 | + initialization. |
| 132 | +- Use NaNs in `image_uv` for masked entries; the visibility mask must match. |
| 133 | +- Start with `max_iter=1` to validate inputs and shapes. |
| 134 | + |
| 135 | +Testing |
| 136 | +------- |
| 137 | + |
| 138 | +To run the optional solver tests: |
| 139 | + |
| 140 | +```bash |
| 141 | +EASYSBA_RUN_SOLVER=1 pytest |
| 142 | +``` |
0 commit comments