Flow-Based Frame Warping
Implement a frame interpolation technique using optical flow to create an intermediate frame at time . Given an image and optical flow, the goal is to warp the image using backward warping, which samples each output pixel from the input based on the flow. The optical flow at each pixel represents the motion of the pixel in the and directions.
- For each output pixel at , sample from the input at .
- Use nearest-neighbor sampling with boundary clamping to handle pixels outside the image bounds.
This technique is widely used in video processing and computer vision applications.
Example
image = [[1,2,3],[4,5,6],[7,8,9]]
flow = [[(1,0),(1,0),(1,0)],
[(1,0),(1,0),(1,0)],
[(1,0),(1,0),(1,0)]]
t = 1[[1, 1, 2], [4, 4, 5], [7, 7, 8]]
-
For each output pixel, sample from (x - ut, y - vt):
-
Output (0,0): sample from (0-1×1, 0-0×1) = (-1, 0) → clamp to (0, 0) → value 1
-
Output (0,1): sample from (1-1×1, 0-0×1) = (0, 0) → value 1
-
Output (0,2): sample from (2-1×1, 0-0×1) = (1, 0) → value 2
-
Output (1,0): sample from (0-1×1, 1-0×1) = (-1, 1) → clamp to (0, 1) → value 4 ...and so on
Result: [[1,1,2], [4,4,5], [7,7,8]]
The rightward flow (u=1) shifts content left.
Constraints
- image: 2D grayscale image
- flow: 2D array of (u, v) flow vectors (same size as image)
- t: time factor (0 to 1)
- Return warped image using nearest neighbor sampling
- Clamp source coordinates to valid range