04 Trajectory stitching and imputation (optional, after Step 2)
Entry functions:
from trajlab.processing.stitching import PAPER_STAGES, impute_trajectories, stitch_fragments
result = stitch_fragments(tracks, stages=PAPER_STAGES)
tracks, links = result.tracks, result.links
tracks = impute_trajectories(tracks, fps=30, max_gap_sec=3.0) # optional
Purpose
Camera-based tracking produces fragmented trajectories: a vehicle disappears behind a bridge or a truck, or at a camera hand-over, and reappears under a new ID. Stitching links the fragments of one vehicle; imputation re-grids each vehicle and fills short gaps. Both run on projected, HGV-corrected and aligned positions.
Interface
| Pipeline step | Requires columns | Produces columns | Further output columns |
|---|---|---|---|
stitching |
id, datum, pos_lon, pos_lat |
— | — |
imputation |
id, datum, pos_lon, pos_lat |
— | is_imputed |
Stitching keeps every row and only replaces the IDs of linked fragments by one common ID (the smallest
by string order). Imputation adds rows on a regular per-vehicle grid and marks them is_imputed; it
drops speed/acceleration (Step 3 always recomputes them).
| Parameter | Default | Description |
|---|---|---|
stitching.enabled |
False |
Run the step. |
stitching.stages |
None |
Parameter dicts for cascaded_stitching (keyword arguments of stitch_trajectories). None uses the built-in 4-stage cascade; "overlap" uses overlap_stages() (multi-camera data with overlapping fields of view, I-24 MOTION). |
stitching.bridge_joints |
False |
Join the fragments of each stitched vehicle with cubic Hermite bridges (bridge_joints: 0.5 s trimmed at both fragment ends, C1 joint). Needed when fragments overlap in time. |
stitching.bridge_trim_sec |
0.5 |
Seconds trimmed at both fragment ends of a joint. |
| Parameter | Default | Description |
|---|---|---|
imputation.enabled |
False |
Run the step. |
imputation.fps |
30 |
Frame rate of the imputation grid in Hz. |
imputation.max_gap_sec |
3 |
Longest gap in seconds that is filled; longer gaps stay gaps. |
Method: one stitching pass (stitch_trajectories)
Inspired by the fragment association of Wang et al. (2024; preprint arXiv:2212.07907), implemented as a
one-to-one bipartite matching of fragment tails to fragment heads. The gates and weights of one pass
are the fields of StitchingStage (one entry of stitching.stages):
stitch_trajectories(df, StitchingStage(max_gap_sec=2.0, ...)).
- States. Tail/head: time,
s,d, class, length and the velocities $v_s, v_d$. Original: slope over the last/first 5 samples. Withstate_window_sec: least-squares slope over a time window (the 5-sample slope has a p90 error of 2.9 m/s against the provider speed at Site 1 fragment ends). Optional exit/entry zones exclude tails/heads at ramps. - Prediction for a candidate head at gap $\Delta t$: $\hat s = s_{tail} + v_s \Delta t$, $\hat d = d_{tail} + v_d \Delta t$.
- Gates (a pair failing one is forbidden):
- $0 < \Delta t \le \Delta t_{max}$;
- kinematic envelope $\hat s - \tfrac12 a_{dec}\Delta t^2 - e_x \le s_{head} \le \hat s + \tfrac12 a_{acc}\Delta t^2 + e_x$;
- forward motion $s_{head} \ge s_{tail} - e_x$;
- lateral $|\hat d - d_{head}| < e_y + v_{y,max}\Delta t$, $|d_{head} - d_{tail}| < e_y + 0.2\,|s_{head} - s_{tail}|$ and $< d_{abs,max}$;
- same class, $|L_{head} - L_{tail}| < e_L$;
- speed gate (optional,
max_dv_m_per_s): $|v_{head} - v_{tail}| \le \Delta v_{max} + g\,\Delta t$. - Cost $C = \sqrt{\Delta s_{pred}^2 + (w_y \Delta d_{pred})^2 + (w_L \Delta L_n)^2 + (w_v \Delta v_n)^2}$ (length and speed differences normalized to meter-equivalents).
- Assignment: Hungarian algorithm (
linear_sum_assignment) on the cost matrix; every accepted pair is an edge of a graph whose connected components (chains A→B→C) get one ID.
Method: cascade (cascaded_stitching, stitching.stages)
Passes with growing maximum gap. default_stages() returns the built-in cascade (stages 1–4 below,
original parameters). The cascade is greedy by gap length: a stage only sees candidates within its
own window and, without a speed gate and with a wide envelope, links a wrong neighbor when the true
continuation lies just beyond it. The cascade of the paper evaluation
(applications/ant2027/inputs/stitch_choice.json, also in the drone_hgv_paper preset):
| Stage | Δt_max [s] | e_x [m] | a_acc / a_dec [m/s²] | e_y [m] | v_y,max [m/s] | d_abs,max [m] | w_y | speed gate, 1 s velocity window |
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 1.0 | 2.0 / 6.0 | 0.5 | 0.2 | 5.0 | 10 | no (original) |
| 2 | 5 | 2.0 | 2.0 / 3.5 | 1.0 | 0.5 | 5.0 | 6 | no (original) |
| 3 | 8 | 3.0 | 0.75 / 1.0 (orig. 2.0 / 2.5) | 1.5 | 1.2 | 5.0 | 4 | yes |
| 4 | 11 | 3.0 | 0.75 / 1.0 (orig. 1.0 / 1.5) | 1.5 | 1.2 | 2.5 | 4 | yes |
| 5 hand-over | 16 | 3.0 | 0.5 / 1.0 | 0.75 | 0.2 | 1.5 | 4 (+ w_v = 1) | yes |
Speed gate 3 m/s + 0.25 m/s² × gap. Stages 1–2 keep the original parameters: in congestion the speed at fragment ends varies by more than 3 m/s, and at gaps ≤ 5 s the envelope is tight enough.
Method: evaluation without ground truth (artificial gaps)
In each hand-over zone (clusters of interior track starts/ends) 30 % of the tracks that cross the zone
are cut at real fragment end/start positions of that zone; the cut tracks stay in the data with all real
fragments as competing candidates. Per cut: correct if the head directly follows its tail, wrong if
the tail is followed by (or the head preceded by) another fragment, missed otherwise. Precision =
correct / (correct + wrong), recall = correct / cuts. Parameters were selected on seeds 1–3 over all
zones (rule fixed beforehand: precision ≥ 0.95 in every zone, then the highest recall) and reported on
independent seeds 11–15: precision 0.995, recall 0.949 (original cascade 0.983 / 0.945); at one
camera hand-over of campaign 1b 0.976 / 0.865 (original 0.888 / 0.782); wrong joins 117 → 31. The hand-over zones
behind these numbers are a fixed input, applications/ant2027/inputs/zones_submitted.json.
Method: imputation (impute_trajectories)
- Per vehicle a grid $t_0 + k\,\Delta t$ ($t_0$ = the whole second before its first sample,
$\Delta t$ = round(10⁹ /
fps) ns; integer nanoseconds, no drift). - Continuous columns (
pos_lon,pos_lat,utm_x,utm_y,pos_lat_raw,y_correction,pos_lon_ref) are interpolated with PCHIP (≥ 3 samples, else linear); all other columns take the value of the nearest original sample; the geometry is rebuilt fromutm_x/utm_y. is_imputedmarks grid points without an original sample within half a frame. Gaps longer thanmax_gap_secare not filled.
Design choices
The choices below are made for this library unless a paper is named; the evidence for each is given
under "Source" (a test, a script of applications/ant2027/ or a measurement named in the text).
- Stitching is off by default. Its gates depend on the recording set-up (camera hand-overs, gap lengths, traffic state); the cascade tuned for one set-up does not transfer (see the A39 result below). Recordings without interior track starts and ends need no stitching: of the campaigns at Sites 1 and 2 only campaign 1b is fragmented (camera hand-overs).
- Precision before recall. A wrong join creates artificial lateral jumps and lane changes; a missed join only splits a track. The selection rule therefore asks for precision ≥ 0.95 in every zone first and maximizes recall second.
- Speed gate and tight envelope only in stages 3–4. All wrong joins of the original cascade arose in stage 3. A speed gate in the short stages 1–2 cost recall in stop-and-go traffic, where speeds at fragment ends vary by more than 3 m/s, without removing wrong joins.
- Velocity from a 1 s least-squares window. The 5-sample slope has a p90 error of 2.9 m/s against the provider speed at fragment ends, too large for a 3 m/s speed gate.
- Parameters selected and reported on different seeds. Selection on seeds 1–3 over all zones,
report on seeds 11–15, so that the reported precision is not the one that was tuned. Source:
applications/ant2027/text_numbers/stitch_eval.py,seams.py.
Validation and limitations
tests/processing/stitching/test_stitching.py(pipeline integration, speed gate, velocity window),tests/processing/stitching/test_stitching_dlrht*.py,tests/processing/stitching/test_stitching_a39_ground_truth*.py(external data);applications/ant2027/text_numbers/seams.py,stitch_eval.py.- Site-specific parameters: on the hand-stitched A39 ground truth (curved 7.7 km, several sensors, up to 16 fragments per vehicle) all variants reach a link precision of 0.86–0.88 and recall 0.34–0.54; the cascade tuned on Site 1 does not transfer. Measured vehicle lengths (available on A39) are not yet used.
- In congested zones the tight envelope of stages 3–5 misses true joins (recall 0.83–0.91 there).