当前位置:网站首页>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);
}
边栏推荐
- Hdu1234 door opener and door closer (water question)
- 【AppLinking实战案例】通过AppLinking分享应用内图片
- shell编程01_Shell基础
- LeetCode+ 76 - 80 暴搜专题
- 14.信号量的代码实现
- MySQL lethal serial question 3 -- are you familiar with MySQL locks?
- LabVIEW为什么浮点数会丢失精度
- AI技术产业热点分析
- js promise.all
- AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
猜你喜欢
随机推荐
13.信号量临界区保护
"Matching" is true love, a new attitude for young people to make friends
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
UVM - usage of common TLM port
01-spooldir
Easyexcel, a concise, fast and memory saving excel processing tool
flume 190 INSTALL
02-taildir source
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
Excuse me, is it cost-effective to insure love life patron saint 2.0 increased lifelong life insurance? What are the advantages of this product?
Rapid prototyping
MySQL数据库远程访问权限设置
13. Semaphore critical zone protection
【AGC】构建服务3-认证服务示例
【ARK UI】HarmonyOS ETS的启动页的实现
HDU1236 排名(结构体排序)
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
Windows环境MySQL8忘记密码文件解决方案
MongoDB-快速上手MongoDB命令行的一些简单操作
SPSS做Shapiro-Wilk正态分析