Stitching

flowtracks.stitching reconnects broken trajectory segments across short frame gaps: candidates are matched between the end of one segment and the start of the next, then accepted when the candidate is the unique nearest neighbour — a linear-sum-assignment-based matching.

Stitching is one of the steps in flowtracks.clean; it is also available standalone.

Trajectory stitching routines for reconnecting broken trajectory segments.

flowtracks.stitching.stitch_trajectories(trajs, fps=1.0, max_gap=5, max_distance=5.0, max_vel_diff=10.0, dt=None)[source]

Stitch (relink) broken trajectory segments across short frame gaps.

Parameters

trajs : list of Trajectory List of trajectory objects to be stitched. fps : float, optional Frames per second (or sampling frequency). Default is 1.0. max_gap : int, optional Maximum number of missing frames allowed between track end and start. e.g., max_gap=5 means a gap of 1 to 5 missing frames can be bridged. max_distance : float, optional Maximum spatial distance between the extrapolated end of Track A and start of Track B. max_vel_diff : float, optional Maximum magnitude of velocity difference between Track A end and Track B start. dt : float, optional Time step delta between consecutive frames. If None, derived as 1.0 / fps.

Returns

stitched_trajs : list of Trajectory List of stitched Trajectory objects with gaps interpolated.

flowtracks.stitching.stitch_trajectories_fast(trajs, fps=1.0, max_gap=5, max_distance=5.0, max_vel_diff=10.0, dt=None)[source]

Stitch broken trajectory segments across short frame gaps (vectorized).

Same parameters, acceptance criteria, gap interpolation, and return convention as stitch_trajectories(), but scales to tens of thousands of segments: candidate (end, start) pairs are generated per (end-time, gap) group with numpy matrix ops instead of an O(N^2) pure-Python double loop, and the global Hungarian assignment is replaced by greedy matching in cost-ascending order. Matches are spatially local and rarely contested, so the greedy result almost always equals the optimum; it is not guaranteed identical.

Segments whose times are not strictly increasing (e.g. the unlinked particles bucket) cannot be extrapolated meaningfully and are passed through untouched.

Parameters

trajs : list of Trajectory List of trajectory objects to be stitched. fps : float, optional Frames per second. Default 1.0. max_gap : int, optional Maximum number of missing frames allowed between track end and start. max_distance : float, optional Maximum mean distance between the velocity-extrapolated end of one segment and the start of the other. max_vel_diff : float, optional Maximum magnitude of the velocity difference at the join. dt : float, optional Time step between consecutive frames. If None, derived as 1 / fps.

Returns

stitched_trajs : list of Trajectory List of stitched Trajectory objects with gaps interpolated.

From file to picture

The figure below shows two broken segments of one trajectory (left) being reconnected by flowtracks.stitching.stitch_trajectories() across a short frame gap (right), with the gap interpolated.

Stitching two broken trajectory segments
from flowtracks.trajectory import Trajectory
from flowtracks.stitching import stitch_trajectories
import numpy as np

seg1 = Trajectory(pos1, vel1, t1, 1)
seg2 = Trajectory(pos2, vel2, t2, 2)
stitched = stitch_trajectories([seg1, seg2], fps=1.0,
                               max_gap=5, max_distance=5.0)