pcl点云处理

10 篇文章 11 订阅

原点云
在这里插入图片描述
1.统计滤波,去除离群点

#include "pch.h"
using namespace std;
#include <pcl/point_types.h>          //PCL中所有点类型定义的头文件
#include <pcl/io/pcd_io.h>            //打开关闭pcd文件的类定义的头文件
#include <pcl/filters/statistical_outlier_removal.h>

typedef pcl::PointXYZ PoinT;

int main(int argc, char** argv)
{
    pcl::PointCloud<PoinT>::Ptr cloud(new pcl::PointCloud<PoinT>());
    // 加载pcd文件
    pcl::io::loadPCDFile("desk.pcd", *cloud);
    std::cout << "Cloud before filtering: " << std::endl;
    std::cout << *cloud << std::endl;

    // 统计滤波
    // 创建滤波器,对每个点分析的临近点的个数设置为50 ,并将标准差的倍数设置为1  这意味着如果一
    // 个点的距离超出了平均距离一个标准差以上,则该点被标记为离群点,并将它移除
    pcl::StatisticalOutlierRemoval<PoinT> sor;   	// 创建滤波器对象
    sor.setInputCloud(cloud);                   	// 设置待滤波的点云
    sor.setMeanK(50);                           	// 设置在进行统计时考虑查询点临近点数
    sor.setStddevMulThresh(1);                      // 设置判断是否为离群点的阀值
    sor.filter(*cloud);                            	// 过滤
    // 储存
    pcl::io::savePCDFileBinary("desk-sor.pcd", *cloud);
    return 0;
}

在这里插入图片描述

2.直通滤波,去除室外点

	pcl::PassThrough<PoinT> pass;
    pass.setInputCloud(cloud);
    pass.setFilterFieldName("z");          	// 设置过滤时所需要点云类型的Z字段
    pass.setFilterLimits(-0.1, 10);       	// 设置在过滤字段的范围
    pass.setFilterLimitsNegative(true);   	// 保留还是过滤掉范围内的点
    pass.filter(*cloud);

3.平面分割

#include "pch.h"
using namespace std;
#include <pcl/point_types.h>          //PCL中所有点类型定义的头文件
#include <pcl/io/pcd_io.h>            //打开关闭pcd文件的类定义的头文件
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>

typedef pcl::PointXYZ PoinT;

// 随机产生颜色
int *rand_rgb() {
    int *rgb = new int[3];
    rgb[0] = rand() % 255;
    rgb[1] = rand() % 255;
    rgb[2] = rand() % 255;
    return rgb;
}

int main(int argc, char** argv)
{
    pcl::PointCloud<PoinT>::Ptr cloud(new pcl::PointCloud<PoinT>());
    // 加载pcd文件
    pcl::io::loadPCDFile("desk-sor.pcd", *cloud);
    std::cout << "Cloud before filtering: " << std::endl;
    std::cout << *cloud << std::endl;

    // 平面分隔
    pcl::PointCloud<PoinT>::Ptr cloud_filtered(new pcl::PointCloud<PoinT>());
    pcl::SACSegmentation<PoinT> seg;
    pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());// 创建参数模型的的参数对象
    pcl::PointIndices::Ptr inliers(new pcl::PointIndices());// 储存模型内点的下标索引的数组指针
    seg.setMethodType(pcl::SAC_RANSAC);     // 设置方法【聚类或随机样本一致性】
    seg.setModelType(pcl::SACMODEL_PLANE);  // 设置模型类型,检测平面
    seg.setMaxIterations(100);
    seg.setDistanceThreshold(0.01);
    // Extract the inliers
    pcl::ExtractIndices<PoinT> extract;
    extract.setNegative(true);
    int cloud_size = cloud->size();
    int i = 0;
    // 迭代进行分割直到剩余80%的点云
    while (cloud->size() > cloud_size * .8)
    {
        seg.setInputCloud(cloud);
        seg.segment(*inliers, *coefficients);// 传入的参数未进行赋值  利用参数模型进行分割
        extract.setInputCloud(cloud);
        extract.setIndices(inliers);
        extract.setNegative(false);
        extract.filter(*cloud_filtered);
        extract.setNegative(true);
        extract.filter(*cloud);
        cout << i << ":" << cloud->size() << endl;
        stringstream ss;
        ss << "desk-seg" << i << ".pcd";
        // 保存结果
        pcl::io::savePCDFileBinary(ss.str(), *cloud_filtered);
        i++;
    }
    pcl::io::savePCDFileBinary("desk-seg.pcd", *cloud);
    return 0;
}

seg0:
在这里插入图片描述
seg:
在这里插入图片描述

4.聚类分割

#include "pch.h"
using namespace std;
#include <pcl/point_types.h>          //PCL中所有点类型定义的头文件
#include <pcl/io/pcd_io.h>            //打开关闭pcd文件的类定义的头文件
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/kdtree/kdtree_flann.h>  //kd-tree搜索对象的类定义的头文件
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/visualization/cloud_viewer.h>

typedef pcl::PointXYZ PoinT;

int main(int argc, char** argv)
{
    pcl::PointCloud<PoinT>::Ptr cloud(new pcl::PointCloud<PoinT>());
    // 加载pcd文件
    pcl::io::loadPCDFile("desk-seg.pcd", *cloud);
    std::cout << "Cloud before filtering: " << std::endl;
    std::cout << *cloud << std::endl;
	
	// 聚类分割
    //为提取点云时使用的搜素对象利用输入点云cloud_filtered创建Kd树对象tree。
    pcl::search::KdTree<PoinT>::Ptr tree(new pcl::search::KdTree<PoinT>);
    tree->setInputCloud(cloud);//创建点云索引向量,用于存储实际的点云信息

    std::vector<pcl::PointIndices> cluster_indices;
    pcl::EuclideanClusterExtraction<PoinT> ece;
    ece.setClusterTolerance(0.02); //设置近邻搜索的搜索半径为2cm
    ece.setMinClusterSize(100);//设置一个聚类需要的最少点数目为100
    // ec.setMaxClusterSize(25000); //设置一个聚类需要的最大点数目为25000
    ece.setSearchMethod(tree);//设置点云的搜索机制
    ece.setInputCloud(cloud);
    ece.extract(cluster_indices);//从点云中提取聚类,并将点云索引保存在cluster_indices中

    //迭代访问点云索引cluster_indices,直到分割出所有聚类
    int i = 0;
    for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); it++)
    {
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZ>);
        cloud_cluster->width = cloud_cluster->points.size();
        cloud_cluster->height = 1;
        cloud_cluster->is_dense = true;
        //创建新的点云数据集cloud_cluster,将所有当前聚类写入到点云数据集中
        for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); pit++)
        {
            cloud_cluster->points.push_back(cloud->points[*pit]);
        }
        cout << i << ":" << cloud->size() << endl;
        // 保存结果
        stringstream ss;
        ss << "desk-ece" << i << ".pcd";
        pcl::io::savePCDFileBinary(ss.str(), *cloud_cluster);
        i++;
    }
}

ece0:
在这里插入图片描述
ece1:
在这里插入图片描述
ece2:
在这里插入图片描述
ece3:
在这里插入图片描述
ece4:
在这里插入图片描述
ece5:
在这里插入图片描述
ece6:
在这里插入图片描述

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值