Linear,Bilinear,Trilinear Interpolation

from wiki

线性插值____



In this geometric visualisation, the value at the green circle multiplied by the distance between the red and blue circles is equal to the sum of the value at the red circle multiplied by the distance between the green and blue circles, and the value at the blue circle multiplied by the distance between the green and red circles.

If the two known points are given by the coordinates {\displaystyle (x_{0},y_{0})}(x_{0},y_{0}) and {\displaystyle (x_{1},y_{1})}(x_{1},y_{1}), the linear interpolant is the straight line between these points. For a value x in the interval {\displaystyle (x_{0},x_{1})}(x_{0},x_{1}), the value y along the straight line is given from the equation

{\displaystyle {\frac {y-y_{0}}{x-x_{0}}}={\frac {y_{1}-y_{0}}{x_{1}-x_{0}}},} {\displaystyle {\frac {y-y_{0}}{x-x_{0}}}={\frac {y_{1}-y_{0}}{x_{1}-x_{0}}},}

which can be derived geometrically from the figure on the right. It is a special case of polynomial interpolation with n = 1.

Solving this equation for y, which is the unknown value at x, gives

{\displaystyle y=y_{0}+(x-x_{0}){\frac {y_{1}-y_{0}}{x_{1}-x_{0}}},} {\displaystyle y=y_{0}+(x-x_{0}){\frac {y_{1}-y_{0}}{x_{1}-x_{0}}},}

which is the formula for linear interpolation in the interval {\displaystyle (x_{0},x_{1})}(x_{0},x_{1}). Outside this interval, the formula is identical to linear extrapolation.

This formula can also be understood as a weighted average. The weights are inversely related to the distance from the end points to the unknown point; the closer point has more influence than the farther point. Thus, the weights are {\textstyle {\frac {x-x_{0}}{x_{1}-x_{0}}}}{\textstyle {\frac  {x-x_{0}}{x_{1}-x_{0}}}} and {\textstyle {\frac {x_{1}-x}{x_{1}-x_{0}}}}{\textstyle {\frac  {x_{1}-x}{x_{1}-x_{0}}}}, which are normalized distances between the unknown point and each of the end points. Because these sum to 1,

{\displaystyle y=y_{0}\left(1-{\frac {x-x_{0}}{x_{1}-x_{0}}}\right)+y_{1}\left(1-{\frac {x_{1}-x}{x_{1}-x_{0}}}\right)=y_{0}\left(1-{\frac {x-x_{0}}{x_{1}-x_{0}}}\right)+y_{1}\left({\frac {x-x_{0}}{x_{1}-x_{0}}}\right),} {\displaystyle y=y_{0}\left(1-{\frac {x-x_{0}}{x_{1}-x_{0}}}\right)+y_{1}\left(1-{\frac {x_{1}-x}{x_{1}-x_{0}}}\right)=y_{0}\left(1-{\frac {x-x_{0}}{x_{1}-x_{0}}}\right)+y_{1}\left({\frac {x-x_{0}}{x_{1}-x_{0}}}\right),}

which yields the formula for linear interpolation given above.



双线性插值______

Algorithm[edit]

Suppose that we want to find the value of the unknown function f at the point (xy). 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}}}{\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}}}{\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.

Alternative algorithm[edit]

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}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}}}{\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}}

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})}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 found by solving

{\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)^{T}{\begin{bmatrix}1\\x\\y\\xy\end{bmatrix}}}{\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)^{T}{\begin{bmatrix}1\\x\\y\\xy\end{bmatrix}}

Unit Square[edit]

If we choose a coordinate system in which the four points where f is known are (0, 0), (0, 1), (1, 0), 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.}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}}.}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}}.
In this geometric visualisation, the value at the black spot is the sum of the value at each coloured spot multiplied by the area of the rectangle of the same colour, divided by the total area of all four rectangles.

Nonlinear[edit]

Contrary to what the name suggests, the bilinear interpolant is not linear; but it is the product of two linear functions.

Alternatively, the interpolant can be written as

{\displaystyle f(x,y)=\sum _{i=0}^{1}\sum _{j=0}^{1}a_{ij}x^{i}y^{j}=a_{00}+a_{10}x+a_{01}y+a_{11}xy}f(x,y)=\sum _{i=0}^{1}\sum _{j=0}^{1}a_{ij}x^{i}y^{j}=a_{00}+a_{10}x+a_{01}y+a_{11}xy

where

{\displaystyle a_{00}=f(0,0)}a_{00}=f(0,0)
{\displaystyle a_{10}=f(1,0)-f(0,0)}a_{10}=f(1,0)-f(0,0)
{\displaystyle a_{01}=f(0,1)-f(0,0)}a_{01}=f(0,1)-f(0,0)
{\displaystyle a_{11}=f(1,1)+f(0,0)-(f(1,0)+f(0,1))}a_{11}=f(1,1)+f(0,0)-(f(1,0)+f(0,1))

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 isnot linear in the position (x and y), it is linear in the amplitude, as it is apparent from the equations above: all the coefficient bj, 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.



三线性插值


Method[edit]

Eight corner points on a cube surrounding the interpolation point C
Depiction of 3D interpolation
A geometric visualisation of trilinear interpolation. The product of the value at the desired point and the entire volume is equal to the sum of the products of the value at each corner and the partial volume diagonally opposite the corner.

On a periodic and cubic lattice, let {\displaystyle x_{d}}x_{d}{\displaystyle y_{d}}y_{d}, and {\displaystyle z_{d}}z_{d} be the differences between each of {\displaystyle x}x{\displaystyle y}y{\displaystyle z}z and the smaller coordinate related, that is:

{\displaystyle \ x_{d}=(x-x_{0})/(x_{1}-x_{0})} \ x_{d}=(x-x_{0})/(x_{1}-x_{0})
{\displaystyle \ y_{d}=(y-y_{0})/(y_{1}-y_{0})} \ y_{d}=(y-y_{0})/(y_{1}-y_{0})
{\displaystyle \ z_{d}=(z-z_{0})/(z_{1}-z_{0})} \ z_{d}=(z-z_{0})/(z_{1}-z_{0})

where {\displaystyle x_{0}}x_{0} indicates the lattice point below {\displaystyle x}x, and {\displaystyle x_{1}}x_{1} indicates the lattice point above {\displaystyle x}x and similarly for {\displaystyle y_{0},y_{1},z_{0}}y_{0},y_{1},z_{0} and {\displaystyle z_{1}}z_{1}.

First we interpolate along {\displaystyle x}x (imagine we are pushing the front face of the cube to the back), giving:

{\displaystyle \ c_{00}=V[x_{0},y_{0},z_{0}](1-x_{d})+V[x_{1},y_{0},z_{0}]x_{d}} \ c_{​{00}}=V[x_{0},y_{0},z_{0}](1-x_{d})+V[x_{1},y_{0},z_{0}]x_{d}
{\displaystyle \ c_{01}=V[x_{0},y_{0},z_{1}](1-x_{d})+V[x_{1},y_{0},z_{1}]x_{d}} \ c_{​{01}}=V[x_{0},y_{0},z_{1}](1-x_{d})+V[x_{1},y_{0},z_{1}]x_{d}
{\displaystyle \ c_{10}=V[x_{0},y_{1},z_{0}](1-x_{d})+V[x_{1},y_{1},z_{0}]x_{d}} \ c_{​{10}}=V[x_{0},y_{1},z_{0}](1-x_{d})+V[x_{1},y_{1},z_{0}]x_{d}
{\displaystyle \ c_{11}=V[x_{0},y_{1},z_{1}](1-x_{d})+V[x_{1},y_{1},z_{1}]x_{d}} \ c_{​{11}}=V[x_{0},y_{1},z_{1}](1-x_{d})+V[x_{1},y_{1},z_{1}]x_{d}

Where {\displaystyle V[x_{0},y_{0},z_{0}]}V[x_{0},y_{0},z_{0}] means the function value of {\displaystyle (x_{0},y_{0},z_{0}).}(x_{0},y_{0},z_{0}). Then we interpolate these values (along {\displaystyle y}y, as we were pushing the top edge to the bottom), giving:

