双线性插值(Bilinear Interpolation)

双线性插值(Bilinear Interpolation)


在这里插入图片描述

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.

在这里插入图片描述

图像双线性插值

在图像双线性插值中,有时候我们需要知道一个位置的像素值,而这个位置恰好不在像素点上(对应坐标一般来说不是整数,而非整数的坐标是无法在图像这种离散数据上使用的),因此,要用该位置周围的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,那么源图像和目标图像各个像素之间的对应关系如下:

在这里插入图片描述

只画了一行,用做示意,从图中可以很明显的看到,如果选择左上角为原点(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)()

在这里插入图片描述

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

实际应用举例

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。

  • 10
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bilinear interpolation双线性插值)是一种用于在离散数据点之间进行插值的方法。它通过在两个方向上进行线性插值来估计未知点的值。这种插值方法常用于图像处理和计算机图形学中。 双线性插值的原理是基于两个相邻的数据点之间的线性插值。首先,根据给定的坐标找到四个最近的数据点,然后在水平和垂直方向上进行线性插值,以获得未知点的值。 以下是一个使用双线性插值的示例代码[^1]: ```python import numpy as np def bilinear_interpolation(x, y, points): x1, y1 = points[0] x2, y2 = points[1] q11 = points[2] q12 = points[3] q21 = points[4] q22 = points[5] f = 1 / ((x2 - x1) * (y2 - y1)) value = f * ( q11 * (x2 - x) * (y2 - y) + q21 * (x - x1) * (y2 - y) + q12 * (x2 - x) * (y - y1) + q22 * (x - x1) * (y - y1) ) return value # 示例数据点 points = np.array([ [0, 0, 1, 3, 2, 4], [0, 1, 6, 8, 7, 9] ]) # 插值点坐标 x = 0.5 y = 0.5 # 进行双线性插值 result = bilinear_interpolation(x, y, points) print("Interpolated value:", result) ``` 这段代码中,我们定义了一个`bilinear_interpolation`函数,它接受插值点的坐标和四个最近的数据点的值作为输入。然后,根据双线性插值的公式计算出插值点的值,并返回结果。 在上面的示例中,我们使用了一个简单的二维数组作为示例数据点,然后对坐标为(0.5, 0.5)的点进行双线性插值。最后,输出插值点的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值