双线性插值(Bilinear Interpolation)

双线性插值(Bilinear Interpolation)

文章目录

  • 双线性插值(Bilinear Interpolation)
    • 线性插值
    • 双线性插值
      • 基本原理
      • 图像双线性插值
      • 存在的问题
        • 坐标系的选择
      • 实际应用举例

双线性插值(Bilinear Interpolation)_第1张图片

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.

线性插值

已知坐标 ( x 0 , y 0 ) (x_0, y_0) (x0,y0) ( x 1 , y 1 ) (x_1, y_1) (x1,y1),要得到 [ x 0 , x 1 ] [x_0, x_1] [x0,x1] 区间内某一位置 x x x在直线上的值。
y − y 0 x − x 0 = y 1 − y 0 x 1 − x 0 \frac {y - y_0} {x - x_0} = \frac {y_1 - y_0} {x_1 - x_0} xx0yy0=x1x0y1y0
由于 x x x值已知,所以可以从公式得到 y y y的值
y = x 1 − x x 1 − x 0 y 0 + x − x 0 x 1 − x 0 y 1 y = \frac {x_1 - x} {x_1 - x_0} y_0 + \frac {x - x_0} {x_1 - x_0} y_1 y=x1x0x1xy0+x1x0xx0y1
已知 y y y x x x的过程与以上过程相同,只是 x x x y y y要进行交换。

x x x x 0 x_0 x0 x 1 x_1 x1的距离作为一个权重,用于 y 0 y_0 y0 y 1 y_1 y1的加权。双线性插值本质上就是在两个方向上做线性插值。

双线性插值

基本原理

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 Q 11 = ( x 1 , y 1 ) Q_{11} = (x_1, y_1) Q11=(x1,y1), Q 12 = ( x 1 , y 2 ) Q_{12} = (x_1, y_2) Q12=(x1,y2), Q 21 = ( x 2 , y 1 ) Q_{21} = (x_2, y_1) Q21=(x2,y1), and Q 22 = ( x 2 , y 2 ) Q_{22} = (x_2, y_2) Q22=(x2,y2).

最常见的情况,f就是一个像素点的像素值
We first do linear interpolation in the x-direction. This yields
f ( R 1 ) = f ( x , y 1 ) ≈ x 2 − x x 2 − x 1 f ( Q 11 ) + x − x 1 x 2 − x 1 f ( Q 21 ) , f ( R 2 ) = f ( x , y 2 ) ≈ x 2 − x x 2 − x 1 f ( Q 12 ) + x − x 1 x 2 − x 1 f ( Q 22 ) . {\displaystyle {\begin{aligned}f(R_1)=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(R_2)=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}}} f(R1)=f(x,y1)f(R2)=f(x,y2)x2x1x2xf(Q11)+x2x1xx1f(Q21),x2x1x2xf(Q12)+x2x1xx1f(Q22).
We proceed by interpolating in the y-direction to obtain the desired estimate:
f ( x , y ) ≈ y 2 − y y 2 − y 1 f ( R 1 ) + y − y 1 y 2 − y 1 f ( R 2 ) = y 2 − y y 2 − y 1 ( x 2 − x x 2 − x 1 f ( Q 11 ) + x − x 1 x 2 − x 1 f ( Q 21 ) ) + y − y 1 y 2 − y 1 ( x 2 − x x 2 − x 1 f ( Q 12 ) + x − x 1 x 2 − x 1 f ( Q 22 ) ) = 1 ( x 2 − x 1 ) ( y 2 − y 1 ) ( 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 ) ) = 1 ( x 2 − x 1 ) ( y 2 − y 1 ) [ x 2 − x x − x 1 ] [ f ( Q 11 ) f ( Q 12 ) f ( Q 21 ) f ( Q 22 ) ] [ y 2 − y y − y 1 ] . {\displaystyle {\begin{aligned}f(x,y)&\approx {\frac {y_{2}-y}{y_{2}-y_{1}}}f(R_{1})+{\frac {y-y_{1}}{y_{2}-y_{1}}}f(R_{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})}}{\big (}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}){\big )}\\&={\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}}} f(x,y)y2y1y2yf(R1)+y2y1yy1f(R2)=y2y1y2y(x2x1x2xf(Q11)+x2x1xx1f(Q21))+y2y1yy1(x2x1x2xf(Q12)+x2x1xx1f(Q22))=(x2x1)(y2y1)1(f(Q11)(x2x)(y2y)+f(Q21)(xx1)(y2y)+f(Q12)(x2x)(yy1)+f(Q22)(xx1)(yy1))=(x2x1)(y2y1)1[x2xxx1][f(Q11)f(Q21)f(Q12)f(Q22)][y2yyy1].
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.

双线性插值(Bilinear Interpolation)_第2张图片

图像双线性插值