{\displaystyle \ c_{0}=c_{00}(1-y_{d})+c_{10}y_{d}} \ c_{0}=c_{​{00}}(1-y_{d})+c_{​{10}}y_{d}
{\displaystyle \ c_{1}=c_{01}(1-y_{d})+c_{11}y_{d}} \ c_{1}=c_{​{01}}(1-y_{d})+c_{​{11}}y_{d}

Finally we interpolate these values along {\displaystyle z}z(walking through a line):

{\displaystyle \ c=c_{0}(1-z_{d})+c_{1}z_{d}.} \ c=c_{0}(1-z_{d})+c_{1}z_{d}.

This gives us a predicted value for the point.

The result of trilinear interpolation is independent of the order of the interpolation steps along the three axes: any other order, for instance along {\displaystyle x}x, then along {\displaystyle y}y, and finally along {\displaystyle z}z, produces the same value.

The above operations can be visualized as follows: First we find the eight corners of a cube that surround our point of interest. These corners have the values C000, C100, C010, C110, C001, C101, C011, C111.

Next, we perform linear interpolation between C000 and C100 to find C00, C001 and C101 to find C01, C011 and C111 to find C11, C010 and C110 to find C10.

Now we do interpolation between C00 and C10 to find C0, C01 and C11 to find C1. Finally, we calculate the value C via linear interpolation of C0 and C1

In practice, a trilinear interpolation is identical to two bilinear interpolation combined with a linear interpolation:

{\displaystyle C\approx \ l(b(C_{000},C_{010},C_{100},C_{110}),b(C_{001},C_{011},C_{101},C_{111}))} C\approx \ l(b(C_{​{000}},C_{​{010}},C_{​{100}},C_{​{110}}),b(C_{​{001}},C_{​{011}},C_{​{101}},C_{​{111}}))
### 双线性插值概念 双线性插值是一种用于图像处理和计算机图形学中的基本技术,主要用于估计二维离散数据集之间的值。该方法通过四个最近邻像素的位置及其灰度值来计算目标位置的灰度值[^1]。 具体来说,在给定矩形网格上的已知函数值的情况下,可以利用这些点构建一个近似平面,从而得到任意位置处更精确的结果。这种方法不仅适用于单通道灰度图,也广泛应用于多通道彩色图片中。 ### 实现方法 以下是Python语言实现的一个简单版本: ```python import numpy as np def bilinear_interpolation(image, point): """ 对输入图像执行双线性插值 参数: image (numpy.ndarray): 输入图像矩阵. point (tuple of float): 需要插值得到的新坐标(x,y). 返回: interpolated_value (float or tuple): 插值得到的颜色/亮度值. """ x_floor = int(np.floor(point[0])) y_floor = int(np.floor(point[1])) dx = point[0] - x_floor dy = point[1] - y_floor Q11 = image[y_floor][x_floor] Q21 = image[y_floor][min(len(image[0])-1,x_floor+1)] Q12 = image[min(len(image)-1,y_floor+1)][x_floor] Q22 = image[min(len(image)-1,y_floor+1)][min(len(image[0])-1,x_floor+1)] if isinstance(Q11,int) or isinstance(Q11,float): R1 = (1-dx)*Q11 + dx*Q21 R2 = (1-dx)*Q12 + dx*Q22 P = (1-dy)*R1 + dy*R2 else: channels = len(Q11) P = [] for c in range(channels): R1_c = (1-dx)*Q11[c] + dx*Q21[c] R2_c = (1-dx)*Q12[c] + dx*Q22[c] Pc = (1-dy)*R1_c + dy*R2_c P.append(Pc) return P ``` 这段代码实现了基于四角权重加权平均的方法来进行双线性插值运算。对于RGB三通道颜色模型,则分别针对每个分量独立完成上述过程并最终组合成完整的色彩表示形式。 ### 应用场景 - **图像缩放**:当放大或缩小位图时,为了保持视觉质量而采用的技术之一就是双线性插值。 - **纹理映射**:在三维渲染过程中,物体表面贴附材质图案时常会遇到分辨率不匹配的问题;此时借助此算法可有效改善显示效果。 - **视频编码与解码**:某些压缩标准里也会涉及到帧间预测环节里的运动补偿操作,其中就可能运用到了类似的重采样手段以提高效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值