Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace cm;
- using namespace cv;
- using namespace std;
- // All the objects needed
- pcl::PCDReader reader;
- pcl::PassThrough<PointT> pass;
- pcl::NormalEstimation<PointT, pcl::Normal> ne;
- pcl::SACSegmentationFromNormals<PointT, pcl::Normal> seg;
- pcl::PCDWriter writer;
- pcl::ExtractIndices<PointT> extract;
- pcl::ExtractIndices<pcl::Normal> extract_normals;
- pcl::search::KdTree<PointT>::Ptr tree (new pcl::search::KdTree<PointT> ());
- // Datasets
- pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PointT>);
- pcl::PointCloud<PointT>::Ptr cloud_filtered (new pcl::PointCloud<PointT>),
- cloud_projected(new pcl::PointCloud<PointT>);
- pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
- pcl::PointCloud<PointT>::Ptr cloud_filtered2 (new pcl::PointCloud<PointT>);
- pcl::PointCloud<pcl::Normal>::Ptr cloud_normals2 (new pcl::PointCloud<pcl::Normal>);
- pcl::ModelCoefficients::Ptr coefficients_plane (new pcl::ModelCoefficients), coefficients_cylinder (new pcl::ModelCoefficients);
- pcl::PointIndices::Ptr inliers_plane (new pcl::PointIndices), inliers_cylinder (new pcl::PointIndices);
- // Read in the cloud data
- reader.read ("dm2hm_right32.pcd", *cloud);
- std::cerr << "PointCloud has: " << cloud->points.size () << " data points." << std::endl;
- // Build a passthrough filter to remove spurious NaNs
- pass.setInputCloud (cloud);
- pass.setFilterFieldName ("z");
- pass.setFilterLimits (-600 , 600);
- pass.filter (*cloud_filtered);
- std::cerr << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl;
- // Estimate point normals
- ne.setSearchMethod (tree);
- ne.setInputCloud (cloud_filtered);
- ne.setKSearch (50);
- ne.compute (*cloud_normals);
- // Create the segmentation object for the planar model and set all the parameters
- seg.setOptimizeCoefficients (true);
- seg.setModelType (pcl::SACMODEL_NORMAL_PLANE);
- seg.setNormalDistanceWeight (0.1);
- seg.setMethodType (pcl::SAC_RANSAC);
- seg.setMaxIterations (100);
- seg.setDistanceThreshold (20);
- seg.setEpsAngle(0.05320);
- seg.setInputCloud (cloud_filtered);
- seg.setInputNormals (cloud_normals);
- Eigen::Vector3f YAxis(0,1,0);
- seg.setAxis(YAxis);
- // Obtain the plane inliers and coefficients
- seg.segment (*inliers_plane, *coefficients_plane);
- std::cerr << "Plane coefficients: " << *coefficients_plane << std::endl;
- // Extract the planar inliers from the input cloud
- extract.setInputCloud (cloud_filtered);
- extract.setIndices (inliers_plane);
- extract.setNegative (false);
- // Write the planar inliers to disk
- pcl::PointCloud<PointT>::Ptr cloud_plane (new pcl::PointCloud<PointT> ());
- extract.filter (*cloud_plane);
- std::cerr << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;
- pcl::ProjectInliers<pcl::PointXYZ> proj;
- proj.setModelType (pcl::SACMODEL_PLANE);
- proj.setIndices (inliers_plane);
- proj.setInputCloud (cloud_filtered);
- proj.setModelCoefficients (coefficients_plane);
- proj.filter (*cloud_projected);
- std::cerr << "PointCloud after projection has: "
- << cloud_projected->points.size () << " data points." << std::endl;
- // Create a Concave Hull representation of the projected inliers
- pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_hull (new pcl::PointCloud<pcl::PointXYZ>);
- pcl::ConcaveHull<pcl::PointXYZ> chull;
- chull.setInputCloud (cloud_projected);
- chull.setAlpha (50);
- chull.reconstruct (*cloud_hull);
- std::cerr << "Concave hull has: " << cloud_hull->points.size ()
- << " data points." << std::endl;
- writer.write ("table_scene_mug_stereo_textured_hull.pcd", *cloud_hull, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement