1、插值的计算公式
y = ( x 2 − x ) x 2 − x 1 y 1 + ( x 2 − x 1 ) x 2 − x 1 y 2 \begin{aligned} y & =\frac{\left(x_{2}-x\right)}{x_{2}-x_{1}} y_{1} +\frac{\left(x_{2}-x_{1}\right)}{x_{2}-x_{1}} y_{2}\end{aligned} y=x2−x1(x2−x)y1+x2−x1(x2−x1)y2
2、新的索引坐标计算公式
w e i g h t ( t 1 , t 2 ) = w e i g h t ( t 1 ) + w e i g h t ( t 2 ) offset ( t 1 , t 2 ) = offset ( t 1 ) ⋅ weight ( t 1 ) + offset ( t 2 ) ⋅ weight ( t 2 ) weight ( t 1 , t 2 ) \begin{aligned} weight\left(t_{1}, t_{2}\right) &= weight\left(t_{1}\right)+ weight\left(t_{2}\right) \\ \operatorname{offset}\left(t_{1}, t_{2}\right) &=\frac{\text {offset}\left(t_{1}\right) \cdot \text {weight}\left(t_{1}\right)+\text {offset}\left(t_{2}\right) \cdot \text {weight}\left(t_{2}\right)}{\text {weight}\left(t_{1}, t_{2}\right)} \end{aligned} weight(t1,t2)offset(t1,t2)=weight(t1)+weight(t2)=weight(t1,t2)offset(t1)⋅weight(t1)+offset(t2)⋅weight(t2)
其中,weight(t1) 和 weight(t2) 是相邻的两个点的权重,offset(t1) 和 offset(t2)是相邻的两个的偏移量,下面是两个例子,基于 9x9 的高斯核(一半,因为高斯核是对称的):
o f f s e t [ 5 ] = ( 0.0 , 1.0 , 2.0 , 3.0 , 4.0 ) ; w e i g h t [ 5 ] = ( 0.2270270270 , 0.1945945946 , 0.1216216216 , 0.0540540541 , 0.0162162162 ) ; \begin{aligned} \begin{array}{l}offset [5] &= (0.0,\quad1.0,\quad2.0,\quad3.0,\quad4.0) \text {; } \\ weight [5] &= (0.2270270270,\quad0.1945945946,\quad0.1216216216, \quad 0.0540540541, \quad 0.0162162162) ;\end{array} \end{aligned} offset[5]weight[5]=(0.0,1.0,2.0,3.0,4.0); =(0.2270270270,0.1945945946,0.1216216216,0.0540540541,0.0162162162);
经过计算后的的新权重和坐标为:
offset [3] = ( 0.0 , 1.3846153846 , 3.2307692308 ) ; weight [3] = ( 0.2270270270 , 0.3162162162 , 0.0702702703 ) \begin{array}{l}\text { offset [3] }=(0.0,\quad1.3846153846,\quad3.2307692308) ; \\ \text { weight [3] }=(0.2270270270,\quad0.3162162162,\quad0.0702702703)\end{array} offset [3] =(0.0,1.3846153846,3.2307692308); weight [3] =(0.2270270270,0.3162162162,0.0702702703)
简单理解一下计算过程,就是 1 和 2 、3 和 4 两组像素点的偏移和权重带入公式的计算结果
3、推理过程
理解 1
offset(t1, t2) 是一个基于高斯权重的加权平均值,算出的是一个 offset(t1) 和 offset(2) 中间的一个偏移值,然后硬件基于这个偏移做一次插值,offset(t1,t2) 这个在 GPU 中是一个归一化的值
y = ( 1 − o f f s e t ( t 1 , t 2 ) ) ∗ y 1 + o f f s e t ( t 1 , t 2 ) ∗ y 2 y = (1-offset(t1,t2))*y1 + offset(t1,t2)*y2 y=(1−offset(t1,t2))∗y1+offset(t1,t2)∗y2
后续基于硬件插值的插值结果和 weight(t1,t2) 相乘
y = y ∗ w e i g h t ( t 1 , t 2 ) y = y * weight(t1,t2) y=y∗weight(t1,t2)
对于这个理解,首先高斯权重的和为1,其次新的y(插值结果)是两个点共同作用的结果,那么插值的权重也对应于两个点的权重
理解2
将 offset(t1,t2)带入插值的计算公式中,有以下的过程
y = x 2 − x x 2 − x 1 y 1 + x − x 1 x 2 − x 1 y 2 = x 2 − offset ( t 1 , t 2 ) x 2 − x 1 y 1 + offset ( t 1 , t 2 ) − x 1 x 2 − x 1 y 2 = x 2 ( t 1 + t 2 ) − ( x 1 t 1 + x 2 t 2 ) ( x 2 − x 1 ) ( t 1 + t 2 ) y 1 + ( x 1 t 1 + x 2 t 2 ) − x 1 ( t 1 + t 2 ) ( x 2 − x 1 ) ( t 1 + t 2 ) y 2 = t 1 t 1 + t 2 y 1 + t 2 t 1 + t 2 y 2 \begin{align} y &= \frac{x_2 - x}{x_2 - x_1} y_1 + \frac{x - x_1}{x_2 - x_1} y_2 \\ &= \frac{x_2 - \text{offset}(t_1, t_2)}{x_2 - x_1} y_1 + \frac{\text{offset}(t_1, t_2) - x_1}{x_2 - x_1} y_2 \\ &= \frac{x_2(t_1 + t_2) - (x_1 t_1 + x_2 t_2)}{(x_2 - x_1)(t_1 + t_2)} y_1 + \frac{(x_1 t_1 + x_2 t_2) - x_1 (t_1 + t_2)}{(x_2 - x_1)(t_1 + t_2)} y_2 \\ &= \frac{t_1}{t_1 + t_2} y_1 + \frac{t_2}{t_1 + t_2} y_2 \end{align} y=x2−x1x2−xy1+x2−x1x−x1y2=x2−x1x2−offset(t1,t2)y1+x2−x1offset(t1,t2)−x1y2=(x2−x1)(t1+t2)x2(t1+t2)−(x1t1+x2t2)y1+(x2−x1)(t1+t2)(x1t1+x2t2)−x1(t1+t2)y2=t1+t2t1y1+t1+t2t2y2
这是新的插值计算结果,然后的计算逻辑是
y = y ∗ w e i g h t ( t 1 , t 2 ) = ( t 1 t 1 + t 2 ∗ y 1 + t 2 t 1 + t 2 ∗ y 2 ) ∗ ( t 1 + t 2 ) = y 1 ∗ t 1 + y 2 ∗ t 2 \begin{align} y &= y * weight(t1,t2) \\ &= (\frac{t_1}{t_1+t_2}*y1+\frac{t_2}{t_1+t_2}*y2) * (t_1 + t_2) \\ &= y_1*t_1 + y_2*t_2 \end{align} y=y∗weight(t1,t2)=(t1+t2t1∗y1+t1+t2t2∗y2)∗(t1+t2)=y1∗t1+y2∗t2
这和高斯权重的计算结果一致