当前位置:网站首页>PCL 从一个点云中提取一个子集
PCL 从一个点云中提取一个子集
2022-07-02 07:00:00 【AICVer】
参数化分割平面
#include <iostream>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/visualization/pcl_visualizer.h>
void visualize (pcl::PointCloud<pcl::PointXYZ>::Ptr source, pcl::PointCloud<pcl::PointXYZ>::Ptr target)
{
pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer");
// 创建两个显示窗口
int v1, v2;
viewer.createViewPort(0, 0.0, 0.5, 1.0, v1);
viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
// 设置背景颜色
viewer.setBackgroundColor(255, 255, 255, v1);
viewer.setBackgroundColor(255, 255, 255, v2);
// 给点云添加颜色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_color(source, 0, 0, 255); // blue
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> target_color(target, 255, 0, 0); // red
// 添加点云到显示窗口
viewer.addPointCloud(source, source_color, "source cloud", v1);
viewer.addPointCloud(target, target_color, "target cloud", v2);
while (!viewer.wasStopped())
{
viewer.spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
int main(int argc, char** argv)
{
//定义并实例化一个PointCloud指针对象,申明滤波前后的点云
pcl::PCLPointCloud2::Ptr cloud_blob(new pcl::PCLPointCloud2), cloud_filtered_blob(new pcl::PCLPointCloud2);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>), cloud_p(new pcl::PointCloud<pcl::PointXYZ>), cloud_f(new pcl::PointCloud<pcl::PointXYZ>);
//读取PCD文件
pcl::PCDReader reader;
reader.read("D:\\Data\\rabbit.pcd", *cloud_blob); //读取点云文件中的数据到 cloud 对象,table_scene_lms400.pcd 文件与该cpp文件在同一级目录下
std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;
//创建体素栅格下采样: 下采样的大小为1cm
pcl::VoxelGrid<pcl::PCLPointCloud2> sor; //创建 VoxelGrid 体素栅格滤波对象
sor.setInputCloud(cloud_blob); //设置输入的原始采样点云数据
sor.setLeafSize(0.01f, 0.01f, 0.01f); //设置采样的体素大小为 1cm 立方体
sor.filter(*cloud_filtered_blob); //执行滤波处理,存储滤波后的输出点云 cloud_filtered_blob
//转换为模板点云
pcl::fromPCLPointCloud2(*cloud_filtered_blob, *cloud_filtered);
std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;
//保存采样后的点云
pcl::PCDWriter writer;
//writer.write<pcl::PointXYZ>("table_scene_lms400_downsampled.pcd", *cloud_filtered, false);
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
// Create the segmentation object
pcl::SACSegmentation<pcl::PointXYZ> seg; //创建分割对象
// Optional
seg.setOptimizeCoefficients(true); //设置对估计的模型参数进行优化处理
// Mandatory
seg.setModelType(pcl::SACMODEL_PLANE); //设置分割模型类别
seg.setMethodType(pcl::SAC_RANSAC); //设置用哪个随机参数估计方法
seg.setMaxIterations(1000); //设置最大迭代次数
seg.setDistanceThreshold(0.01); //设置判断是否为模型内点的距离阈值
//设置ExtractIndices的实际参数
pcl::ExtractIndices<pcl::PointXYZ> extract;
int i = 0, nr_points = (int)cloud_filtered->size(); //点云总数
// While 30% of the original cloud is still there
while (cloud_filtered->size() > 0.3 * nr_points)
{
//为了处理点云包含的多个模型,在一个循环中执行该过程并在每次模型被提取后,保存剩余的点进行迭代
//将最大的平面部件从剩余的云中分割出来
seg.setInputCloud(cloud_filtered);
seg.segment(*inliers, *coefficients);
if (inliers->indices.size() == 0)
{
std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
break;
}
// Extract the inliers
extract.setInputCloud(cloud_filtered);
extract.setIndices(inliers);
extract.setNegative(false);
extract.filter(*cloud_p);
std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl;
std::stringstream ss;
ss << "table_scene_lms400_plane_" << i << ".pcd";
//writer.write<pcl::PointXYZ>(ss.str(), *cloud_p, false);
visualize(cloud_filtered, cloud_p);
// Create the filtering object
extract.setNegative(true);
extract.filter(*cloud_f);
cloud_filtered.swap(cloud_f);
i++;
}
return (0);
}
边栏推荐
- Pywin32 opens the specified window
- 2021-10-04
- [jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found
- [MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
- 互联网快讯:腾讯会议应用市场正式上线;Soul赴港递交上市申请书
- 01-spooldir
- 13.信号量临界区保护
- 【Lua】常见知识点汇总(包含常见面试考点)
- Pytest learning --base
- This article takes you to learn in detail what is fiber to home FTTH
猜你喜欢

Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud

618再次霸榜的秘密何在?耐克最新财报给出答案

使用sqlcipher打开加密的sqlite方法
![[Fantasy 4] the transformation from U3D to UE4](/img/bb/665eba3c8cd774c94fe14f169121da.png)
[Fantasy 4] the transformation from U3D to UE4
![[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened](/img/c3/aec0a9fb79cf94b3575d95530aa8e8.png)
[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened

13. Semaphore critical zone protection

Basic usage of mock server

Is this code PHP MySQL redundant?

Delivery mode design of Spartacus UI of SAP e-commerce cloud

测试--面试题总结
随机推荐
Mock Server基本使用方法
pytest学习--base
MySQL -- time zone / connector / driver type
session-cookie与token
转换YV12到RGB565图像转换,附YUV转RGB测试
Shapiro Wilk normal analysis by SPSS
Flink submitter
"Matching" is true love, a new attitude for young people to make friends
测试--面试题总结
13. Semaphore critical zone protection
使用Windbg静态分析dump文件(实战经验总结)
Operator-1 first acquaintance with operator
SPSS做Shapiro-Wilk正态分析
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
SUS系统可用性量表
Database dictionary Navicat automatic generation version
[Fantasy 4] the transformation from U3D to UE4
Determine whether there are duplicate elements in the array
Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud
【Unity3D】制作进度条——让Image同时具有Filled和Sliced的功能