Bilinear Interpolation
Given a 2D image (matrix of values) and a floating-point coordinate where is column and is row, compute the bilinearly interpolated value.
Algorithm:
- Clamp coordinates to valid range: ,
- Find the four nearest integer coordinates: , , ,
- Ensure and (so are valid)
- Compute fractional parts: ,
- Interpolate:
Round to 4 decimal places.
Example
image = [[10, 20, 30], [40, 50, 60], [70, 80, 90]] x = 0.5, y = 0.5
30.0
- Input:
imageis a grid, so . The point is column , row : halfway between the first two columns and the first two rows. It lies inside , so clamping changes nothing. - Surrounding pixels: columns and rows box it in. Because is the column, is row 0, column 1, i.e. . The corners are , , , , with .
- Weights: each corner gets the area of the opposite sub-rectangle, here each. The four weights always sum to .
- Math: .
- 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. , all the weight lands there and you get .
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