帮助理解这些方程?

发布于 2024-09-10 11:54:20 字数 1251 浏览 2 评论 0原文

我问了一个关于双线性变换的问题并收到了这个答案:

从您发布的那个页面上,有一个指向源的链接 代码。 中解释双线性变换

我将在http://www.antigrain.com/ __code/include/agg_trans_bilinear.h.html

这里的想法是找到形式的变换:

output_x = a * input_x + b * input_x * input_y + c * input_y + d
output_y = e * input_x + f * input_x * input_y + g * input_y + h

术语“双线性”来自于每个线性方程 任一输入坐标本身。我们想要解决 a、b、c 和 d 的正确值。说你有参考资料 要映射到 (0,0), (1,0), (0,1) 的矩形 r1, r2, r3, r4, (1,1)(或某些图像坐标系)。

对于 a、b、c、d:

0 = a * r1_x + b * r1_x * r1_y + c * r1_y + d
1 = a * r2_x + b * r2_x * r2_y + c * r2_y + d
0 = a * r3_x + b * r3_x * r3_y + c * r3_y + d
1 = a * r4_x + b * r4_x * r4_y + c * r4_y + d

对于 e、f、g、h:

0 = e * r1_x + f * r1_x * r1_y + g * r1_y + h
0 = e * r2_x + f * r2_x * r2_y + g * r2_y + h
1 = e * r3_x + f * r3_x * r3_y + g * r3_y + h
1 = e * r4_x + f * r4_x * r4_y + g * r4_y + h

您可以按照您最喜欢的方式解决此问题。 (如果您熟悉 矩阵表示法,这是两个矩阵方程,其中矩阵 是一样的,然后只需要求LU分解即可 一次,并求解两个未知向量)。那么系数是 用于将矩形的内部映射到 长方形。


问题是,我有 input_x 和 input_y 以及 r1、r2、r3、r4,但我不确定如何实现 output_x 和 output_y。我该如何解这样的方程?我只熟悉解两个变量的方程。

谢谢

I asked a question regarding Bi linear transformations and received this answer:

From that very page you posted, there's a link to the source
code. I'll explain the bilinear transformation in

http://www.antigrain.com/__code/include/agg_trans_bilinear.h.html

The idea here is to find a transformation of the form:

output_x = a * input_x + b * input_x * input_y + c * input_y + d
output_y = e * input_x + f * input_x * input_y + g * input_y + h

The term "bilinear" comes from each of those equations being linear in
either of the input coordinates by themselves. We want to solve for
the right values of a, b, c, and d. Say you have the reference
rectangle r1, r2, r3, r4 which you want to map to (0,0), (1,0), (0,1),
(1,1) (or some image coordinate system).

For a,b,c,d:

0 = a * r1_x + b * r1_x * r1_y + c * r1_y + d
1 = a * r2_x + b * r2_x * r2_y + c * r2_y + d
0 = a * r3_x + b * r3_x * r3_y + c * r3_y + d
1 = a * r4_x + b * r4_x * r4_y + c * r4_y + d

For e,f,g,h:

0 = e * r1_x + f * r1_x * r1_y + g * r1_y + h
0 = e * r2_x + f * r2_x * r2_y + g * r2_y + h
1 = e * r3_x + f * r3_x * r3_y + g * r3_y + h
1 = e * r4_x + f * r4_x * r4_y + g * r4_y + h

You can solve this however you like best. (If you're familiar with
matrix notation, these are two matrix equations for which the matrix
is the same, and then you simply need to find the LU decomposition
once, and solve the two unknown vectors). The coefficients are then
applied to map the interior of the rectangle to the position in the
rectangle.


The problem is, I have input_x and input_y aswell as r1, r2, r3, r4, but I'm not sure how to achieve output_x and output_y. How do I solve such an equation? I'm only familiar with solving equations with 2 variables.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-09-17 11:54:20

你发现的这个解释太糟糕了。希望我能帮助您了解引擎盖下发生的事情。

您想要做的是将一个矩形区域(一个四边形)映射到另一个(任意形状)四边形。让我们从一个更简单的情况开始:线性插值(查找沿线的值)

如果有一条从 a 到 b 的线,那么您可以使用以下方法找到该线上的任何点“c”方程:

c = ((1 - p) * a) + (p * b);

该方程沿着两个位置之间的直线(线性)“混合”(插值)两个位置的值。如果使用 p=0,则方程变为,

c = (1 * a) + (0 * b)   = a

因此在 p=0 时,方程给出点“a”。

当 p=1 时,方程变为:

c = (0 * a) + (1 * b)   = b

因此,当 p=1 时,方程给出点“b”。

在 p 的中间值处,您可以在 a 和 b 之间的直线上获得点。 (因此,在 p=0.5 时,您得到的点正好位于 a 和 b 之间的中间位置)

因此,要将像素从屏幕上的一行复制到屏幕上的另一行,我们可以使用线性插值来查找行上的位置读取和写入。

for (float p = 0.0; p <= 1.0; p += 0.1)    // copy 10 pixels from line 1 to line 2
    DrawPixelOnLine2(p, GetPixelFromLine1(p));

这两种方法只使用线性插值来计算读取和绘制像素的正确位置,如下所示:(

int ax = 0, ay = 100, bx = 50, by = 170;   // line a-b goes from (0,100) to (50,170)

int px = ((1-p) * ax) + (p * bx);          // calc the x and y values separately
int py = ((1-p) * ay) + (p * by);

请注意,我必须使用计算两次才能完成 2D 位置。我可以通过简单地添加相同的计算来在 3D 中进行插值再次表示 az/bz/pz 坐标)

