当前位置:网站首页>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);
}
边栏推荐
- The URL in the RTSP setup header of the axis device cannot take a parameter
- Hdu1234 door opener and door closer (water question)
- AppGallery Connect场景化开发实战—图片存储分享
- HDU1228 A + B(map映射)
- JSP webshell免殺——JSP的基礎
- 13. Semaphore critical zone protection
- UVM learning - build a simple UVM verification platform
- Leetcode+ 76 - 80 storm search topic
- Kustomize使用手册
- Pywin32 opens the specified window
猜你喜欢
随机推荐
JSP webshell free -- webshell free
PCL 从一个点云中提取一个子集
Solutions to a series of problems in sqoop job creation
《MySQL 8 DBA基础教程》简介
(5) Gear control setting of APA scene construction
Shutter - canvas custom graph
【TS】1368- 秒懂 TypeScript 泛型工具类型!
Shapiro Wilk normal analysis by SPSS
全网显示 IP 归属地,是怎么实现的?
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
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?
STM32 and motor development (upper system)
2.hacking-lab脚本关[详细writeup]
Operator-1初识Operator
UVM factory mechanism
面对不确定性,供应链的作用
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
02-taildir source
Set the playback speed during the playback of UOB equipment
shell编程01_Shell基础
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)



![2.hacking-lab脚本关[详细writeup]](/img/f3/29745761cd5ad4df84c78ac904ea51.png)




