1、使用PCL工具
1 //创建一个模型参数对象,用于记录结果
2 pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
3 //inliers表示误差能容忍的点,记录点云序号
4 pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
5 //创建一个分割器
6 pcl::SACSegmentation<pcl::PointXYZ> seg;
7 //Optional,设置结果平面展示的点是分割掉的点还是分割剩下的点
8 seg.setOptimizeCoefficients(true);
9 //Mandatory-设置目标几何形状
10 seg.setModelType(pcl::SACMODEL_PLANE);
11 //分割方法:随机采样法
12 seg.setMethodType(pcl::SAC_RANSAC);
13 //设置误差容忍范围,也就是阈值
14 seg.setDistanceThreshold(0.01);
15 //输入点云
16 seg.setInputCloud (cloud);
17 //分割点云
18 seg.segment (*inliers, *coefficients);
2、RANSAC拟合平面代码
while ((iterNum < iter_maxNum) && inPlaneNum_max <= RSample_pointsNum)
{
inPlaneNum_t = 3;//当前拟合平面中点个数
//随机抽3个点,验证不在一条直线上
/*A(x1,y1)、B(x2,y2)、C(x3,y3)
AB斜率:kAB=(y2-y1)/(x2-x1)
BC斜率:kBC=(y3-y2)/(x3-x2)
计算结果可得:kAB=kBC
因为kAB=kBC,且共点B
所以直线AB与直线BC共线。*/
do {
rand_i_1 = real(gen);
rand_i_2 = real(gen);
if (rand_i_1 == rand_i_2)continue;
rand_i_3 = real(gen);
if (rand_i_1 == rand_i_3 || rand_i_2 == rand_i_3)continue;
x1 = r_sample[rand_i_1].x; x2 = r_sample[rand_i_2].x; x3 = r_sample[rand_i_3].x;
y1 = r_sample[rand_i_1].y; y2 = r_sample[rand_i_2].y; y3 = r_sample[rand_i_3].y;
} while (((y2 - y1)*(x3 - x2)) == ((y3 - y2)*(x2 - x1)));
//x1 = r_sample[rand_i_1].x; x2 = r_sample[rand_i_2].x; x3 = r_sample[rand_i_3].x;
//y1 = r_sample[rand_i_1].y; y2 = r_sample[rand_i_2].y; y3 = r_sample[rand_i_3].y;
z1 = r_sample[rand_i_1].z; z2 = r_sample[rand_i_2].z; z3 = r_sample[rand_i_3].z;
//求平面方程
A_t = (y2 - y1)*(z3 - z1) - (z2 - z1)*(y3 - y1);
B_t = (x3 - x1)*(z2 - z1) - (x2 - x1)*(z3 - z1);
C_t = (x2 - x1)*(y3 - y1) - (x3 - x1)*(y2 - y1);
D_t = -(A_t * x1 + B_t * y1 + C_t * z1);
//求在平面内的点的个数
temp = sqrt(A_t*A_t + B_t*B_t + C_t*C_t);//点到平面距离参数
for (int i = 0; i < RSample_pointsNum; i++)
{
temp_D = abs(A_t*r_sample[i].x + B_t*r_sample[i].y + C_t*r_sample[i].z + D_t) / temp;//点到平面距离
if (temp_D < maxD)
{
inPlaneNum_t++;
}
}
//与最优(最大)个数比较,保留最优个数的平面公式
if (inPlaneNum_t > inPlaneNum_max)
{
A_best = A_t;
B_best = B_t;
C_best = C_t;
D_best = D_t;
inPlaneNum_max = inPlaneNum_t;
}
iterNum++;//迭代次数+1
}
3、多点情况迭代次数的计算(转载于https://www.cnblogs.com/littlepear/p/10129861.html)
4、关于RANSAC算法https://blog.csdn.net/weixin_43795395/article/details/90751650讲得很好,可以参考
5、自适应阈值https://blog.csdn.net/hanshuobest/article/details/73718440