当前位置:网站首页>PCL extracts a subset from a point cloud
PCL extracts a subset from a point cloud
2022-07-02 10:57:00 【AICVer】
Parametric split plane
#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");
// Create two display windows
int v1, v2;
viewer.createViewPort(0, 0.0, 0.5, 1.0, v1);
viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
// Set the background color
viewer.setBackgroundColor(255, 255, 255, v1);
viewer.setBackgroundColor(255, 255, 255, v2);
// Add color to point clouds
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_color(source, 0, 0, 255); // blue
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> target_color(target, 255, 0, 0); // red
// Add a point cloud to the display window
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)
{
// Define and instantiate a PointCloud Pointer object , Declare the point cloud before and after filtering
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>);
// Read PCD file
pcl::PCDReader reader;
reader.read("D:\\Data\\rabbit.pcd", *cloud_blob); // Read the data in the point cloud file to cloud object ,table_scene_lms400.pcd Documents and the cpp The files are in the same directory
std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;
// Create voxel grid down sampling : The size of the down sample is 1cm
pcl::VoxelGrid<pcl::PCLPointCloud2> sor; // establish VoxelGrid Voxel grid filter object
sor.setInputCloud(cloud_blob); // Set the input original sampling point cloud data
sor.setLeafSize(0.01f, 0.01f, 0.01f); // Set the voxel size of sampling to 1cm Cube
sor.filter(*cloud_filtered_blob); // Perform filtering , Store the filtered output point cloud cloud_filtered_blob
// Convert to template point cloud
pcl::fromPCLPointCloud2(*cloud_filtered_blob, *cloud_filtered);
std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;
// Save the sampled point cloud
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; // Create split objects
// Optional
seg.setOptimizeCoefficients(true); // Set to optimize the estimated model parameters
// Mandatory
seg.setModelType(pcl::SACMODEL_PLANE); // Set the split model category
seg.setMethodType(pcl::SAC_RANSAC); // Set which random parameter estimation method to use
seg.setMaxIterations(1000); // Set the maximum number of iterations
seg.setDistanceThreshold(0.01); // Set the distance threshold to judge whether it is the inner point of the model
// Set up ExtractIndices Actual parameters of
pcl::ExtractIndices<pcl::PointXYZ> extract;
int i = 0, nr_points = (int)cloud_filtered->size(); // Total number of point clouds
// While 30% of the original cloud is still there
while (cloud_filtered->size() > 0.3 * nr_points)
{
// To handle multiple models contained in a point cloud , This process is performed in a loop and after each time the model is extracted , Save the remaining points for iteration
// Split the largest plane part from the remaining cloud
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);
}
边栏推荐
- SUS系统可用性量表
- UVM - usage of common TLM port
- Database dictionary Navicat automatic generation version
- HDU1236 排名(结构体排序)
- Set the playback speed during the playback of UOB equipment
- [visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
- 从MediaRecord录像中读取H264参数
- 华为应用市场应用统计数据问题大揭秘
- PCL 从一个点云中提取一个子集
- P1055 [NOIP2008 普及组] ISBN 号码
猜你喜欢
How to get the password of cpolar?
The URL in the RTSP setup header of the axis device cannot take a parameter
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
Read H264 parameters from mediarecord recording
session-cookie与token
4.随机变量
Database dictionary Navicat automatic generation version
nodejs+express+mysql简单博客搭建
STM32 and motor development (upper system)
Hdu1234 door opener and door closer (water question)
随机推荐
SQOOP 1.4.6 INSTALL
PCL 投影点云
Flink submitter
What is the significance of the college entrance examination
如何用list组件实现tabbar标题栏
Kustomize使用手册
Oracle notes
简洁、快速、节约内存的Excel处理工具EasyExcel
Use WinDbg to statically analyze dump files (summary of practical experience)
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
shell编程01_Shell基础
(五)APA场景搭建之挡位控制设置
The URL in the RTSP setup header of the axis device cannot take a parameter
stm32和電機開發(上比特系統)
JSP webshell free -- webshell free
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
使用Windbg静态分析dump文件(实战经验总结)
JSP webshell免杀——webshell免杀
12. Process synchronization and semaphore
The nanny level tutorial of flutter environment configuration makes the doctor green to the end