toolbox: publish current calibration.py (project_gel_frame was missing)
Browse files- toolbox/calibration.py +56 -0
toolbox/calibration.py
CHANGED
|
@@ -105,3 +105,59 @@ def project_gel_to_pixel(sensor_pose7, gel_center_mm, cam_calib):
|
|
| 105 |
u = K["fx"] * p_cam[0] / p_cam[2] + K["ppx"]
|
| 106 |
v = K["fy"] * p_cam[1] / p_cam[2] + K["ppy"]
|
| 107 |
return float(u), float(v)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
u = K["fx"] * p_cam[0] / p_cam[2] + K["ppx"]
|
| 106 |
v = K["fy"] * p_cam[1] / p_cam[2] + K["ppy"]
|
| 107 |
return float(u), float(v)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def project_rigid_origin(pose7, cam_calib):
|
| 111 |
+
"""The OptiTrack rigid-body origin -> pixel. The BACK of the tool.
|
| 112 |
+
|
| 113 |
+
Exposed because the gel centre alone is ambiguous on screen: the marker
|
| 114 |
+
cluster sits 52 mm behind the gel, which projects a median 21 px away, and
|
| 115 |
+
a viewer with no stem drawn between them reads that gap as a calibration
|
| 116 |
+
error. It is the length of the tool.
|
| 117 |
+
"""
|
| 118 |
+
import numpy as _np
|
| 119 |
+
p = _np.asarray(pose7, float)
|
| 120 |
+
T = _np.asarray(cam_calib["T_mocap_to_cam"], float)
|
| 121 |
+
X = T[:3, :3] @ (p[:3] * 1000.0) + T[:3, 3]
|
| 122 |
+
if X[2] <= 1.0:
|
| 123 |
+
return None
|
| 124 |
+
K = cam_calib["intrinsics"]
|
| 125 |
+
return _np.array([K["fx"] * X[0] / X[2] + K["ppx"],
|
| 126 |
+
K["fy"] * X[1] / X[2] + K["ppy"]])
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def project_gel_frame(sensor_pose7, gel_center_mm, cam_calib,
|
| 130 |
+
axis_len_mm: float = 60.0):
|
| 131 |
+
"""Project the gel centre AND its three body axes. Perspective is real.
|
| 132 |
+
|
| 133 |
+
Returns {"centre": (u, v), "tips": [x_tip, y_tip, z_tip], "depth_mm": z}
|
| 134 |
+
or None when the gel centre is behind the camera. A tip that falls behind
|
| 135 |
+
the camera is None; the centre being visible does not guarantee the tips
|
| 136 |
+
are.
|
| 137 |
+
|
| 138 |
+
THE AXIS TIPS ARE PLACED IN 3D AND THEN PROJECTED, at a fixed millimetre
|
| 139 |
+
length, so the drawn triad shrinks with distance and foreshortens with
|
| 140 |
+
orientation. Drawing a fixed PIXEL length instead would assert that two
|
| 141 |
+
sensors at different depths are the same size — the one thing a projection
|
| 142 |
+
exists to tell you apart. It also makes the six rotation probes readable:
|
| 143 |
+
a dot barely moves under rotation, a triad turns.
|
| 144 |
+
"""
|
| 145 |
+
T_rigid = pose7_to_matrix(sensor_pose7)
|
| 146 |
+
R = T_rigid[:3, :3]
|
| 147 |
+
p_gel = (T_rigid @ np.array([*gel_center_mm, 1.0]))[:3]
|
| 148 |
+
|
| 149 |
+
def _proj(P_mocap_mm):
|
| 150 |
+
pc = (cam_calib["T_mocap_to_cam"] @ np.array([*P_mocap_mm, 1.0]))[:3]
|
| 151 |
+
if pc[2] <= 0:
|
| 152 |
+
return None, pc[2]
|
| 153 |
+
K = cam_calib["intrinsics"]
|
| 154 |
+
return (float(K["fx"] * pc[0] / pc[2] + K["ppx"]),
|
| 155 |
+
float(K["fy"] * pc[1] / pc[2] + K["ppy"])), pc[2]
|
| 156 |
+
|
| 157 |
+
centre, depth = _proj(p_gel)
|
| 158 |
+
if centre is None:
|
| 159 |
+
return None
|
| 160 |
+
tips = [_proj(p_gel + R @ (axis_len_mm * e))[0]
|
| 161 |
+
for e in (np.array([1.0, 0, 0]), np.array([0, 1.0, 0]),
|
| 162 |
+
np.array([0, 0, 1.0]))]
|
| 163 |
+
return {"centre": centre, "tips": tips, "depth_mm": float(depth)}
|