当前位置:网站首页>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);
}
边栏推荐
- Windows环境MySQL8忘记密码文件解决方案
- 使用sqlcipher打开加密的sqlite方法
- Stm32 et développement de moteurs (système supérieur)
- JSP webshell免殺——JSP的基礎
- PCL 投影点云
- Open the encrypted SQLite method with sqlcipher
- MySQL environment configuration
- Start class, data analysis, high salary training plan, elite class
- 2022爱分析· 国央企数字化厂商全景报告
- SUS系统可用性量表
猜你喜欢
![2. Hacking lab script off [detailed writeup]](/img/f3/29745761cd5ad4df84c78ac904ea51.png)
2. Hacking lab script off [detailed writeup]

Jsp webshell Free from killing - The Foundation of JSP

2022-06-17

Win11 arm系统配置.net core环境变量

【TS】1368- 秒懂 TypeScript 泛型工具类型!

Use WinDbg to statically analyze dump files (summary of practical experience)

LeetCode+ 76 - 80 暴搜专题

Analysis of hot spots in AI technology industry

Open the encrypted SQLite method with sqlcipher

The nanny level tutorial of flutter environment configuration makes the doctor green to the end
随机推荐
12. Process synchronization and semaphore
4. Random variables
正则及常用公式
Database dictionary Navicat automatic generation version
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
Oracle 笔记
《实习报告》Skywalking分布式链路追踪?
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
【TS】1368- 秒懂 TypeScript 泛型工具类型!
js promise. all
PCL 投影点云
【AppLinking实战案例】通过AppLinking分享应用内图片
In the face of uncertainty, the role of supply chain
STM32 and motor development (upper system)
Ks009 implement pet management system based on SSH
AI技术产业热点分析
nodejs+express+mysql简单博客搭建
C#中索引器
点云投影图片
使用Windbg静态分析dump文件(实战经验总结)