双线性图像插值/缩放 - 计算示例 [英] Bilinear image interpolation / scaling - A calculation example

查看:165
本文介绍了双线性图像插值/缩放 - 计算示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你一些双线性插值/缩放细节。我们假设我们有这个矩阵:

I would like to ask you about some bilinear interpolation / scaling details. Let's assume that we have this matrix:

|100 | 50 |
|70  | 20 |

这是一张2 x 2灰度图像。现在,我想将它缩放两倍,我的矩阵看起来像这样:

This is a 2 x 2 grayscale image. Now, I would like scale it by factor of two and my matrix looks like this:

| 100   | f1 | 50 | f2 |
| f3    | f4 | f5 | f6 |
| 70    | f7 | 20 | f8 |

所以,如果我们想计算 f4 ,计算定义为

so if we would like to calculate f4, the calculation is defined as

f1 = 100 + 0.5(50 - 100) = 75
f7 = 70 +  0.5(20 - 70) = 45

现在终于:

f4 = 75 + 0.5(45 - 75) = 60

但是,我无法真正了解哪些计算适用于 f3 f1

However, I can't really understand what calculations are proper for f3 or f1

我们是否分别在每个方向进行双线性缩放?因此,这意味着:

Do we do the bilinear scaling in each direction separately? Therefore, this would mean that:

f3 = 100 + 0.5(70 - 100) = 85
f1 = 100 + 0.5(50 - 100) = 75

此外,我应该如何对待 f2,f6 ,f8 。这些点是否像在最近邻算法中一样被复制?

Also, how should I treat f2, f6, f8. Are those points simply being copied like in the nearest neighbor algorithm?

推荐答案

我想指出这个非常有见地的图形来自维基百科说明了如何对一点进行双线性插值:

I would like to point you to this very insightful graphic from Wikipedia that illustrates how to do bilinear interpolation for one point:

来源:维基百科

如您所见,这四个红点是已知的。您事先知道的这些点和 P 是我们希望插值的点。因此,我们必须做两个步骤(正如您在帖子中指出的那样)。要处理 x 坐标(水平),我们必须计算内插值对于红色顶行和红色底行的行。这导致两个蓝点 R1 R2 。要处理 y 坐标(垂直),我们使用两个蓝点并垂直插值以获得最终的 P 点。

As you can see, the four red points are what is known. These points you know before hand and P is the point we wish to interpolate. As such, we have to do two steps (as you have indicated in your post). To handle the x coordinate (horizontal), we must calculate what the interpolated value is row wise for the top row of red points and the bottom row of red points. This results in the two blue points R1 and R2. To handle the y coordinate (vertical), we use the two blue points and interpolate vertically to get the final P point.

当您调整图像大小时,即使我们看不到我要说的内容,但想象这个图像是 3D信号 f 。矩阵中的每个点实际上都是一个3D坐标,其中列位置是 x 值,行位置是 y value和 z 值是矩阵本身的数量/灰度值。因此,在(x,y)(x,y)的每个值都是从1到最多的行/列的整数。您正在查看的维度。

When you resize an image, even though we don't visually see what I'm about to say, but imagine that this image is a 3D signal f. Each point in the matrix is in fact a 3D coordinate where the column location is the x value, the row location is the y value and the z value is the quantity / grayscale value of the matrix itself. Therefore, doing z = f(x,y) is the value of the matrix at location (x,y) in the matrix. In our case, because you're dealing with images, each value of (x,y) are integers that go from 1 up to as many rows/columns as we have depending on what dimension you're looking at.

因此,给定您想要在(x,y),并给出上图中的红色坐标,我们称之为 x1,y1,x2,y2 ,如图所示 - 具体使用图表的约定并参考图像的访问方式: x1 = 1,x2 = 2,y1 = 2,y2 = 1 ,蓝色坐标 R1 R2 是通过1D插值列使用相同的行计算两个点重合:

Therefore, given the coordinate you want to interpolate at (x,y), and given the red coordinates in the image above, which we call them x1,y1,x2,y2 as per the diagram - specifically going with the convention of the diagram and referencing how images are accessed: x1 = 1, x2 = 2, y1 = 2, y2 = 1, the blue coordinates R1 and R2 are computed via 1D interpolation column wise using the same row both points coincide on:

R1 = f(x1,y1) + (x - x1)/(x2 - x1)*(f(x2,y1) - f(x1,y1))
R2 = f(x1,y2) + (x - x1)/(x2 - x1)*(f(x2,y2) - f(x1,y2))

