00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00052 #include <pcl/ModelCoefficients.h>
00053
00054 #include <pcl/io/pcd_io.h>
00055 #include <pcl/point_types.h>
00056
00057 #include <pcl/features/normal_3d.h>
00058
00059 #include <pcl/filters/extract_indices.h>
00060 #include <pcl/filters/passthrough.h>
00061
00062 #include <pcl/sample_consensus/method_types.h>
00063 #include <pcl/sample_consensus/model_types.h>
00064 #include <pcl/segmentation/sac_segmentation.h>
00065
00066 typedef pcl::PointXYZ PointT;
00067
00068
00069 int
00070 main (int argc, char** argv)
00071 {
00072
00073 pcl::PCDReader reader;
00074 pcl::PassThrough<PointT> pass;
00075 pcl::NormalEstimation<PointT, pcl::Normal> ne;
00076 pcl::SACSegmentationFromNormals<PointT, pcl::Normal> seg;
00077 pcl::PCDWriter writer;
00078 pcl::ExtractIndices<PointT> extract;
00079 pcl::ExtractIndices<pcl::Normal> extract_normals;
00080 pcl::KdTreeFLANN<PointT>::Ptr tree (new pcl::KdTreeFLANN<PointT> ());
00081
00082
00083 pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PointT> ());
00084 pcl::PointCloud<PointT>::Ptr cloud_filtered (new pcl::PointCloud<PointT> ());
00085 pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal> ());
00086 pcl::ModelCoefficients::Ptr coefficients_plane (new pcl::ModelCoefficients ()), coefficients_cylinder (new pcl::ModelCoefficients ());
00087 pcl::PointIndices::Ptr inliers_plane (new pcl::PointIndices ()), inliers_cylinder (new pcl::PointIndices ());
00088
00089
00090 reader.read ("data/table_scene_mug_stereo_textured.pcd", *cloud);
00091 ROS_INFO ("PointCloud has: %zu data points.", cloud->points.size ());
00092
00093
00094 pass.setInputCloud (cloud);
00095 pass.setFilterFieldName ("z");
00096 pass.setFilterLimits (0, 1.5);
00097 pass.filter (*cloud_filtered);
00098 ROS_INFO ("PointCloud after filtering has: %zu data points.", cloud_filtered->points.size ());
00099
00100
00101 ne.setSearchMethod (tree);
00102 ne.setInputCloud (cloud_filtered);
00103 ne.setKSearch (50);
00104 ne.compute (*cloud_normals);
00105
00106
00107 seg.setOptimizeCoefficients (true);
00108 seg.setModelType (pcl::SACMODEL_NORMAL_PLANE);
00109 seg.setNormalDistanceWeight (0.1);
00110 seg.setMethodType (pcl::SAC_RANSAC);
00111 seg.setMaxIterations (100);
00112 seg.setDistanceThreshold (0.03);
00113 seg.setInputCloud (cloud_filtered);
00114 seg.setInputNormals (cloud_normals);
00115
00116 seg.segment (*inliers_plane, *coefficients_plane);
00117 std::cerr << "Plane coefficients: " << *coefficients_plane << std::endl;
00118
00119
00120 extract.setInputCloud (cloud_filtered);
00121 extract.setIndices (inliers_plane);
00122 extract.setNegative (false);
00123
00124
00125 pcl::PointCloud<PointT>::Ptr cloud_plane (new pcl::PointCloud<PointT> ());
00126 extract.filter (*cloud_plane);
00127 ROS_INFO ("PointCloud representing the planar component: %zu data points.", cloud_plane->points.size ());
00128 writer.write ("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);
00129
00130
00131 extract.setNegative (true);
00132 extract.filter (*cloud_filtered);
00133 extract_normals.setNegative (true);
00134 extract_normals.setInputCloud (cloud_normals);
00135 extract_normals.setIndices (inliers_plane);
00136 extract_normals.filter (*cloud_normals);
00137
00138
00139 seg.setOptimizeCoefficients (true);
00140 seg.setModelType (pcl::SACMODEL_CYLINDER);
00141 seg.setMethodType (pcl::SAC_RANSAC);
00142 seg.setNormalDistanceWeight (0.1);
00143 seg.setMaxIterations (10000);
00144 seg.setDistanceThreshold (0.05);
00145 seg.setRadiusLimits (0, 0.1);
00146 seg.setInputCloud (cloud_filtered);
00147 seg.setInputNormals (cloud_normals);
00148
00149
00150 seg.segment (*inliers_cylinder, *coefficients_cylinder);
00151 std::cerr << "Cylinder coefficients: " << *coefficients_cylinder << std::endl;
00152
00153
00154 extract.setInputCloud (cloud_filtered);
00155 extract.setIndices (inliers_cylinder);
00156 extract.setNegative (false);
00157 pcl::PointCloud<PointT>::Ptr cloud_cylinder (new pcl::PointCloud<PointT> ());
00158 extract.filter (*cloud_cylinder);
00159 ROS_INFO ("PointCloud representing the cylindrical component: %zu data points.", cloud_cylinder->points.size ());
00160 writer.write ("table_scene_mug_stereo_textured_cylinder.pcd", *cloud_cylinder, false);
00161
00162 return (0);
00163 }
00164