在图像双线性插值中,有时候我们需要知道一个位置的像素值,而这个位置恰好不在像素点上(对应坐标一般来说不是整数,而非整数的坐标是无法在图像这种离散数据上使用的),因此,要用该位置周围的4个像素点的值来估计该点的值,因为只会用相邻的4个点,所以上述公式的分母都是1。

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. [2]

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.[3]

存在的问题

坐标系的选择

要通过双线性插值的方法算出dst中每一个像素点的像素值,是通过dst像素点的坐标对应到src图像当中的坐标;然后通过双线性插值的方法算出src中相应坐标的像素值。

坐标对应关系:

➢按比例对应:

S r c X = ( d s t X ) ∗ ( s r c W i d t h / d s t W i d t h ) SrcX=(dstX)*(srcWidth/dstWidth) SrcX=(dstX)(srcWidth/dstWidth)

S r c Y = ( d s t Y ) ∗ ( s r c H e i g h t / d s t H e i g h t ) SrcY=(dstY)*(srcHeight/dstHeight) SrcY=(dstY)(srcHeight/dstHeight)

如果源图像和目标图像的原点(0,0)均选择左上角,然后根据插值公式计算目标图像每点像素,假设你需要将一幅5x5的图像缩小成3x3,那么源图像和目标图像各个像素之间的对应关系如下:

双线性插值(Bilinear Interpolation)_第3张图片

只画了一行,用做示意,从图中可以很明显的看到,如果选择左上角为原点(0,0),那么最右边和最下边的像素实际上并没有参与计算,而且目标图像的每个像素点计算出的灰度值也相对于源图像偏左偏上。那么,让坐标加1或者选择右下角为原点怎么样呢?很不幸,还是一样的效果,不过这次得到的图像将偏右偏下。最好的方法就是,两个图像的几何中心重合,并且目标图像的每个像素之间都是等间隔的,并且都和两边有一定的边距。如下面的做法。

➢按比例对应最后一列没有办法参与计算,所以按几何中心对应:

S r c X + 0.5 = ( d s t X + 0.5 ) ∗ ( s r c W i d t h / d s t W i d t h ) SrcX+0.5=(dstX+0.5)*(srcWidth/dstWidth) SrcX+0.5=(dstX+0.5)(srcWidth/dstWidth)

S r c Y + 0.5 = ( d s t Y + 0.5 ) ∗ ( s r c H e i g h t / d s t H e i g h t ) SrcY+0.5=(dstY+0.5)*(srcHeight/dstHeight) SrcY+0.5=(dstY+0.5)(srcHeight/dstHeight)

对于图片的几何中心,希望原图片和当前图片的几何中心重叠

原 图 片 大 小 − 1 2 + 0.5 = ( 当 前 图 片 大 小 − 1 2 + 0.5 ) ∗ ( 原 图 片 大 小 当 前 图 片 大 小 ) \frac {原图片大小 - 1} {2} + 0.5 = \left( \frac {当前图片大小 - 1} {2} + 0.5 \right)*\left( \frac {原图片大小} {当前图片大小} \right) 21+0.5=(21+0.5)()

双线性插值(Bilinear Interpolation)_第4张图片

注:几何中心对应中,如果索引是负值,实际上是从一行像素值的末端取值

实际应用举例

x ( p ) = ∑ q G ( q , p ) ⋅ x ( q ) x(p) = \sum_{q} G(q, p)\cdot x(q) x(p)=qG(q,p)x(q)

where p p p denotes an arbitrary (fractional) location, q q q enumerates all integral spatial locations in the feature map x x x, and G ( ⋅ , ⋅ ) G(\cdot, \cdot) G(,) is the bilinear interpolation kernel. Note that G G G is two dimensional. It is separated into two one dimensional kernels as
G ( q , p ) = g ( q x , p x ) ⋅ g ( q y , p y ) G(q, p) = g(q_x, p_x) \cdot g(q_y, p_y) G(q,p)=g(qx,px)g(qy,py)
where g ( a , b ) = max ⁡ ( 0 , 1 − ∣ a − b ∣ ) g(a,b)=\max(0, 1- \vert a-b\vert) g(a,b)=max(0,1ab)

els as
G ( q , p ) = g ( q x , p x ) ⋅ g ( q y , p y ) G(q, p) = g(q_x, p_x) \cdot g(q_y, p_y) G(q,p)=g(qx,px)g(qy,py)
where g ( a , b ) = max ⁡ ( 0 , 1 − ∣ a − b ∣ ) g(a,b)=\max(0, 1- \vert a-b\vert) g(a,b)=max(0,1ab)

该公式计算很快,因为只有位置 p p p周围的4个像素点的 G ( q , p ) G(q, p) G(q,p)值才不为0。

你可能感兴趣的:(#,人工智能笔记)