PIXELBANKv9.1.0
Menu

Flow-Based Frame Warping

Implement a frame interpolation technique using optical flow to create an intermediate frame at time tt. 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 (u,v)(u, v) at each pixel represents the motion of the pixel in the xx and yy directions.

  1. For each output pixel at (x,y)(x, y), sample from the input at (xβˆ’uΓ—t,yβˆ’vΓ—t)(x - u \times t, y - v \times t).
  2. Use nearest-neighbor sampling with boundary clamping to handle pixels outside the image bounds.
(xβ€²,yβ€²)=(xβˆ’uΓ—t,yβˆ’vΓ—t)(x', y') = (x - u \times t, y - v \times t)

This technique is widely used in video processing and computer vision applications.

Example:

Input:
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
Output:
[[1, 1, 2], [4, 4, 5], [7, 7, 8]]
Reasoning:
  • 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
πŸ”’

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.

solution.py

Test Results

0/0
Run code to see test results.
Flow-Based Frame Warping - Medium | PixelBank