v9.1.1
Menu

Bilinear Flow Warping

hardPROFrame InterpolationInterpolationCV: Motion Estimation

Implement frame warping using optical flow with bilinear interpolation to create an intermediate frame. This task involves understanding how to apply optical flow to warp a source frame.

The concept of optical flow is crucial here, as it represents the pattern of apparent motion of objects, surfaces, and edges in a visual scene caused by the relative motion between an observer and the scene. Given the optical flow (u,v)(u, v) at each pixel, the goal is to warp the source frame to create an intermediate frame. The warping process involves shifting each pixel in the source frame by the corresponding optical flow values.

Here are the steps to achieve this:

  1. For each output pixel (x,y)(x, y), calculate the source location by adding the optical flow values (u,v)(u, v).
  2. Check if the calculated source location falls between pixels.
  3. If it does, apply bilinear interpolation to determine the intensity value of the output pixel.
Iout(x,y)=Isrc(x+u(x,y),y+v(x,y))I_{out}(x, y) = I_{src}(x + u(x,y), y + v(x,y))

Details

  • The flow is read at the output pixel: row yy, column xx of flow_u and flow_v give u(x,y)u(x,y) and v(x,y)v(x,y) in pixels. There is no separate time parameter; to build the frame at time tt, pass the flow already scaled by tt.
  • The source location (xs,ys)=(x+u,y+v)(x_s, y_s) = (x + u, y + v) is valid only when 0≤xs<W−10 \le x_s < W-1 and 0≤ys<H−10 \le y_s < H-1, so that all four neighbouring pixels exist. Otherwise the output pixel is 00. (A location exactly on the last row or column counts as outside, so with zero flow the last row and column of the output are 00.)
  • Return an H×WH \times W array of 8-bit unsigned integers; interpolated values keep only their integer part (truncated, not rounded).

This technique is widely used in video processing applications, such as frame interpolation and video slow-motion.

Example

Input
src_frame = image at t=0
flow = motion from t=0 to t=1
warp_factor = 0.5
Output
Intermediate frame at t=0.5
Reasoning
  • src_frame is an H×WH \times W intensity grid; flow_u, flow_v are same-shape grids of horizontal and vertical shift in pixels. The sample's warp_factor = 0.5 means the flow passed in is pre-halved.
  • Take source rows [0,100,200][0, 100, 200], [40,140,240][40, 140, 240], [80,180,255][80, 180, 255] and u=0.5u = 0.5, v=0.25v = 0.25 everywhere. Output pixel (0,0)(0, 0) reads source point (0.5,0.25)(0.5, 0.25), between 0,100,40,1400, 100, 40, 140, with dx=0.5d_x = 0.5, dy=0.25d_y = 0.25.
  • Weights: (1−dx)(1−dy)=0.375(1-d_x)(1-d_y) = 0.375 on 00, dx(1−dy)=0.375d_x(1-d_y) = 0.375 on 100100, (1−dx)dy=0.125(1-d_x)d_y = 0.125 on 4040, dxdy=0.125d_x d_y = 0.125 on 140140, giving 0+37.5+5+17.5=600 + 37.5 + 5 + 17.5 = 60.
  • Pixel (1,1)(1, 1) reads (1.5,1.25)(1.5, 1.25): 0.375⋅140+0.375⋅240+0.125⋅180+0.125⋅255=196.8750.375 \cdot 140 + 0.375 \cdot 240 + 0.125 \cdot 180 + 0.125 \cdot 255 = 196.875, kept as 196196 in the 8-bit output.
  • Column x=2x = 2 (xs=2.5≥W−1x_s = 2.5 \ge W - 1) and row y=2y = 2 (ys=2.25y_s = 2.25) have no right or lower neighbour, so 00. Output rows: [60,160,0][60, 160, 0], [100,196,0][100, 196, 0], [0,0,0][0, 0, 0].
  • Notice zero flow would still blank the last row and column: those points sit exactly on the edge.

Constraints

  • src_frame: Source frame (H, W)
  • flow_u, flow_v: Optical flow fields (H, W)
  • Return: Warped frame

Learn this in

More from CV: Motion Estimation

PRO

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

Sign in to run your code against the test cases.