当前位置:网站首页>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);
}
边栏推荐
- Start class, data analysis, high salary training plan, elite class
- Kustomize使用手册
- 01-spooldir
- Operator-1初识Operator
- 【TS】1368- 秒懂 TypeScript 泛型工具类型!
- 集成学习概览
- [visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
- 14.信号量的代码实现
- Oracle notes
- MySQL environment configuration
猜你喜欢

使用华为性能管理服务,按需配置采样率

Database dictionary Navicat automatic generation version

Easyexcel, a concise, fast and memory saving excel processing tool

Hdu1234 door opener and door closer (water question)

从.bag文件中读取并保存.jpg图片和.pcd点云

数据库字典Navicat自动生成版本

Shell programming 01_ Shell foundation

JSP webshell free -- the basis of JSP

Leetcode+ 76 - 80 storm search topic

Ks009 implement pet management system based on SSH
随机推荐
618再次霸榜的秘密何在?耐克最新财报给出答案
02-taildir source
[SUCTF2018]followme
使用Windbg静态分析dump文件(实战经验总结)
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
《MySQL 8 DBA基础教程》简介
C#中索引器
【AppLinking实战案例】通过AppLinking分享应用内图片
Is this code PHP MySQL redundant?
JSP webshell免杀——webshell免杀
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
JSP webshell free -- webshell free
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
2.hacking-lab脚本关[详细writeup]
【快应用】Win7系统使用华为IDE无法运行和调试项目
华为快应用中如何实现同时传递事件对象和自定义参数
nodejs+express+mysql简单博客搭建
快应用中实现自定义抽屉组件
对话吴纲:我为什么笃信“大国品牌”的崛起?