Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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/features/normal_3d.h>
- #include <pcl/visualization/cloud_viewer.h>
- #include <pcl/io/io.h>
- #include <pcl/features/integral_image_normal.h>
- int main (int argc, char** argv)
- {
- 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>);
- // Load the cloud data
- pcl::PCDReader reader;
- reader.read ("table_scene_lms400.pcd", *cloud_blob);
- std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;
- // Create the filtering object: downsample the dataset using a leaf size of 1cm
- pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
- sor.setInputCloud (cloud_blob);
- sor.setLeafSize (0.01f, 0.01f, 0.01f);
- sor.filter (*cloud_filtered_blob);
- // Convert to the templated PointCloud
- pcl::fromPCLPointCloud2 (*cloud_filtered_blob, *cloud_filtered);
- std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;
- pcl::PCDWriter writer;
- // Write the downsampled version to disk
- //writer.write<pcl::PointXYZ> ("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;
- // Optional
- seg.setOptimizeCoefficients (true);
- // Mandatory
- seg.setModelType (pcl::SACMODEL_PLANE);
- seg.setMethodType (pcl::SAC_RANSAC);
- seg.setMaxIterations (1000);
- seg.setDistanceThreshold (0.05);
- // Create the filtering object
- pcl::ExtractIndices<pcl::PointXYZ> extract;
- int i = 0, nr_points = (int) cloud_filtered->points.size ();
- // While 30% of the original cloud is still there
- while (cloud_filtered->points.size () > 0.1 * nr_points)
- {
- // Segment the largest planar component 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 << "plane_" << i << ".pcd";
- writer.write<pcl::PointXYZ> (ss.str (), *cloud_p, false);
- // Create the normal estimation class, and pass the input dataset to it
- pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
- ne.setInputCloud (cloud_p);
- // Create an empty kdtree representation, and pass it to the normal estimation object.
- // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
- pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());
- ne.setSearchMethod (tree);
- // Output datasets
- pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
- // Use all neighbors in a sphere of radius 3cm
- ne.setRadiusSearch (0.03);
- // Compute the features
- ne.compute (*cloud_normals);
- // std::cout << "Normals: " << cloud_normals->points[0] << std::endl;
- // visualize normals
- pcl::visualization::PCLVisualizer viewer("PCL Viewer");
- viewer.setBackgroundColor (0.0, 0.0, 0.5);
- viewer.addPointCloudNormals<pcl::PointXYZ,pcl::Normal>(cloud_p, cloud_normals);
- while (!viewer.wasStopped ())
- {
- viewer.spinOnce ();
- }
- // Create the filtering object
- extract.setNegative (true);
- extract.filter (*cloud_f);
- cloud_filtered.swap (cloud_f);
- i++;
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement