tutorial_fast_segmentation.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include <pcl/point_types.h>
00004 #include <pcl_ros/point_cloud.h>
00005 #include "cob_3d_mapping_common/point_types.h"
00006 #include "cob_3d_segmentation/impl/fast_segmentation.hpp"
00007 #include "cob_3d_features/organized_normal_estimation_omp.h"
00008 
00009 
00010 // specify point types (at least: XYZRGB, Normal, PointLabel)
00011 typedef cob_3d_segmentation::FastSegmentation<
00012   pcl::PointXYZRGB,
00013   pcl::Normal,
00014   PointLabel> Segmentation3d;
00015 typedef cob_3d_features::OrganizedNormalEstimationOMP<
00016   pcl::PointXYZRGB,
00017   pcl::Normal,
00018   PointLabel> NormalEstimation;
00019 
00020 void cloud_cb(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr& input)
00021 {
00022   /************ compute segments ***************/
00023   pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
00024   pcl::PointCloud<PointLabel>::Ptr labels(new pcl::PointCloud<PointLabel>);
00025 
00026   // Segmentation requires estimated normals for every 3d point
00027   NormalEstimation one;
00028   // labels are used to mark NaN and border points as a
00029   // preparation for the segmantation
00030   one.setOutputLabels(labels);
00031   // sets the pixelwindow size, radial step size and window step size
00032   one.setPixelSearchRadius(8,2,2);
00033   // sets the threshold for border point determination
00034   one.setSkipDistantPointThreshold(8);
00035   one.setInputCloud(input);
00036   one.compute(*normals);
00037 
00038   Segmentation3d seg;
00039   seg.setNormalCloudIn(normals);
00040   // labels are assigned according to cluster a point belongs to
00041   seg.setLabelCloudInOut(labels);
00042   // defines the method seed points are initialized (SEED_RANDOM | SEED_LINEAR)
00043   seg.setSeedMethod(cob_3d_segmentation::SEED_RANDOM);
00044   seg.setInputCloud(input);
00045   seg.compute();
00046 
00047 
00048   /************ access results ***************/
00049   // for visualization you can map the segments to a rgb point cloud
00050   pcl::PointCloud<pcl::PointXYZRGB>::Ptr points(
00051     new pcl::PointCloud<pcl::PointXYZRGB>);
00052   *points = *input; // deep copy input coordinates
00053   seg.mapSegmentColor(points);
00054 
00055   // to work with each cluster you can either use the labeled cloud:
00056   for(size_t i = 0; i<labels->size(); ++i)
00057   {
00058     int cluster_id = labels->points[i].label;
00059   }
00060 
00061   // or the internal cluster structure:
00062   // the cluster handler manages all segments and is being reset
00063   // before every call of seg.compute()
00064   Segmentation3d::ClusterHdlPtr cluster_handler = seg.clusters();
00065   // use .begin() and .end() to iterate over all computed clusters
00066   Segmentation3d::ClusterPtr c = cluster_handler->begin();
00067   for(; c != cluster_handler->end(); ++c)
00068   {
00069     // now you can access several properties of a cluster such as
00070     // the number of points
00071     int size = c->size();
00072     // the centroid
00073     Eigen::Vector3f mean = c->getCentroid();
00074     // the average normal:
00075     Eigen::Vector3f orientation = c->getOrientation();
00076     // the average color value:
00077     Eigen::Vector3i mean_color = c->getMeanColorVector();
00078     // the dominant color value (based on HSV histogram):
00079     Eigen::Vector3i dom_color = c->computeDominantColorVector();
00080     // the border points:
00081     std::vector<cob_3d_segmentation::PolygonPoint> border = c->border_points;
00082     // or the principal components (v1 > v2 > v3):
00083     cluster_handler->computeClusterComponents(c);
00084     Eigen::Vector3f v1 = c->pca_point_comp1;
00085     Eigen::Vector3f v2 = c->pca_point_comp2;
00086     Eigen::Vector3f v3 = c->pca_point_comp3;
00087     Eigen::Vector3f values = c->pca_point_values;
00088     // and you can iterate over the original point indices:
00089     std::vector<int>::iterator it = c->begin();
00090     for (; it != c->end(); ++it)
00091     {
00092       // access point of point cloud:
00093       pcl::PointXYZRGB p = input->points[*it];
00094     }
00095   }
00096   // and of course you can also access each cluster by its id:
00097   int cluster_id = 20;
00098   c = cluster_handler->getCluster(cluster_id);
00099 }
00100 
00101 
00102 int main(int argc, char** argv)
00103 {
00104   // Initialize ROS
00105   ros::init(argc,argv,"fast_segmentation_tutorial");
00106   ros::NodeHandle nh;
00107 
00108   // Create a ROS subscriber for the input point cloud
00109   ros::Subscriber sub = nh.subscribe ("input", 1, cloud_cb);
00110 
00111   // Spin
00112   ros::spin ();
00113 }


cob_3d_segmentation
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:03