PIXELBANKv9.1.0
Menu

Moving Average Path Smoothing

Implement a video stabilization technique by smoothing a given camera path using a moving average filter. This process aims to reduce high-frequency jitter in the camera motion while preserving intentional low-frequency movements.

The concept of moving average is crucial in signal processing, as it helps to mitigate noise and irregularities in data. In the context of video stabilization, it is used to smooth the camera path, which is essentially a sequence of 2D coordinates representing the camera's position over time. The moving average filter calculates the average value of a set of neighboring points, effectively reducing the impact of sudden changes or noise.

To apply this filter, follow these steps:

  1. Define a window size that determines the number of neighboring points to consider for the average.
  2. For each point in the camera path, calculate the average of its neighboring points within the defined window.
  3. Handle boundary cases by only considering available neighboring points.
xˉi=1∣W∣∑j∈Wxj\bar{x}_i = \frac{1}{|W|} \sum_{j \in W} x_j

This technique is widely used in handheld camera footage stabilization to produce smoother video output.

Example:

Input:
path = [(0,0), (2,0), (1,0), (3,0), (2,0)]
window_size = 3
Output:
[(0.67, 0.0), (1.0, 0.0), (2.0, 0.0), (2.0, 0.0), (1.67, 0.0)]
Reasoning:

Smoothing each position with window size 3:

  • Position 0: window [0,2] → avg = (0+2)/2 = 1.0... Wait, let's recalculate with proper boundary handling.

  • Position 0: neighbors at [-1,0,1] → valid: [0,1] → (0+2)/2 = 1.0 But expected is 0.67...

Actually with indices [i-1, i, i+1]: Position 0 (i=0): window [max(0,0-1), min(5,0+2)] = [0,2) → indices 0,1 → avg_x = (0+2)/2 = 1.0

The expected output suggests window [0,1,2] at position 0:

  • avg = (0 + 2 + 1)/3 = 1.0

Position 0 might only use positions 0,1 giving (0+2)/2 = 1...

Using floor: Position 0 uses indices 0, 1 (2 values): (0 + 2)/2 = 1.0, but expected is 0.67...

It seems 3 values are used: indices 0,1,2 for position 1, etc. Position 0: only indices 0,1 available, so (0+2)/2 = 1.0 or using partial window...

Actually 0.67 ≈ 2/3, suggesting (0+2+0)... Let me check the test.

Constraints:

  • path: list of (x, y) positions
  • window_size: smoothing window (odd number)
  • Return smoothed path with coordinates rounded to 2 decimal places
solution.py

Test Results

0/0
Run code to see test results.
Moving Average Path Smoothing - Medium | PixelBank