点云拟合直线轨迹(CloudComapre+PCL)

  1. ui添加

  1. 添加connect连接槽函数

connect(m_UI->actionLinePathFit, &QAction::triggered, this, &MainWindow::doActionLinePathFit);
  1. 添加函数声明

void doActionLinePathFit(); //直线轨迹
  1. 添加PCL头文件

#include <pcl/keypoints/uniform_sampling.h>            // 均匀采样
#include <pcl/sample_consensus/ransac.h>            //随机采样一致性算法实现
#include <pcl/sample_consensus/sac_model_line.h>    //直线采样
  1. 添加函数实现

void MainWindow::doActionLinePathFit()
{
    if (getSelectedEntities().size() != 1)
    {
        ccLog::Print(QStringLiteral("只能选择一个点云实体"));
        return;
    }
    ccHObject* entity = getSelectedEntities()[0];
    ccPointCloud* ccCloud = ccHObjectCaster::ToPointCloud(entity);
    // ---------------------------读取数据到PCL----------------------------------
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    cloud->resize(ccCloud->size());
    for (int i = 0; i < cloud->size(); ++i)
    {
        const CCVector3* point = ccCloud->getPoint(i);
        cloud->points[i].x = point->x;
        cloud->points[i].y = point->y;
        cloud->points[i].z = point->z;
    }


    //----------------------------- 空间直线拟合 -----------------------------
    pcl::SampleConsensusModelLine<pcl::PointXYZ>::Ptr model_line(new pcl::SampleConsensusModelLine<pcl::PointXYZ>(cloud));    //指定拟合点云与几何模型
    pcl::RandomSampleConsensus<pcl::PointXYZ> ransac(model_line);    //创建随机采样一致性对象
    ransac.setDistanceThreshold(1);    //设置容忍范围,也就是阈值
    ransac.setMaxIterations(10000);        //最大迭代次数
    ransac.computeModel();                //执行RANSAC空间直线拟合

    vector<int> inliers;                //存储内点索引的向量
    ransac.getInliers(inliers);            //提取内点对应的索引

    /// 根据索引提取内点
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_line(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::copyPointCloud<pcl::PointXYZ>(*cloud, inliers, *cloud_line);

    /// 模型参数
    Eigen::VectorXf coefficient;
    ransac.getModelCoefficients(coefficient);

    ccLog::Print(QStringLiteral("直线点向式方程为\n(x -%1) / %2 = (y - %3) / %4 = (z - %5) / %6").arg(coefficient[0]).arg(coefficient[1]).arg(coefficient[2]).arg(coefficient[3]).arg(coefficient[4]).arg(coefficient[5]));

    // ------------------------PCL->CloudCompare--------------------------------
    if (!cloud_line->empty())
    {
        ccPointCloud* Path = new ccPointCloud(QString("Path"));
        for (int i = 0; i < cloud_line->size(); ++i)
        {
            double x = cloud_line->points[i].x;
            double y = cloud_line->points[i].y;
            double z = cloud_line->points[i].z;
            Path->addPoint(CCVector3(x, y, z));
        }
        Path->setColor(ccColor::Rgba(rand() % 255, rand() % 255, 0, 255));
        Path->showColors(true);
        if (ccCloud->getParent())
        {
            ccCloud->getParent()->addChild(Path);
        }
        ccCloud->setEnabled(false);
        addToDB(Path);


        ccPointCloud* PointCloudafterPCL = new ccPointCloud(QString("PCL计算后的点云"));
        for (int i = 0; i < cloud->size(); ++i)
        {
            double x = cloud->points[i].x;
            double y = cloud->points[i].y;
            double z = cloud->points[i].z;
            PointCloudafterPCL->addPoint(CCVector3(x, y, z));
        }
        PointCloudafterPCL->setColor(ccColor::Rgba(rand() % 255, rand() % 255, 0, 255));
        PointCloudafterPCL->showColors(true);
        if (ccCloud->getParent())
        {
            ccCloud->getParent()->addChild(PointCloudafterPCL);
        }
        ccCloud->setEnabled(false);
        addToDB(PointCloudafterPCL);



        refreshAll();
        updateUI();
    }
    else
    {
        ccCloud->setEnabled(true);
        // Display a warning message in the console
        dispToConsole("Warning: example shouldn't be used as is", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
    }

}

结果显示1

主视图

左视图

显示结果2

主视图

左视图

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中实现多条点云拟合直线可以使用RANSAC算法,RANSAC可以在数据集中找到符合特定模型的数据子集,从而估计出模型参数。以下是一个简单的示例代码,用于从多条点云拟合直线: ```c++ #include <iostream> #include <pcl/point_types.h> #include <pcl/filters/voxel_grid.h> #include <pcl/features/normal_3d.h> #include <pcl/segmentation/extract_clusters.h> #include <pcl/segmentation/sac_segmentation.h> #include <pcl/sample_consensus/method_types.h> #include <pcl/sample_consensus/model_types.h> #include <pcl/ModelCoefficients.h> #include <pcl/kdtree/kdtree.h> #include <pcl/segmentation/extract_clusters.h> #include <pcl/filters/passthrough.h> #include <pcl/visualization/cloud_viewer.h> int main (int argc, char** argv) { //读入多条点云数据 std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> clouds; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile<pcl::PointXYZ>("cloud1.pcd", *cloud1); pcl::io::loadPCDFile<pcl::PointXYZ>("cloud2.pcd", *cloud2); clouds.push_back(cloud1); clouds.push_back(cloud2); //将多条点云合并为一个点云 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); for (int i = 0; i < clouds.size(); i++) *cloud += *clouds[i]; //对点云进行下采样 pcl::VoxelGrid<pcl::PointXYZ> sor; sor.setInputCloud(cloud); sor.setLeafSize(0.01f, 0.01f, 0.01f); sor.filter(*cloud); //创建RANSAC算法对象 pcl::SACSegmentation<pcl::PointXYZ> seg; seg.setOptimizeCoefficients(true); seg.setModelType(pcl::SACMODEL_LINE); seg.setMethodType(pcl::SAC_RANSAC); seg.setDistanceThreshold(0.01); seg.setInputCloud(cloud); //循环拟合直线 std::vector<pcl::ModelCoefficients> coefficients; std::vector<pcl::PointIndices> inliers; for (int i = 0; i < clouds.size(); i++) { //设置当前点云 seg.setInputCloud(clouds[i]); //拟合直线 pcl::ModelCoefficients coeff; pcl::PointIndices ind; seg.segment(ind, coeff); coefficients.push_back(coeff); inliers.push_back(ind); } //对拟合结果进行可视化 pcl::visualization::PCLVisualizer viewer("Line viewer"); viewer.setBackgroundColor(1, 1, 1); for (int i = 0; i < clouds.size(); i++) { //将拟合直线可视化 pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_handler(clouds[i], 255 - i * 50, 0, i * 50); viewer.addPointCloud<pcl::PointXYZ>(clouds[i], color_handler, "cloud" + std::to_string(i)); viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud" + std::to_string(i)); viewer.addLine<pcl::PointXYZ>(coefficients[i], "line" + std::to_string(i)); } viewer.spin(); return (0); } ``` 在上面的代码中,我们首先读入多条点云数据,将它们合并为一个点云,然后对点云进行下采样。接着,我们创建RANSAC算法对象,并设置模型类型为直线,距离阈值为0.01。最后,我们循环拟合每条点云中的直线,并将拟合结果可视化。 需要注意的是,由于RANSAC算法的结果受到随机性的影响,因此需要对多次运行结果进行平均。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值