Least-squares affine alignment: gt ≈ s * pred + t. Args: pred_depth: (H, W) gt_depth: (H, W) valid_mask: (H, W) bool Returns: aligned_pred: (H, W) s: float, scale t: float, offset
(pred_depth, gt_depth, valid_mask)
| 31 | |
| 32 | |
| 33 | def lstsq_alignment(pred_depth, gt_depth, valid_mask): |
| 34 | """Least-squares affine alignment: gt ≈ s * pred + t. |
| 35 | |
| 36 | Args: |
| 37 | pred_depth: (H, W) |
| 38 | gt_depth: (H, W) |
| 39 | valid_mask: (H, W) bool |
| 40 | |
| 41 | Returns: |
| 42 | aligned_pred: (H, W) |
| 43 | s: float, scale |
| 44 | t: float, offset |
| 45 | """ |
| 46 | pred_valid = pred_depth[valid_mask].flatten() |
| 47 | gt_valid = gt_depth[valid_mask].flatten() |
| 48 | |
| 49 | if len(pred_valid) < 2: |
| 50 | return pred_depth.copy(), 1.0, 0.0 |
| 51 | |
| 52 | # Build [pred, 1] @ [s, t]^T = gt |
| 53 | A = np.stack([pred_valid, np.ones_like(pred_valid)], axis=1) |
| 54 | result = np.linalg.lstsq(A, gt_valid, rcond=None) |
| 55 | s, t = result[0] |
| 56 | |
| 57 | aligned_pred = pred_depth * s + t |
| 58 | return aligned_pred, float(s), float(t) |
| 59 | |
| 60 | |
| 61 | def procrustes_alignment(pred_poses, gt_poses): |