加权最小二乘法 - 将平面拟合到 3D 点集

发布于 2025-01-04 20:33:35 字数 721 浏览 1 评论 0原文

我正在使用最小二乘法将平面拟合到 3D 点集。我已经有算法可以做到这一点,但我想修改它以使用加权最小二乘。这意味着我对每个点都有一个权重(权重越大,飞机应该离该点越近)。

当前的算法(无权重)如下所示:

计算总和:

for(Point3D p3d : pointCloud) {
    pos = p3d.getPosition();
    fSumX += pos[0];
    fSumY += pos[1];
    fSumZ += pos[2];
    fSumXX += pos[0]*pos[0];
    fSumXY += pos[0]*pos[1];
    fSumXZ += pos[0]*pos[2];
    fSumYY += pos[1]*pos[1];
    fSumYZ += pos[1]*pos[2];
}

然后制作矩阵:

double[][] A = {
    {fSumXX, fSumXY, fSumX},
    {fSumXY, fSumYY, fSumY},
    {fSumX,  fSumY,  pointCloud.size()}
};

double[][] B =  {
    {fSumXZ},
    {fSumYZ},
    {fSumZ}
};

然后求解 Ax = B 并且解的 3 个分量是拟合平面的系数...

那么,您能帮我吗?修改它以使用权重?谢谢!

I am fitting a plane to a 3D point set with the least square method. I already have algorithm to do that, but I want to modify it to use weighted least square. Meaning I have a weight for each point (the bigger weight, the closer the plane should be to the point).

The current algorithm (without weight) looks like this:

Compute the sum:

for(Point3D p3d : pointCloud) {
    pos = p3d.getPosition();
    fSumX += pos[0];
    fSumY += pos[1];
    fSumZ += pos[2];
    fSumXX += pos[0]*pos[0];
    fSumXY += pos[0]*pos[1];
    fSumXZ += pos[0]*pos[2];
    fSumYY += pos[1]*pos[1];
    fSumYZ += pos[1]*pos[2];
}

than make the matrices:

double[][] A = {
    {fSumXX, fSumXY, fSumX},
    {fSumXY, fSumYY, fSumY},
    {fSumX,  fSumY,  pointCloud.size()}
};

double[][] B =  {
    {fSumXZ},
    {fSumYZ},
    {fSumZ}
};

than solve Ax = B and the 3 components of the solution are the coefficients of the fitted plain...

So, can you please help me how to modify this to use weights? Thanks!

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

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

发布评论

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

评论(3

拥有 2025-01-11 20:33:35

直觉

由法线n定义的平面上的点x和平面p上的点遵循:n.(x - p) = 0。如果点 y 不在平面上,n.(y -p) 将不等于 0,因此定义成本的一个有用方法是通过 <代码>|n.(y - p)|^2 。这是点 y 到平面的平方距离。

在权重相等的情况下,您希望找到一个n,在对点求和时最小化总平方误差:

f(n) = sum_i | n.(x_i - p) |^2

现在假设我们知道一些p 位于飞机上。我们可以轻松地将 1 计算为质心,它只是点云中点的分量平均值,并且始终位于最小二乘平面中。

解决方案

让我们定义一个矩阵M,其中每行是第x_i减去质心c< /code>,我们可以重写:

f(n) = | M n |^2

您应该能够说服自己,这个矩阵乘法版本与上一个方程的总和相同。

然后,您可以对 M 进行奇异值分解,并且n 由对应于最小奇异值的 M 的右奇异向量给出。

要合并权重,您只需为每个点定义一个权重 w_i 即可。计算c作为点的加权平均值,并更改sum_i | n.(x_i - c) |^2sum_i | w_i * n.(x_i - c) |^2,矩阵M以类似的方式。然后像以前一样解决。

Intuition

A point x on a plane defined by normal n and a point on the plane p obeys: n.(x - p) = 0. If a point y does not lie on the plane, n.(y -p) will not be equal to zero, so a useful way to define a cost is by |n.(y - p)|^2 . This is the squared distance of the point y from the plane.

With equal weights, you want to find an n that minimizes the total squared error when summing over the points:

f(n) = sum_i | n.(x_i - p) |^2

Now this assumes we know some point p that lies on the plane. We can easily compute one as the centroid, which is simply the component-wise mean of the points in the point cloud and will always lie in the least-squares plane.

Solution

Let's define a matrix M where each row is the ith point x_i minus the centroid c, we can re-write:

f(n) = | M n |^2

You should be able to convince yourself that this matrix multiplication version is the same as the sum on the previous equation.

You can then take singular value decomposition of M, and the n you want is then given by the right singular vector of M that corresponds to the smallest singular value.

To incorporate weights you simply need to define a weight w_i for each point. Calculate c as the weighted average of the points, and change sum_i | n.(x_i - c) |^2 to sum_i | w_i * n.(x_i - c) |^2, and the matrix M in a similar way. Then solve as before.

绮烟 2025-01-11 20:33:35

将每个总和中的每一项乘以相应的权重。例如:

fSumZ += weight * pos[2];
fSumXX += weight * pos[0]*pos[0];

由于 pointCloude.size() 是所有点的 1 之和,因此应将其替换为所有权重之和。

Multiply each term in each sum by the corresponding weight. For example:

fSumZ += weight * pos[2];
fSumXX += weight * pos[0]*pos[0];

Since pointCloude.size() is the sum of 1 for all points, it should be replaced with the sum of all weights.

橘和柠 2025-01-11 20:33:35

从重新定义最小二乘误差计算开始。该公式试图最小化误差平方和。将平方误差乘以两点的函数,该函数随着距离的增加而减小。然后尝试最小化加权平方和并从中导出系数。

Start from redefining the least-square error calculation. The formula tries to minimize the sum of squares of errors. Multiply the squared error by a function of two points which decreases with their distance. Then try to minimize the weighted sum of squared errors and derive the coefficients from that.

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