v9.1.1
Menu

Flow Field Magnitude

easyPROOptical FlowMotionCV: Motion Estimation

Implement a function to compute the magnitude of a given 2D optical flow field at each pixel, which represents the speed of motion. The optical flow field is a 2D vector field where each vector (u,v)(u, v) at a pixel location represents the motion of that pixel in the xx and yy directions.

The concept of optical flow is crucial in Computer Vision as it helps in understanding the motion of objects in a scene. The magnitude of flow at each pixel is calculated using the formula ∣v∣=u2+v2|\mathbf{v}| = \sqrt{u^2 + v^2}, where (u,v)(u, v) is the flow vector at that pixel.

  1. Understand the given 2D optical flow field.
  2. Calculate the magnitude at each pixel using the flow vector.
∣v∣=u2+v2 |\mathbf{v}| = \sqrt{u^2 + v^2}

This technique is widely used in traffic monitoring applications to measure velocity.

Example

Input
flow = [[(3, 4), (0, 0)],
       [(1, 0), (0, 1)]]
Output
[[5.0, 0.0], [1.0, 1.0]]
Reasoning
  • Input: flow is a 2×22 \times 2 grid of pixels. Each pixel holds a flow vector (u,v)(u, v): how far that pixel moved horizontally (uu) and vertically (vv) between frames.
  • Top-left (3,4)(3, 4): the speed is the length of the vector, 32+42=9+16=25=5.0\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5.0. This is the classic 3-4-5 right triangle.
  • Top-right (0,0)(0, 0): the pixel did not move, so 0+0=0.0\sqrt{0 + 0} = 0.0.
  • Bottom row (1,0)(1, 0) and (0,1)(0, 1): one unit right and one unit down. Both give 1=1.0\sqrt{1} = 1.0, so the direction differs but the speed is identical.
  • Output: [[5.0, 0.0], [1.0, 1.0]], a grid with the same 2×22 \times 2 layout as the input, but each pixel now holds one non-negative number instead of a pair. Values are rounded to 2 decimal places, so a vector like (1,1)(1, 1) would give 2≈1.41\sqrt{2} \approx 1.41.
  • Notice that magnitude throws away direction: (3,4)(3, 4), (4,3)(4, 3) and (−3,−4)(-3, -4) all map to 5.05.0.

Constraints

  • flow is a 2D array of (u, v) tuples
  • Return magnitude array rounded to 2 decimal places

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.