Trajectory Smoothing Filter
Implement camera trajectory smoothing for video stabilization. This task involves reducing the jitter in a video by smoothing the camera's motion trajectory between frames.
Video stabilization is crucial for enhancing the viewing experience, and it relies on accurately estimating the homography between consecutive frames, which describes the transformation of the camera's motion. The accumulation of these transformations yields the camera's trajectory over time. However, this trajectory is often noisy and needs to be smoothed to remove unwanted jitter.
Here are the key steps:
- Estimate the camera motion between frames using homography estimation techniques.
- Accumulate the transformations to obtain the camera trajectory.
- Apply a smoothing filter to the trajectory.
This technique is widely used in handheld camera recordings and action cameras to reduce shakiness.
Example:
trajectory = [(0,0,0), (1,1,0.1), (0,2,0), (2,1,0.05), (1,2,0)] window_size = 3
Smoothed trajectory with reduced jitter
Original trajectory has jumps. Moving average over window=3:
- Frame 2: avg of frames 1,2,3 positions Result is smoother path.
Constraints:
- trajectory: List of (x, y, angle) camera positions per frame
- window_size: Smoothing window
- Return: Smoothed trajectory
- Background Knowledge
Video stabilization tries to remove unwanted high‑frequency camera shake while preserving the intended low‑frequency camera motion (pans, tilts, zooms). The camera motion between two frames is often modeled as a 2D transform: translation, rotation, scale, or a full homography H∈R3×3. By chaining these inter‑frame transforms, you get a camera trajectory over time (e.g., trajectory of camera pose parameters or homographies).
This trajectory is typically noisy: hand shake appears as rapid, small oscillations in the parameter curves over time. Stabilization works by smoothing these parameter trajectories with a temporal filter (moving average, Gaussian, low‑pass, etc.). The smoothed trajectory T_{\text{smooth}}(t) describes how the virtual stabilized camera should move. You then compute a per‑frame correction transform C(t) that maps the original view at time t to where it would be if the camera had followed the smoothed path instead of the original shaky one.
- Algorithm / Approach Pattern
General pattern for trajectory‑based stabilization:
- Estimate inter‑frame motion: For each pair of consecutive frames, estimate a 2D transform Mt​ (e.g., homography) from frame t to t+1.
- Accumulate to get trajectory: Compose the transforms to obtain a global camera transform T(t) for each frame (relative to a reference frame).
- Smooth the trajectory: Apply a temporal filter to the sequence {T(t)} (or to its parameter vectors) to get \{T_{\text{smooth}}(t)\}.
- Compute correction transforms: For each frame, compute C(t) = T_{\text{smooth}}(t) \cdot T(t)^{-1}.
- Warp frames: Apply C(t) to frame t to render the stabilized video.
Your problem focuses on steps 3–4: given T(t), implement the smoothing and compute C(t).
- Step‑by‑Step Strategy
Assume your input is a list/array of transforms T,T,…,T[N−1] (each could be a 3×3 homography or a parameter vector like (dx,dy,θ,s)).
Step 1: Choose representation to smooth
- Common and simpler: convert each transform T(t) to a parameter vector:
- For example, from a 3×3 homography restricted to similarity/affine or from a known model:
- translation: (dxt​,dyt​)
- rotation: θt​
- scale: st​
- Then you get 1D time series for each parameter: {dxt​},{dyt​},{θt​},{st​}.
This avoids problems with directly averaging matrices.
Step 2: Design the smoothing filter
Given your formula
Tsmooth​(t)=i∑​wi​⋅T(t+i),you are conceptually doing a finite impulse response (FIR) filter with weights wi​ over a temporal window.
For parameters:
# Example: uniform moving average with window size 2k+1
w = [1.0 / (2*k + 1)] * (2*k + 1) # or Gaussian weights normalized to sum to 1
For each frame index t, you’ll combine values from t-k to t+k with weights w[i].
Step 3: Apply smoothing per parameter
For each time t:
- For each parameter p∈{dx,dy,θ,s}:
with boundary handling when t+i is outside [0,N−1] (e.g., clamp indices or shrink window).
In code (sketch):
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.