如何在(x, y, z)空间中,基于一组三维数据点计算出最小二乘平面的算法?换言之,如果我有一堆点,例如(1,2,3),(4,5,6),(7,8,9),那么如何计算最适合的拟合平面f(x,y)=ax+by+c?从一组三维点中获取a、b和c的算法是什么?
如何在(x, y, z)空间中,基于一组三维数据点计算出最小二乘平面的算法?换言之,如果我有一堆点,例如(1,2,3),(4,5,6),(7,8,9),那么如何计算最适合的拟合平面f(x,y)=ax+by+c?从一组三维点中获取a、b和c的算法是什么?
如果你有 n 个数据点 (x[i], y[i], z[i]),计算出一个 3x3 对称矩阵 A,其条目为:
sum_i x[i]*x[i], sum_i x[i]*y[i], sum_i x[i]
sum_i x[i]*y[i], sum_i y[i]*y[i], sum_i y[i]
sum_i x[i], sum_i y[i], n
还需要计算三元素向量b:
{sum_i x[i]*z[i], sum_i y[i]*z[i], sum_i z[i]}
使用给定的A和b解Ax = b。 解向量的三个分量是最小二乘拟合平面的系数{a,b,c}。
请注意,这是“普通最小二乘”拟合,仅在z预计为x和y的线性函数时才适用。如果您更普遍地寻找三维空间中的“最佳拟合平面”,您可能需要了解“几何”最小二乘。
还要注意,如果您的点在一条直线上,则此方法将失败,就像您的示例点一样。
平面方程为:ax + by + c = z。因此,使用所有数据设置如下矩阵:
x_0 y_0 1
A = x_1 y_1 1
...
x_n y_n 1
a
x = b
c
并且
z_0
B = z_1
...
z_n
a
b = (A^T A)^-1 A^T B
c
以下是一个简单的Python代码示例:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
N_POINTS = 10
TARGET_X_SLOPE = 2
TARGET_y_SLOPE = 3
TARGET_OFFSET = 5
EXTENTS = 5
NOISE = 5
# create random data
xs = [np.random.uniform(2*EXTENTS)-EXTENTS for i in range(N_POINTS)]
ys = [np.random.uniform(2*EXTENTS)-EXTENTS for i in range(N_POINTS)]
zs = []
for i in range(N_POINTS):
zs.append(xs[i]*TARGET_X_SLOPE + \
ys[i]*TARGET_y_SLOPE + \
TARGET_OFFSET + np.random.normal(scale=NOISE))
# plot raw data
plt.figure()
ax = plt.subplot(111, projection='3d')
ax.scatter(xs, ys, zs, color='b')
# do fit
tmp_A = []
tmp_b = []
for i in range(len(xs)):
tmp_A.append([xs[i], ys[i], 1])
tmp_b.append(zs[i])
b = np.matrix(tmp_b).T
A = np.matrix(tmp_A)
fit = (A.T * A).I * A.T * b
errors = b - A * fit
residual = np.linalg.norm(errors)
print("solution:")
print("%f x + %f y + %f = z" % (fit[0], fit[1], fit[2]))
print("errors:")
print(errors)
print("residual:")
print(residual)
# plot plane
xlim = ax.get_xlim()
ylim = ax.get_ylim()
X,Y = np.meshgrid(np.arange(xlim[0], xlim[1]),
np.arange(ylim[0], ylim[1]))
Z = np.zeros(X.shape)
for r in range(X.shape[0]):
for c in range(X.shape[1]):
Z[r,c] = fit[0] * X[r,c] + fit[1] * Y[r,c] + fit[2]
ax.plot_wireframe(X,Y,Z, color='k')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
除非有人告诉我如何在此处输入等式,否则让我只写下您需要进行的最终计算:
首先,给定点r_i \n \R,i = 1..N,计算所有点的质心:
r_G = \frac{\sum_{i=1}^N r_i}{N}
接下来,通过计算3x3矩阵A来计算法向量n,它与基向量r_G一起定义了平面。
A = \sum_{i=1}^N (r_i - r_G)(r_i - r_G)^T
有了这个矩阵,法线向量n现在由A的对应于最小特征值的特征向量给出。
要了解特征向量/特征值对,请使用您选择的任何线性代数库。
此解决方案基于Hermitian矩阵A的Rayleight-Ritz定理。
请参考David Eberly的“数据最小二乘拟合”来了解我是如何得出这个结果的,以最小化几何拟合(点到平面的正交距离)。
bool Geom_utils::Fit_plane_direct(const arma::mat& pts_in, Plane& plane_out)
{
bool success(false);
int K(pts_in.n_cols);
if(pts_in.n_rows == 3 && K > 2) // check for bad sizing and indeterminate case
{
plane_out._p_3 = (1.0/static_cast<double>(K))*arma::sum(pts_in,1);
arma::mat A(pts_in);
A.each_col() -= plane_out._p_3; //[x1-p, x2-p, ..., xk-p]
arma::mat33 M(A*A.t());
arma::vec3 D;
arma::mat33 V;
if(arma::eig_sym(D,V,M))
{
// diagonalization succeeded
plane_out._n_3 = V.col(0); // in ascending order by default
if(plane_out._n_3(2) < 0)
{
plane_out._n_3 = -plane_out._n_3; // upward pointing
}
success = true;
}
}
return success;
}
在 Windows 7、i7、32位程序环境下,对1000个点拟合平面所需时间为37微秒。
float fitPlaneToSetOfPoints(const std::vector<cv::Point3f> &pts, cv::Point3f &p0, cv::Vec3f &nml) {
const int SCALAR_TYPE = CV_32F;
typedef float ScalarType;
// Calculate centroid
p0 = cv::Point3f(0,0,0);
for (int i = 0; i < pts.size(); ++i)
p0 = p0 + conv<cv::Vec3f>(pts[i]);
p0 *= 1.0/pts.size();
// Compose data matrix subtracting the centroid from each point
cv::Mat Q(pts.size(), 3, SCALAR_TYPE);
for (int i = 0; i < pts.size(); ++i) {
Q.at<ScalarType>(i,0) = pts[i].x - p0.x;
Q.at<ScalarType>(i,1) = pts[i].y - p0.y;
Q.at<ScalarType>(i,2) = pts[i].z - p0.z;
}
// Compute SVD decomposition and the Total Least Squares solution, which is the eigenvector corresponding to the least eigenvalue
cv::SVD svd(Q, cv::SVD::MODIFY_A|cv::SVD::FULL_UV);
nml = svd.vt.row(2);
// Calculate the actual RMS error
float err = 0;
for (int i = 0; i < pts.size(); ++i)
err += powf(nml.dot(pts[i] - p0), 2);
err = sqrtf(err / pts.size());
return err;
}
conv
函数是什么?它不是 OpenCV 的函数,对吧? - Szymon BednorzCGAL::linear_least_squares_fitting_3
写出平面的某种参数化方程,比如在三个参数中写成0 = ax + by + z + d
。
找到一个表达式D(\vec{v};a, b, d)
,表示任意点\vec{v}
到该平面的距离。
写出求和式S = \sigma_i=0,n D^2(\vec{x}_i)
,并简化它,使其用v
的各分量的简单求和来表示,例如\sigma v_x
,\sigma v_y^2
,\sigma v_x*v_z
等。
写出每个参数的最小化表达式dS/dx_0 = 0
,dS/dy_0 = 0
... 这给出了一个由三个参数和前一步的求和组成的三个方程组。
解这组方程得到参数。
请注意,如果您实际上只有三个点,则最好找到通过它们的平面。
此外,如果解析解不可行(对于平面不是这种情况,但一般可能存在),则可以执行步骤1和2,并在步骤3中使用Monte Carlo minimizer。
你需要做的就是解决这个方程组。
如果这些是你的点: (1, 2, 3), (4, 5, 6), (7, 8, 9)
那么你可以得到以下方程:
3=a*1 + b*2 + c
6=a*4 + b*5 + c
9=a*7 + b*8 + c
所以你的问题实际上应该是:如何解决一组方程?
因此,我建议阅读this这个SO问题。
如果我误解了你的问题,请告诉我们。
编辑:
请忽略我的答案,因为你可能想问其他问题。
我们首先提出了一种线性最小二乘平面拟合方法,该方法最小化估计法向量与提供的点之间的残差。
回想一下,通过原点的平面方程是Ax + By + Cz = 0,其中(x,y,z)可以是平面上的任意一点,(A,B,C)是垂直于该平面的法向量。
一般平面(可能通过原点或不通过原点)的方程是Ax + By + Cz + D = 0,其中附加系数D表示平面沿着法向量方向离原点有多远。【请注意,在此方程中,(A,B,C)形成一个单位法向量。】
现在,我们可以在这里应用一个技巧,并仅使用提供的点坐标来拟合平面。将两侧都除以D并将此项重新排列到右侧。这导致A/D x + B/D y + C/D z = -1。【请注意,在此方程中,(A/D,B/D,C/D)形成长度为1/D的法向量。】
我们可以相应地建立一个线性方程组,然后通过C++中的Eigen求解器解决它,如下所示。
// Example for 5 points
Eigen::Matrix<double, 5, 3> matA; // row: 5 points; column: xyz coordinates
Eigen::Matrix<double, 5, 1> matB = -1 * Eigen::Matrix<double, 5, 1>::Ones();
// Find the plane normal
Eigen::Vector3d normal = matA.colPivHouseholderQr().solve(matB);
// Check if the fitting is healthy
double D = 1 / normal.norm();
normal.normalize(); // normal is a unit vector from now on
bool planeValid = true;
for (int i = 0; i < 5; ++i) { // compare Ax + By + Cz + D with 0.2 (ideally Ax + By + Cz + D = 0)
if ( fabs( normal(0)*matA(i, 0) + normal(1)*matA(i, 1) + normal(2)*matA(i, 2) + D) > 0.2) {
planeValid = false; // 0.2 is an experimental threshold; can be tuned
break;
}
}
我们随后讨论它与典型的基于SVD的方法的等价性及其比较。
上述线性最小二乘(LLS)方法适用于拟合一般平面方程Ax + By + Cz + D = 0,而基于SVD的方法将D替换为D = - (Ax0 + By0 + Cz0),并拟合平面方程A(x-x0) + B(y-y0) + C(z-z0) = 0,其中(x0,y0,z0)是所有点的平均值,作为新局部坐标系的原点。
两种方法之间的比较:
我们最终提供了一个C++和MATLAB的测试用例。
// Test case in C++ (using LLS fitting method)
matA(0,0) = 5.4637; matA(0,1) = 10.3354; matA(0,2) = 2.7203;
matA(1,0) = 5.8038; matA(1,1) = 10.2393; matA(1,2) = 2.7354;
matA(2,0) = 5.8565; matA(2,1) = 10.2520; matA(2,2) = 2.3138;
matA(3,0) = 6.0405; matA(3,1) = 10.1836; matA(3,2) = 2.3218;
matA(4,0) = 5.5537; matA(4,1) = 10.3349; matA(4,2) = 1.8796;
// With this sample data, LLS fitting method can produce the following result
// fitted normal vector = (-0.0231143, -0.0838307, -0.00266429)
// unit normal vector = (-0.265682, -0.963574, -0.0306241)
// D = 11.4943
% Test case in MATLAB (using SVD-based method)
points = [5.4637 10.3354 2.7203;
5.8038 10.2393 2.7354;
5.8565 10.2520 2.3138;
6.0405 10.1836 2.3218;
5.5537 10.3349 1.8796]
covariance = cov(points)
[V, D] = eig(covariance)
normal = V(:, 1) % pick the eigenvector that corresponds to the smallest eigenvalue
% normal = (0.2655, 0.9636, 0.0306)