00001 #include <pcl/ModelCoefficients.h> 00002 #include <pcl/io/pcd_io.h> 00003 #include <pcl/point_types.h> 00004 #include <pcl/sample_consensus/method_types.h> 00005 #include <pcl/sample_consensus/model_types.h> 00006 #include <pcl/filters/passthrough.h> 00007 #include <pcl/filters/project_inliers.h> 00008 #include <pcl/segmentation/sac_segmentation.h> 00009 #include <pcl/surface/concave_hull.h> 00010 00011 int 00012 main (int argc, char** argv) 00013 { 00014 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>), 00015 cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>), 00016 cloud_projected (new pcl::PointCloud<pcl::PointXYZ>); 00017 pcl::PCDReader reader; 00018 00019 reader.read ("table_scene_mug_stereo_textured.pcd", *cloud); 00020 // Build a filter to remove spurious NaNs 00021 pcl::PassThrough<pcl::PointXYZ> pass; 00022 pass.setInputCloud (cloud); 00023 pass.setFilterFieldName ("z"); 00024 pass.setFilterLimits (0, 1.1); 00025 pass.filter (*cloud_filtered); 00026 std::cerr << "PointCloud after filtering has: " 00027 << cloud_filtered->points.size () << " data points." << std::endl; 00028 00029 pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients); 00030 pcl::PointIndices::Ptr inliers (new pcl::PointIndices); 00031 // Create the segmentation object 00032 pcl::SACSegmentation<pcl::PointXYZ> seg; 00033 // Optional 00034 seg.setOptimizeCoefficients (true); 00035 // Mandatory 00036 seg.setModelType (pcl::SACMODEL_PLANE); 00037 seg.setMethodType (pcl::SAC_RANSAC); 00038 seg.setDistanceThreshold (0.01); 00039 00040 seg.setInputCloud (cloud_filtered); 00041 seg.segment (*inliers, *coefficients); 00042 std::cerr << "PointCloud after segmentation has: " 00043 << inliers->indices.size () << " inliers." << std::endl; 00044 00045 // Project the model inliers 00046 pcl::ProjectInliers<pcl::PointXYZ> proj; 00047 proj.setModelType (pcl::SACMODEL_PLANE); 00048 proj.setIndices (inliers); 00049 proj.setInputCloud (cloud_filtered); 00050 proj.setModelCoefficients (coefficients); 00051 proj.filter (*cloud_projected); 00052 std::cerr << "PointCloud after projection has: " 00053 << cloud_projected->points.size () << " data points." << std::endl; 00054 00055 // Create a Concave Hull representation of the projected inliers 00056 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_hull (new pcl::PointCloud<pcl::PointXYZ>); 00057 pcl::ConcaveHull<pcl::PointXYZ> chull; 00058 chull.setInputCloud (cloud_projected); 00059 chull.setAlpha (0.1); 00060 chull.reconstruct (*cloud_hull); 00061 00062 std::cerr << "Concave hull has: " << cloud_hull->points.size () 00063 << " data points." << std::endl; 00064 00065 pcl::PCDWriter writer; 00066 writer.write ("table_scene_mug_stereo_textured_hull.pcd", *cloud_hull, false); 00067 00068 return (0); 00069 }