Bilinear Flow Warping
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 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:
- For each output pixel , calculate the source location by adding the optical flow values .
- Check if the calculated source location falls between pixels.
- If it does, apply bilinear interpolation to determine the intensity value of the output pixel.
Details
- The flow is read at the output pixel: row , column of flow_u and flow_v give and in pixels. There is no separate time parameter; to build the frame at time , pass the flow already scaled by .
- The source location is valid only when and , so that all four neighbouring pixels exist. Otherwise the output pixel is . (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 .)
- Return an 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
src_frame = image at t=0 flow = motion from t=0 to t=1 warp_factor = 0.5
Intermediate frame at t=0.5
src_frameis an intensity grid;flow_u,flow_vare same-shape grids of horizontal and vertical shift in pixels. The sample'swarp_factor = 0.5means the flow passed in is pre-halved.- Take source rows , , and , everywhere. Output pixel reads source point , between , with , .
- Weights: on , on , on , on , giving .
- Pixel reads : , kept as in the 8-bit output.
- Column () and row () have no right or lower neighbour, so . Output rows: , , .
- 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