PCL使用RANSAC擬合三位平面


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM