# Bilinear interpolation

(Redirected from Bilinear sampling)

In mathematics, bilinear interpolation is an extension of linear interpolation for interpolating functions of two variables (e.g., x and y) on a rectilinear 2D grid.

The four red dots show the data points and the green dot is the point at which we want to interpolate.
Example of bilinear interpolation on the unit square with the z values 0, 1, 1 and 0.5 as indicated. Interpolated values in between represented by color.

Bilinear interpolation is performed using linear interpolation first in one direction, and then again in the other direction. Although each step is linear in the sampled values and in the position, the interpolation as a whole is not linear but rather quadratic in the sample location.

Bilinear interpolation is one of the basic resampling techniques in computer vision and image processing, where it is also called bilinear filtering or bilinear texture mapping.

## Algorithm

Suppose that we want to find the value of the unknown function f at the point (x, y). It is assumed that we know the value of f at the four points Q11 = (x1y1), Q12 = (x1y2), Q21 = (x2y1), and Q22 = (x2y2).

We first do linear interpolation in the x-direction. This yields

{\displaystyle {\begin{aligned}f(x,y_{1})&\approx {\frac {x_{2}-x}{x_{2}-x_{1}}}f(Q_{11})+{\frac {x-x_{1}}{x_{2}-x_{1}}}f(Q_{21}),\\f(x,y_{2})&\approx {\frac {x_{2}-x}{x_{2}-x_{1}}}f(Q_{12})+{\frac {x-x_{1}}{x_{2}-x_{1}}}f(Q_{22}).\end{aligned}}}

We proceed by interpolating in the y-direction to obtain the desired estimate:

{\displaystyle {\begin{aligned}f(x,y)&\approx {\frac {y_{2}-y}{y_{2}-y_{1}}}f(x,y_{1})+{\frac {y-y_{1}}{y_{2}-y_{1}}}f(x,y_{2})\\&={\frac {y_{2}-y}{y_{2}-y_{1}}}\left({\frac {x_{2}-x}{x_{2}-x_{1}}}f(Q_{11})+{\frac {x-x_{1}}{x_{2}-x_{1}}}f(Q_{21})\right)+{\frac {y-y_{1}}{y_{2}-y_{1}}}\left({\frac {x_{2}-x}{x_{2}-x_{1}}}f(Q_{12})+{\frac {x-x_{1}}{x_{2}-x_{1}}}f(Q_{22})\right)\\&={\frac {1}{(x_{2}-x_{1})(y_{2}-y_{1})}}\left(f(Q_{11})(x_{2}-x)(y_{2}-y)+f(Q_{21})(x-x_{1})(y_{2}-y)+f(Q_{12})(x_{2}-x)(y-y_{1})+f(Q_{22})(x-x_{1})(y-y_{1})\right)\\&={\frac {1}{(x_{2}-x_{1})(y_{2}-y_{1})}}{\begin{bmatrix}x_{2}-x&x-x_{1}\end{bmatrix}}{\begin{bmatrix}f(Q_{11})&f(Q_{12})\\f(Q_{21})&f(Q_{22})\end{bmatrix}}{\begin{bmatrix}y_{2}-y\\y-y_{1}\end{bmatrix}}.\end{aligned}}}

Note that we will arrive at the same result if the interpolation is done first along the y direction and then along the x direction.[1]

### Alternative algorithm

An alternative way to write the solution to the interpolation problem is

${\displaystyle f(x,y)\approx a_{0}+a_{1}x+a_{2}y+a_{3}xy,}$

where the coefficients are found by solving the linear system

{\displaystyle {\begin{aligned}{\begin{bmatrix}1&x_{1}&y_{1}&x_{1}y_{1}\\1&x_{1}&y_{2}&x_{1}y_{2}\\1&x_{2}&y_{1}&x_{2}y_{1}\\1&x_{2}&y_{2}&x_{2}y_{2}\end{bmatrix}}{\begin{bmatrix}a_{0}\\a_{1}\\a_{2}\\a_{3}\end{bmatrix}}={\begin{bmatrix}f(Q_{11})\\f(Q_{12})\\f(Q_{21})\\f(Q_{22})\end{bmatrix}},\end{aligned}}}

