v9.1.1
Menu

Bilinear Interpolation

mediumPROGeometric TransformationsComputer Vision 2

Given a 2D image (matrix of values) and a floating-point coordinate (x,y)(x, y) where xx is column and yy is row, compute the bilinearly interpolated value.

Algorithm:

  1. Clamp coordinates to valid range: x∈[0,W−1]x \in [0, W-1], y∈[0,H−1]y \in [0, H-1]
  2. Find the four nearest integer coordinates: (x0,y0)(x_0, y_0), (x1,y0)(x_1, y_0), (x0,y1)(x_0, y_1), (x1,y1)(x_1, y_1)
    • Ensure x0≤W−2x_0 \leq W-2 and y0≤H−2y_0 \leq H-2 (so x1,y1x_1, y_1 are valid)
  3. Compute fractional parts: dx=x−x0dx = x - x_0, dy=y−y0dy = y - y_0
  4. Interpolate: f(x,y)=f(x0,y0)(1−dx)(1−dy)+f(x1,y0)⋅dx(1−dy)+f(x0,y1)(1−dx)⋅dy+f(x1,y1)⋅dx⋅dyf(x,y) = f(x_0,y_0)(1-dx)(1-dy) + f(x_1,y_0) \cdot dx(1-dy) + f(x_0,y_1)(1-dx) \cdot dy + f(x_1,y_1) \cdot dx \cdot dy

Round to 4 decimal places.

Example

Input
image = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
x = 0.5, y = 0.5
Output
30.0
Reasoning
  • Input: image is a 3×33 \times 3 grid, so W=H=3W = H = 3. The point (x,y)=(0.5,0.5)(x, y) = (0.5, 0.5) is column 0.50.5, row 0.50.5: halfway between the first two columns and the first two rows. It lies inside [0,2][0, 2], so clamping changes nothing.
  • Surrounding pixels: columns 0,10, 1 and rows 0,10, 1 box it in. Because xx is the column, f(1,0)f(1, 0) is row 0, column 1, i.e. 2020. The corners are f(0,0)=10f(0,0)=10, f(1,0)=20f(1,0)=20, f(0,1)=40f(0,1)=40, f(1,1)=50f(1,1)=50, with dx=dy=0.5dx = dy = 0.5.
  • Weights: each corner gets the area of the opposite sub-rectangle, here 0.5×0.5=0.250.5 \times 0.5 = 0.25 each. The four weights always sum to 11.
  • Math: 10(0.25)+20(0.25)+40(0.25)+50(0.25)=2.5+5+10+12.5=3010(0.25) + 20(0.25) + 40(0.25) + 50(0.25) = 2.5 + 5 + 10 + 12.5 = 30.
  • Output: a single float, 30.0. Notice that at the centre of four pixels the result is their plain average, while on a pixel itself, e.g. (1,1)(1, 1), all the weight lands there and you get 5050.

Constraints

  • image is a 2D list (at least 2x2)
  • x is column coordinate, y is row coordinate
  • Clamp to valid range before interpolating
  • Return a single float rounded to 4 decimal places

Learn this in

More from Computer Vision 2

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.