PCL --- downsample

记录一下

使用VoxelGrid的filter进行下采样

#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>

#include <pcl/filters/voxel_grid.h>



int main()
{
   
  //use PCDReader load PCD file
  pcl::PCDReader reader;
  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用PCL C++实现的代码,用于检测点云中的所有直线: ```c++ #include <iostream> #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include <pcl/filters/voxel_grid.h> #include <pcl/features/normal_3d.h> #include <pcl/segmentation/sac_segmentation.h> #include <pcl/visualization/pcl_visualizer.h> int main (int argc, char** argv) { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>); pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>); pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients); pcl::PointIndices::Ptr inliers (new pcl::PointIndices); pcl::SACSegmentationFromNormals<pcl::PointXYZ, pcl::Normal> seg; pcl::ExtractIndices<pcl::PointXYZ> extract; pcl::ExtractIndices<pcl::Normal> extract_normals; pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>); pcl::visualization::PCLVisualizer viewer("Detected Lines"); // Read in the cloud data pcl::PCDReader reader; reader.read<pcl::PointXYZ> ("cloud.pcd", *cloud); // Downsample the cloud using a Voxel Grid filter pcl::VoxelGrid<pcl::PointXYZ> vg; vg.setInputCloud (cloud); vg.setLeafSize (0.01f, 0.01f, 0.01f); vg.filter (*cloud_filtered); // Estimate point normals pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; ne.setInputCloud (cloud_filtered); ne.setSearchMethod (tree); ne.setKSearch (50); ne.compute (*cloud_normals); // Create the segmentation object for the planar model and set all the parameters seg.setOptimizeCoefficients (true); seg.setModelType (pcl::SACMODEL_NORMAL_PLANE); seg.setNormalDistanceWeight (0.1); seg.setMethodType (pcl::SAC_RANSAC); seg.setMaxIterations (100); seg.setDistanceThreshold (0.03); seg.setInputCloud (cloud_filtered); seg.setInputNormals (cloud_normals); // Obtain the plane inliers and coefficients seg.segment (*inliers, *coefficients); // Extract the planar inliers from the input cloud extract.setInputCloud (cloud_filtered); extract.setIndices (inliers); extract.setNegative (false); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZ> ()); extract.filter (*cloud_plane); // Remove the planar inliers, extract the rest extract.setNegative (true); extract.filter (*cloud_filtered); extract_normals.setInputCloud (cloud_normals); extract_normals.setIndices (inliers); extract_normals.setNegative (false); pcl::PointCloud<pcl::Normal>::Ptr cloud_normals_plane (new pcl::PointCloud<pcl::Normal> ()); extract_normals.filter (*cloud_normals_plane); // Create the segmentation object for cylinder segmentation and set all the parameters seg.setOptimizeCoefficients (true); seg.setModelType (pcl::SACMODEL_CYLINDER); seg.setMethodType (pcl::SAC_RANSAC); seg.setNormalDistanceWeight (0.1); seg.setMaxIterations (10000); seg.setDistanceThreshold (0.05); seg.setRadiusLimits (0.01, 0.2); seg.setInputCloud (cloud_filtered); seg.setInputNormals (cloud_normals_plane); // Obtain the cylinder inliers and coefficients std::vector<pcl::ModelCoefficients> cylinder_coefficients; std::vector<pcl::PointIndices> cylinder_inliers; seg.segment (cylinder_inliers, cylinder_coefficients); // Draw the cylinder inliers in red for (size_t i = 0; i < cylinder_inliers.size (); i++) { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cylinder (new pcl::PointCloud<pcl::PointXYZ> ()); for (size_t j = 0; j < cylinder_inliers[i].indices.size (); j++) cloud_cylinder->push_back ((*cloud_filtered)[cylinder_inliers[i].indices[j]]); pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> red_color(cloud_cylinder, 255, 0, 0); viewer.addPointCloud<pcl::PointXYZ> (cloud_cylinder, red_color, "cylinder" + std::to_string(i)); } // Spin until the user closes the viewer viewer.spin(); return 0; } ``` 这个代码首先将点云数据读入内存,然后使用体素网格滤波器进行下采样。接着估计点的法线,并使用法线平面模型进行分割。然后使用法线面模型分割后的剩余点云数据,使用圆柱模型进行分割。最后将所有检测到的圆柱绘制在可视化窗口中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值