yielding the result

{\displaystyle {\begin{aligned}a_{0}&={\frac {f(Q_{11})x_{2}y_{2}}{(x_{1}-x_{2})(y_{1}-y_{2})}}+{\frac {f(Q_{12})x_{2}y_{1}}{(x_{1}-x_{2})(y_{2}-y_{1})}}+{\frac {f(Q_{21})x_{1}y_{2}}{(x_{1}-x_{2})(y_{2}-y_{1})}}+{\frac {f(Q_{22})x_{1}y_{1}}{(x_{1}-x_{2})(y_{1}-y_{2})}},\\a_{1}&={\frac {f(Q_{11})y_{2}}{(x_{1}-x_{2})(y_{2}-y_{1})}}+{\frac {f(Q_{12})y_{1}}{(x_{1}-x_{2})(y_{1}-y_{2})}}+{\frac {f(Q_{21})y_{2}}{(x_{1}-x_{2})(y_{1}-y_{2})}}+{\frac {f(Q_{22})y_{1}}{(x_{1}-x_{2})(y_{2}-y_{1})}},\\a_{2}&={\frac {f(Q_{11})x_{2}}{(x_{1}-x_{2})(y_{2}-y_{1})}}+{\frac {f(Q_{12})x_{2}}{(x_{1}-x_{2})(y_{1}-y_{2})}}+{\frac {f(Q_{21})x_{1}}{(x_{1}-x_{2})(y_{1}-y_{2})}}+{\frac {f(Q_{22})x_{1}}{(x_{1}-x_{2})(y_{2}-y_{1})}},\\a_{3}&={\frac {f(Q_{11})}{(x_{1}-x_{2})(y_{1}-y_{2})}}+{\frac {f(Q_{12})}{(x_{1}-x_{2})(y_{2}-y_{1})}}+{\frac {f(Q_{21})}{(x_{1}-x_{2})(y_{2}-y_{1})}}+{\frac {f(Q_{22})}{(x_{1}-x_{2})(y_{1}-y_{2})}}.\end{aligned}}}

If a solution is preferred in terms of f(Q), then we can write

${\displaystyle f(x,y)\approx b_{11}f(Q_{11})+b_{12}f(Q_{12})+b_{21}f(Q_{21})+b_{22}f(Q_{22}),}$

where the coefficients are normalized [2] and can be found by calculating

${\displaystyle {\begin{bmatrix}b_{11}\\b_{12}\\b_{21}\\b_{22}\end{bmatrix}}=\left({\begin{bmatrix}1&x_{1}&y_{1}&x_{1}y_{1}\\1&x_{1}&y_{2}&x_{1}y_{2}\\1&x_{2}&y_{1}&x_{2}y_{1}\\1&x_{2}&y_{2}&x_{2}y_{2}\end{bmatrix}}^{-1}\right)^{\rm {T}}{\begin{bmatrix}1\\x\\y\\xy\end{bmatrix}}.}$

### Unit square

If we choose a coordinate system in which the four points where f is known are (0, 0), (1, 0), (0, 1), and (1, 1), then the interpolation formula simplifies to

${\displaystyle f(x,y)\approx f(0,0)(1-x)(1-y)+f(1,0)x(1-y)+f(0,1)(1-x)y+f(1,1)xy,}$

or equivalently, in matrix operations:

${\displaystyle f(x,y)\approx {\begin{bmatrix}1-x&x\end{bmatrix}}{\begin{bmatrix}f(0,0)&f(0,1)\\f(1,0)&f(1,1)\end{bmatrix}}{\begin{bmatrix}1-y\\y\end{bmatrix}}.}$

A geometric visualisation of bilinear interpolation. The product of the value at the desired point (black) and the entire area is equal to the sum of the products of the value at each corner and the partial area diagonally opposite the corner (corresponding colours).