重要的是要注意(x - x1)/(x2 - x1)重量/比例多少钱混合输出包含在 f(x1,y1) f(x2,y1)中看到的两个值之间对于 R1 f(x1,y2) f(x2,y2) for R2 。具体来说, x1 是起点,(x2 - x1) x的差异值。您可以验证替换 x1 x 给我们0,而 x2 as x 给我们1.此权重在 [0,1] 之间波动,这是计算所需的

It's important to note that (x - x1) / (x2 - x1) is a weight / proportion of how much of a mix the output consists of between the two values seen at f(x1,y1) and f(x2,y1) for R1 or f(x1,y2) and f(x2,y2) for R2. Specifically, x1 is the starting point and (x2 - x1) is the difference in x values. You can verify that substituting x1 as x gives us 0 while x2 as x gives us 1. This weight fluctuates between [0,1] which is required for the calculations to work.

应注意图片的原点位于左上角,所以 (1,1)位于左上角。一旦找到 R1 R2 ,我们就可以找到 P 通过逐行插值:

It should be noted that the origin of the image is at the top-left corner, and so (1,1) is at the top-left corner. Once you find R1 and R2, we can find P by interpolating row wise:

P = R2 + (y - y2)/(y2 - y1)*(R1 - R2)

再次,(y - y2)/(y2 - y1)表示 R1 R2 贡献给最终输出的比例/组合 P 。因此,您正确计算了 f5 ,因为您使用了四个已知点:左上角为100,右上角为50,左下角为70,右下角为20。如果你想计算 f5 ,这意味着(x,y)=(1.5,1.5)因为我们'因为您将图像缩放了两个,所以在100到50之间的中间位置。如果将这些值插入上面的计算中,您将获得预期的值60。两次计算的权重也将导致 0.5 ,这是你在计算中得到的,这就是我们所期望的。

Again, (y - y2) / (y2 - y1) denote the proportion / mix of how much R1 and R2 contribute to the final output P. As such, you calculated f5 correctly because you used four known points: The top left is 100, top right is 50, bottom left is 70 and bottom right is 20. Specifically, if you want to compute f5, this means that (x,y) = (1.5,1.5) because we're halfway in between the 100 and 50 due to the fact that you're scaling the image by two. If you plug in these values into the above computation, you will get the value of 60 as you expected. The weights for both calculations will also result in 0.5, which is what you got in your calculations and that's what we expect.

如果你计算 f1 ,这相当于(x,y)=(1.5 ,1)如果你将其替换为上面的等式,你会看到(y - y2)/(y2 - y1)给你0或权重为0,因此计算的只是 R2 ,对应于仅沿顶行的线性插值。类似地,如果我们计算 f7 ,这意味着我们想要插入(x,y)=(1.5,2)。在这种情况下,您将看到(y - y2)/(y2 - y1)为1或权重为1,因此 P = R2 +(R1 - R2),简化为 R1 ,并且只是底行的线性插值。

If you compute f1, this corresponds to (x,y) = (1.5,1) and if you substitute this into the above equation, you will see that (y - y2)/(y2 - y1) gives you 0 or the weight is 0, and so what is computed is just R2, corresponding to the linear interpolation along the top row only. Similarly, if we computed f7, this means we want to interpolate at (x,y) = (1.5,2). In this case, you will see that (y - y2) / (y2 - y1) is 1 or the weight is 1 and so P = R2 + (R1 - R2), which simplifies to R1 and is the linear interpolation along the bottom row only.

现在有 f3 f5 的情况。这些都对应于(x,y)=(1,1.5)(x,y)=(2,1.5)。将这些值替换为 R1 R2 P 两种情况都给出:

Now there's the case of f3 and f5. Those both correspond to (x,y) = (1,1.5) and (x,y) = (2,1.5) respectively. Substituting these values in for R1 and R2 and P for both cases give:

R1 = f(1,2) + (1 - 1)/(2 - 1)*(f(2,2) - f(1,2)) = f(1,2)
R2 = f(1,1) + (1 - 1)/(2 - 1)*(f(1,2) - f(1,1)) = f(1,1)
P = R1 + (1.5 - 1)*(R1 - R2) = f(1,2) + 0.5*(f(1,2) - f(1,1))

P = 70 + 0.5*(100 - 70) = 85



f5



f5

