三维空间点至拟合直线距离

cv::Vec6d fitting_line;
int distType = cv::DIST_L2; // 距离类型  
double param = 0; // 距离参数  
double reps = 1e-2; // 径向的精度参数  
double aeps = 1e-2; // 角度精度参数  

fitLine(Points, fitting_line, distType, param, reps, aeps);
double DistanceOfPointToLine(cv::Point3f s, cv::Vec6d fitting_line)
{
	cv::Point3f a = cv::Point3f(fitting_line[3], fitting_line[4], fitting_line[5]);
	cv::Point3f b = cv::Point3f(fitting_line[3] + fitting_line[0], fitting_line[4] + fitting_line[1], fitting_line[5] + fitting_line[2]);
	double ab = sqrt(pow((a.x - b.x), 2.0) + pow((a.y - b.y), 2.0) + pow((a.z - b.z), 2.0));
	double as = sqrt(pow((a.x - s.x), 2.0) + pow((a.y - s.y), 2.0) + pow((a.z - s.z), 2.0));
	double bs = sqrt(pow((s.x - b.x), 2.0) + pow((s.y - b.y), 2.0) + pow((s.z - b.z), 2.0));
	double cos_A = (pow(as, 2.0) + pow(ab, 2.0) - pow(bs, 2.0)) / (2 * ab*as);
	double sin_A = sqrt(1 - pow(cos_A, 2.0));
	return as*sin_A;
}

参考:

https://blog.youkuaiyun.com/piaoxuezhong/article/details/71519426

在C#中实现三维空间直线拟合,可以采用最小二乘法(Least Squares Method)或主成分分析(Principal Component Analysis, PCA)来完成。这两种方法都可以用于三维空间中的直线拟合,并且在实际应用中具有较高的准确性与效率。 ### 1. 最小二乘法实现三维直线拟合 最小二乘法的基本思想是使所有拟合直线距离平方和最小化。在三维空间中,直线可以用-方向形式表示: $$ \mathbf{r}(t) = \mathbf{p}_0 + t\mathbf{d} $$ 其中,$\mathbf{p}_0$ 是直线上的一,$\mathbf{d}$ 是直线的方向向量,$t$ 是参数。 #### 实现步骤: 1. **计算集的质心(平均)**。 2. **构建协方差矩阵**。 3. **求解协方差矩阵的最大特征向量,作为直线的方向向量**。 4. **使用质心作为直线上的**。 ```csharp using System; using System.Collections.Generic; using MathNet.Numerics.LinearAlgebra; public class Line3DFit { public class Line { public Vector<double> Point { get; set; } public Vector<double> Direction { get; set; } } public static Line Fit(List<Vector<double>> points) { // 计算质心 int count = points.Count; Vector<double> centroid = Vector<double>.Build.Dense(3, 0); foreach (var point in points) { centroid += point; } centroid = centroid / count; // 构建协方差矩阵 Matrix<double> covariance = Matrix<double>.Build.Dense(3, 3, 0); foreach (var point in points) { Vector<double> diff = point - centroid; covariance += diff.OuterProduct(diff); } // 计算特征值和特征向量 var evd = covariance.Evd(); Vector<double> eigenValues = evd.EigenValues.Real(); Matrix<double> eigenVectors = evd.EigenVectors; // 找到最大特征值对应的特征向量 int maxIndex = 0; for (int i = 1; i < 3; i++) { if (eigenValues[i] > eigenValues[maxIndex]) maxIndex = i; } Vector<double> direction = eigenVectors.Column(maxIndex); // 返回直线 return new Line { Point = centroid, Direction = direction.Normalize(2) }; } } ``` ### 2. 使用示例 ```csharp using System; using System.Collections.Generic; using MathNet.Numerics.LinearAlgebra; class Program { static void Main() { var points = new List<Vector<double>> { Vector<double>.Build.Dense(new double[] { 1, 2, 3 }), Vector<double>.Build.Dense(new double[] { 2, 3, 4 }), Vector<double>.Build.Dense(new double[] { 3, 4, 5 }), Vector<double>.Build.Dense(new double[] { 4, 5, 6 }) }; var line = Line3DFit.Fit(points); Console.WriteLine("Point on line: " + string.Join(", ", line.Point)); Console.WriteLine("Direction: " + string.Join(", ", line.Direction)); } } ``` ### 3. 算法特与适用场景 - **最小二乘法**适用于数据中没有大量异常值的情况。如果数据中存在局外,则可以结合RANSAC算法进行鲁棒拟合[^1]。 - **PCA方法**本质上与最小二乘法一致,但其数学基础更清晰,适用于处理高维数据。 ### 4. 引用说明 上述实现结合了三维空间拟合直线的数学原理和C#编程实现,适用于一般情况下的直线拟合需求。若需要更高的鲁棒性,可以引入RANSAC算法来处理异常值问题。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大臉喵愛吃魚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值