### Nonlinear

As the name suggests, the bilinear interpolant is not linear; but it is the product of two linear functions. For example, the bilinear interpolation derived above is a product of the values of ${\displaystyle x}$  and ${\displaystyle y}$ .

Alternatively, the interpolant on the unit square can be written as

${\displaystyle f(x,y)=\sum _{i=1}^{2}\sum _{j=1}^{2}a_{ij}x^{i-1}y^{j-1}=a_{11}+a_{21}x+a_{12}y+a_{22}xy,}$

where

{\displaystyle {\begin{aligned}a_{11}&=f(1,1),\\a_{21}&=f(2,1)-f(1,1),\\a_{12}&=f(1,2)-f(1,1),\\a_{22}&=f(2,2)+f(1,1)-{\big (}f(2,1)+f(1,2){\big )}.\end{aligned}}}

In both cases, the number of constants (four) correspond to the number of data points where f is given. The interpolant is linear along lines parallel to either the x or the y direction, equivalently if x or y is set constant. Along any other straight line, the interpolant is quadratic. However, even if the interpolation is not linear in the position (x and y), it is linear in the amplitude, as it is apparent from the equations above: all the coefficients aj, j = 1–4, are proportional to the value of the function f.

The result of bilinear interpolation is independent of which axis is interpolated first and which second. If we had first performed the linear interpolation in the y direction and then in the x direction, the resulting approximation would be the same.

The obvious extension of bilinear interpolation to three dimensions is called trilinear interpolation.

## Application in image processing

Comparison of Bilinear interpolation with some 1- and 2-dimensional interpolations. Black and red/yellow/green/blue dots correspond to the interpolated point and neighbouring samples, respectively. Their heights above the ground correspond to their values.

In computer vision and image processing, bilinear interpolation is used to resample images and textures. An algorithm is used to map a screen pixel location to a corresponding point on the texture map. A weighted average of the attributes (color, transparency, etc.) of the four surrounding texels is computed and applied to the screen pixel. This process is repeated for each pixel forming the object being textured.[3]

When an image needs to be scaled up, each pixel of the original image needs to be moved in a certain direction based on the scale constant. However, when scaling up an image by a non-integral scale factor, there are pixels (i.e., holes) that are not assigned appropriate pixel values. In this case, those holes should be assigned appropriate RGB or grayscale values so that the output image does not have non-valued pixels.

Bilinear interpolation can be used where perfect image transformation with pixel matching is impossible, so that one can calculate and assign appropriate intensity values to pixels. Unlike other interpolation techniques such as nearest-neighbor interpolation and bicubic interpolation, bilinear interpolation uses values of only the 4 nearest pixels, located in diagonal directions from a given pixel, in order to find the appropriate color intensity values of that pixel.

Bilinear interpolation considers the closest 2 × 2 neighborhood of known pixel values surrounding the unknown pixel's computed location. It then takes a weighted average of these 4 pixels to arrive at its final, interpolated value.[4]

Example of bilinear interpolation in grayscale values

As seen in the example on the right, the intensity value at the pixel computed to be at row 20.2, column 14.5 can be calculated by first linearly interpolating between the values at column 14 and 15 on each rows 20 and 21, giving

{\displaystyle {\begin{aligned}I_{20,14.5}&={\frac {15-14.5}{15-14}}\cdot 91+{\frac {14.5-14}{15-14}}\cdot 210=150.5,\\I_{21,14.5}&={\frac {15-14.5}{15-14}}\cdot 162+{\frac {14.5-14}{15-14}}\cdot 95=128.5,\end{aligned}}}

and then interpolating linearly between these values, giving

${\displaystyle I_{20.2,14.5}={\frac {21-20.2}{21-20}}\cdot 150.5+{\frac {20.2-20}{21-20}}\cdot 128.5=146.1.}$

This algorithm reduces some of the visual distortion caused by resizing an image to a non-integral zoom factor, as opposed to nearest-neighbor interpolation, which will make some pixels appear larger than others in the resized image.