Uniqki: A Personal Wiki Builder
A Uniqki site

# Bilinear interpolation

Contents

## 1   Algorithm

A digital image has the intensity function $$f(x,y)$$ at a point $$(x,y)$$. Any point $$(x, y)$$ has four neighbor pixels $$(x_1,y_1),\,(x_1,y_2),\,(x_2,y_1),\,\text{and}\,(x_2,y_2)$$, and we already know their pixel values $$f(x_1,y_1),\,f(x_1,y_2),\,f(x_2,y_1),\,\text{and}\,f(x_2,y_2)$$.

In the $$x$$-direction for $$y=y_1$$, $$\begin{split} f(x,y_1)&=f(x_1,y_1)+\frac{f(x_2,y_1)-f(x_1,y_1)}{x_2-x_1}(x-x_1)\\ &=\frac{f(x_1,y_1)(x_2-x_1)+f(x_2,y_1)(x-x_1)-f(x_1,y_1)(x-x_1)}{x_2-x_1}\\ &=\frac{f(x_1,y_1)(x_2-x_1-x+x_1)+f(x_2,y_1)(x-x_1)}{x_2-x_1}\\ &=\frac{x_2-x}{x_2-x_1}f(x_1,y_1)+\frac{x-x_1}{x_2-x_1}f(x_2,y_1) \end{split}$$ or $$\begin{split} f(x,y_1)&=\frac{f(x_2,y_1)-f(x_1,y_1)}{x_2-x_1}x+\frac{f(x_1,y_1)x_2-f(x_2,y_1)x_1}{x_2-x_1}\\ &=a_1x+b_1,\\ \\ a_1&=\frac{f(x_2,y_1)-f(x_1,y_1)}{x_2-x_1}\\ b_1&=\frac{f(x_1,y_1)x_2-f(x_2,y_1)x_1}{x_2-x_1}. \end{split}$$

Similarly for $$y=y_2$$, $$f(x,y_2)=\frac{x_2-x}{x_2-x_1}f(x_1,y_2)+\frac{x-x_1}{x_2-x_1}f(x_2,y_2)$$ or $$\begin{split} f(x,y_2)&=\frac{f(x_2,y_2)-f(x_1,y_2)}{x_2-x_1}x+\frac{f(x_1,y_2)x_2-f(x_2,y_2)x_1}{x_2-x_1}\\ &=a_2x+b_2,\\ \\ a_2&=\frac{f(x_2,y_2)-f(x_1,y_2)}{x_2-x_1}\\ b_2&=\frac{f(x_1,y_2)x_2-f(x_2,y_2)x_1}{x_2-x_1}. \end{split}$$

Now, in the $$y$$-direction, $$\begin{split} f(x,y)&=f(x,y_1)+\frac{f(x,y_2)-f(x,y_1)}{y_2-y_1}(y-y_1)\\ &=\frac{f(x,y_1)(y_2-y_1)+f(x,y_2)(y-y_1)-f(x,y_1)(y-y_1)}{y_2-y_1}\\ &=\frac{f(x,y_1)(y_2-y_1-y+y_1)+f(x,y_2)(y-y_1)}{y_2-y_1}\\ &=\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}f(x,y_1)+\frac{y-y_1}{y_2-y_1}f(x,y_2) \end{split}$$ or $$\begin{split} f(x,y)&=\frac{f(x,y_2)-f(x,y_1)}{y_2-y_1}y+\frac{f(x,y_1)y_2-f(x,y_2)y_1}{y_2-y_1}\\ &=\frac{a_2x+b_2-a_1x-b_1}{y_2-y_1}y+\frac{(a_1x+b_1)y_2-(a_2x+b_2)y_1}{y_2-y_1}\\ &=\frac{(a_2-a_1)xy+(b_2-b_1)y+(a_1y_2-a_2y_1)x+b_1y_2-b_2y_1}{y_2-y_1}\\ &=\frac{(a_1y_2-a_2y_1)x+(b_2-b_1)y+(a_2-a_1)xy+b_1y_2-b_2y_1}{y_2-y_1}\\ &=ax+by+cxy+d,\\ \\ a&=\frac{a_1y_2-a_2y_1}{y_2-y_1}\\ b&=\frac{b_2-b_1}{y_2-y_1}\\ c&=\frac{a_2-a_1}{y_2-y_1}\\ d&=\frac{b_1y_2-b_2y_1}{y_2-y_1}. \end{split}$$

## 2   Matrix method

We know that bilinear interpolation has a functional form of $$f(x, y)=ax+by+cxy+d$$ and has four neighbor pixels: $$(x_1,y_1),\,(x_1,y_2),\,(x_2,y_1),\,\text{and}\,(x_2,y_2)$$. At the same time, we already know the $$f(x,y)$$ values of these neighbor pixels and there are four unknown variables: $$a,\,b,\,c,\,\text{and}\,d$$. From the following system of equations: $$\begin{split} f(x_1,y_1)&=ax_1+by_1+cx_1y_1+d\\ f(x_1,y_2)&=ax_1+by_2+cx_1y_2+d\\ f(x_2,y_1)&=ax_2+by_1+cx_2y_1+d\\ f(x_2,y_2)&=ax_2+by_2+cx_2y_2+d, \end{split}$$ we can obtain this matrix equation: $$\begin{bmatrix} x_1& y_1& x_1y_1& 1\\ x_1& y_2& x_1y_2& 1\\ x_2& y_1& x_2y_1& 1\\ x_2& y_2& x_2y_2& 1 \end{bmatrix} \begin{bmatrix} a\\ b\\ c\\ d \end{bmatrix} = \begin{bmatrix} f(x_1,y_1)\\ f(x_1,y_2)\\ f(x_2,y_1)\\ f(x_2,y_2) \end{bmatrix}.$$ Now, the unknown variables can be obtained by $$\begin{bmatrix} a\\ b\\ c\\ d \end{bmatrix} = \begin{bmatrix} x_1& y_1& x_1y_1& 1\\ x_1& y_2& x_1y_2& 1\\ x_2& y_1& x_2y_1& 1\\ x_2& y_2& x_2y_2& 1 \end{bmatrix}^{-1} \begin{bmatrix} f(x_1,y_1)\\ f(x_1,y_2)\\ f(x_2,y_1)\\ f(x_2,y_2) \end{bmatrix}.$$ $$f(x,y)$$ is calculated as $$f(x,y)= \begin{bmatrix} x& y& xy& 1 \end{bmatrix} \begin{bmatrix} a\\ b\\ c\\ d \end{bmatrix} = \begin{bmatrix} x& y& xy& 1 \end{bmatrix} \begin{bmatrix} x_1& y_1& x_1y_1& 1\\ x_1& y_2& x_1y_2& 1\\ x_2& y_1& x_2y_1& 1\\ x_2& y_2& x_2y_2& 1 \end{bmatrix}^{-1} \begin{bmatrix} f(x_1,y_1)\\ f(x_1,y_2)\\ f(x_2,y_1)\\ f(x_2,y_2) \end{bmatrix}.$$