因此,现在您可以将点从一条任意直线复制到另一条直线。

双线性插值完全相同,只是我们想要在矩形内而不是直线内找到点,因此我们需要两个维度来描述该点在矩形中的位置(沿着矩形的 px)矩形的底部,然后向上绘制矩形的侧面)。现在您可以使用 (px,py) 坐标对定位矩形中的任意点。

为了进行上述映射,我们只需在二维上进行线性插值计算:首先,我们沿着底部和顶部进行插值,找到垂直穿过矩形中间的直线的两个端点。然后我们沿着这条垂直线进行插值以找到矩形中间的最终点。

“矩形”不一定是矩形。两个尺寸可用于任何 4 边形状(即,您可以将矩形的角移动到任何您喜欢的位置,方程仍会找到角之间的位置以填充形状所包围的区域)

在示例中,您发现,作者使用四个点来描述源形状(a,b,c,d),另外四个点来描述目标形状(e,f,g,h)

此外,他们还进行了更多计算通过求解方程来填充变换矩阵,可以将其与点相乘以进行映射过程,从而提高效率。不过,效果和我上面描述的一样。希望我的解释能让您更容易“了解”方程的作用。

That's a terrible explanation you've found. Hopefully I can help you understand what's going on under the bonnet.

What you want to do is map a rectangular area (one quadrilateral) into another (arbitrarily shaped) quadrilateral. Let's start with a simpler case: Linear interpolation (finding values that lie along a line)

If you have a line from a to b, then you can find any point 'c' on that line by using this equation:

c = ((1 - p) * a) + (p * b);

This 'mixes' (interpolates) the values of the two positions along the straight line between them (linearly). If you use p=0 then the equation turns into

c = (1 * a) + (0 * b)   = a

so at p=0 the equation gives you the point 'a'.

When p=1, the equation becomes:

c = (0 * a) + (1 * b)   = b

so at p=1, the equation gives you the point 'b'.

At intermediate values of p, you get points on the line between a and b. (so at p=0.5 you get the point exactly half way between a and b)

So, to copy the pixels from one line on the screen to another line on the screen, we can use linear interpolation to find the positions on the lines to read from and write to.

for (float p = 0.0; p <= 1.0; p += 0.1)    // copy 10 pixels from line 1 to line 2
    DrawPixelOnLine2(p, GetPixelFromLine1(p));

These two methods would just use linear interpolation to calculate the correct positions to read and draw the pixels, like this:

int ax = 0, ay = 100, bx = 50, by = 170;   // line a-b goes from (0,100) to (50,170)

int px = ((1-p) * ax) + (p * bx);          // calc the x and y values separately
int py = ((1-p) * ay) + (p * by);

(Note that I have to use the calculation twice to do a 2D position. I could interpolate in 3D by simply adding the same calculation agaian for the az/bz/pz coordinates)

So, now you can copy points from one arbitrary straight line to another.

bi-linear interpolation is exactly the same, except we want to find a point within a rectangle rather than within a line, so we need two dimensions to describe where the point is in the rectangle (px along the bottom of the rectangle, and py up the side of the rectangle). Now you can locate any point in the rectangle with an (px,py) coordinate pair.

To do the above mapping, we simply do the linear interpolation calculation in two dimensions: first we interpolate along the bottom and along the top to find two endpoints of a line that passes vertically through the middle of the rectangle. then we interpolate along this vertical line to find the final point in the middle of the rectangle.

The "rectangle" doesn't need to be rectangular. The two dimensions can be used on any 4-sided shape (i.e. you can move the corners of your rectangle anywhere you like and the equations will still find the positions between the corners to fill in the area enclosed by the shape)

In the example you have found, the author is using four points to describe the source shape (a,b,c,d) and another four points to describe the destination shape (e,f,g,h)

In addition, they have made the calculations more efficient by solving the equations to populate a transformation matrix which can be multiplied against points to do the mapping process. However, the effect is the same as what I've described above. Hopefully my explanation makes it a bit easier for you to 'see' what the equations are doing.

剩余の解释 2024-09-17 11:54:20

如果要将一个矩形映射到另一个矩形,则需要简单的线性变换。要将点 r 映射到点 q,您有:

qx = arx + bry + c
qy = drx + ery + f

有 6 个变量,您需要 6 个方程(3 分)。要将矩形 r 映射到矩形 q:

q1x = a*r1x + b*r1y + c
q1y = d*r1x + e*r1y + f
q2x = a*r2x + b*r2y + c
q2y = d*r2x + e*r2y + f
q3x = a*r3x + b*r3y + c
q3y = d*r3x + e*r3y + f

根据上面的内容,您必须求解 2 个由 3 个线性方程组组成的系统,每个方程组有 3 个变量。只要点不同且不共线,它们将恰好有 1 个解(在矩形角的情况下满足此要求)。

If you want to map a rectangle to another you will need simple linear transformation. To map point r to point q you have:

qx = arx + bry + c
qy = drx + ery + f

There is 6 variables, you'll need 6 equations (3 points). To map rectangle r to rectangle q:

q1x = a*r1x + b*r1y + c
q1y = d*r1x + e*r1y + f
q2x = a*r2x + b*r2y + c
q2y = d*r2x + e*r2y + f
q3x = a*r3x + b*r3y + c
q3y = d*r3x + e*r3y + f

From above you have to solve 2 systems of 3 linear equations with 3 variables each. They will have exactly 1 solution as long as points are different and not collinear (in case of rectangle corners this is fulfilled).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文