v9.1.1
Menu

Light Field Refocusing Shift

mediumPROLight FieldsLight FieldCV: Image-Based Rendering

Compute the pixel shift for refocusing in a light field.

Light fields enable digital refocusing - changing the focal plane after capture. To focus at depth dd instead of the original d0d_0, we shift each subaperture image by:

shift=(s−s0)⋅d0−ddshift = (s - s_0) \cdot \frac{d_0 - d}{d}

where:

  • ss is the subaperture position
  • s0s_0 is the central subaperture
  • d0d_0 is the original focal distance
  • dd is the target focal distance

Objects at depth dd will be in focus (zero parallax), while objects at other depths are blurred.

If the target focal distance dd is 0, return 0.0 instead of dividing by zero. Round the result to 4 decimal places.

Example

Input
refocus_shift(2, 0, 10, 5)
Output
2.0
Reasoning
  • Inputs: s = 2 is the subaperture's position, s_center = 0 is the central subaperture, d_original = 10 is the depth the capture was focused at, and d_target = 5 is the new, closer depth we want sharp.
  • Baseline offset: s−s0=2−0=2s - s_0 = 2 - 0 = 2. This view's distance from the centre sets how strongly it must move; the centre view itself never moves.
  • Depth factor: d0−dd=10−55=1\frac{d_0 - d}{d} = \frac{10 - 5}{5} = 1. Halving the focal distance makes the factor exactly 1.
  • Shift: 2⋅1=22 \cdot 1 = 2, already exact at 4 decimal places, so the result is 2.0 pixels.
  • Notice: the sign carries meaning. Refocusing farther (say d=20d = 20) gives 10−2020=−0.5\frac{10 - 20}{20} = -0.5 and a shift of −1-1, the opposite direction. When d=d0d = d_0 the factor is 0 and nothing moves, since the focal plane is unchanged.

Constraints

  • s: subaperture position
  • s_center: center subaperture position
  • d_original: original focal distance
  • d_target: target focal distance
  • Return shift in pixels, rounded to 4 decimal places

Learn this in

More from CV: Image-Based Rendering

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.