R1 = f(1,2) + (2 - 1)/(2 - 1)*(f(2,2) - f(1,2)) = f(2,2)
R2 = f(1,1) + (2 - 1)/(2 - 1)*(f(1,2) - f(1,1)) = f(1,2)
P = R1 + (1.5 - 1)*(R1 - R2) = f(2,2) + 0.5*(f(2,2) - f(1,2))

P = 20 + 0.5*(50 - 20) = 35

那么这告诉我们什么?这意味着您只沿y方向插入 。当我们看一下 P 时,这是显而易见的。每个 f3 f5 P 检查计算更彻底c>,您看到我们只考虑垂直方向的值。

So what does this tell us? This means that you are interpolating along the y-direction only. This is apparent when we take a look at P. Examining the calculations more thoroughly of P for each of f3 and f5, you see that we are considering values along the vertical direction only.

因此,如果您想要明确答案, f1 f7 是通过沿着 x /列方向插入而找到的行。通过插值 y /行方向找到 f3 f5 沿着同一列。 f4 使用 f1 f7 的混合来计算你已经看到的最终价值。

As such, if you want a definitive answer, f1 and f7 are found by interpolating along the x / column direction only along the same row. f3 and f5 are found by interpolating y / row direction along the same column. f4 uses a mixture of f1 and f7 to compute the final value as you have already seen.

要回答你的最后一个问题, f2 f6 f8 根据个人喜好填写。这些值被认为是超出范围的, x y 值都是 2.5 并且在我们的 [1,2] 网格之外(x,y)。在MATLAB中,默认实现是将定义边界之外的任何值填充为非数字( NaN ),但有时人们使用线性插值进行外推,复制边框值,或执行一些精细的填充,如对称或圆形填充。这取决于你所处的情况,但没有正确和明确的答案如何填写 f2 f6 f8 - 这一切都取决于你的申请以及对你最有意义的事情。

To answer your final question, f2, f6 and f8 are filled in based on personal preference. These values are considered to be out of bounds, with the x and y values both being 2.5 and that's outside of our [1,2] grid for (x,y). In MATLAB, the default implementation of this is to fill any values outside of the defined boundaries to be not-a-number (NaN), but sometimes, people extrapolate using linear interpolation, copy the border values, or perform some elaborate padding like symmetric or circular padding. It depends on what situation you're in, but there is no correct and definitive answer on how to fill in f2, f6 and f8 - it all depends on your application and what makes the most sense to you.

作为奖励,我们可以在MATLAB中验证我的计算是否正确。我们首先在 [1,2] 范围内定义一个(x,y)点的网格,然后调整大小图像,这是它的两倍大,我们指定每点0.5的分辨率,而不是1.我将调用你定义的矩阵 A

As a bonus, we can verify that my calculations are correct in MATLAB. We first define a grid of (x,y) points in the [1,2] range, then resize the image so that it's twice as large where we specify a resolution of 0.5 per point rather than 1. I'm going to call your defined matrix A:

A = [100 50; 70 20]; %// Define original matrix
[X,Y] = meshgrid(1:2,1:2); %// Define original grid of points
[X2,Y2] = meshgrid(1:0.5:2.5,1:0.5:2.5) %// Define expanded grid of points
B = interp2(X,Y,A,X2,Y2,'linear'); %// Perform bilinear interpolation

原始(x,y)点数网格如下:

>> X

X =

     1     2
     1     2

>> Y

Y =

     1     1
     2     2

展开的网格将矩阵的大小扩展两倍:

The expanded grid to expand the size of the matrix by twice as much looks like:

>> X2

X2 =

    1.0000    1.5000    2.0000    2.5000
    1.0000    1.5000    2.0000    2.5000
    1.0000    1.5000    2.0000    2.5000
    1.0000    1.5000    2.0000    2.5000

>> Y2

Y2 =

    1.0000    1.0000    1.0000    1.0000
    1.5000    1.5000    1.5000    1.5000
    2.0000    2.0000    2.0000    2.0000
    2.5000    2.5000    2.5000    2.5000

B 是使用的输出X Y 作为点的原始网格, X2 Y2 是我们想要插入的点。

B is the output using X and Y as the original grid of points and X2 and Y2 are the points we want to interpolate at.

我们得到:

>> B

B =

   100    75    50   NaN
    85    60    35   NaN
    70    45    20   NaN
   NaN   NaN   NaN   NaN

这篇关于双线性图像插值/缩放 - 计算示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