Point Cloud Library (PCL)  1.7.1
segmentation.h
1 #ifndef SEGMENTATION_H
2 #define SEGMENTATION_H
3 
4 #include "typedefs.h"
5 
6 #include <pcl/ModelCoefficients.h>
7 #include <pcl/sample_consensus/method_types.h>
8 #include <pcl/sample_consensus/model_types.h>
9 #include <pcl/segmentation/sac_segmentation.h>
10 #include <pcl/filters/extract_indices.h>
11 #include <pcl/segmentation/extract_clusters.h>
12 
13 
14 /* Use SACSegmentation to find the dominant plane in the scene
15  * Inputs:
16  * input
17  * The input point cloud
18  * max_iterations
19  * The maximum number of RANSAC iterations to run
20  * distance_threshold
21  * The inlier/outlier threshold. Points within this distance
22  * from the hypothesized plane are scored as inliers.
23  * Return: A pointer to the ModelCoefficients (i.e., the 4 coefficients of the plane,
24  * represented in c0*x + c1*y + c2*z + c3 = 0 form)
25  */
27 fitPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
28 {
29  // Intialize the SACSegmentation object
31  seg.setOptimizeCoefficients (true);
34  seg.setDistanceThreshold (distance_threshold);
35  seg.setMaxIterations (max_iterations);
36 
37  seg.setInputCloud (input);
40  seg.segment (*inliers, *coefficients);
41 
42  return (coefficients);
43 }
44 
45 /* Use SACSegmentation and an ExtractIndices filter to find the dominant plane and subtract it
46  * Inputs:
47  * input
48  * The input point cloud
49  * max_iterations
50  * The maximum number of RANSAC iterations to run
51  * distance_threshold
52  * The inlier/outlier threshold. Points within this distance
53  * from the hypothesized plane are scored as inliers.
54  * Return: A pointer to a new point cloud which contains only the non-plane points
55  */
56 PointCloudPtr
57 findAndSubtractPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
58 {
59  // Find the dominant plane
61  seg.setOptimizeCoefficients (false);
64  seg.setDistanceThreshold (distance_threshold);
65  seg.setMaxIterations (max_iterations);
66  seg.setInputCloud (input);
69  seg.segment (*inliers, *coefficients);
70 
71  // Extract the inliers
73  extract.setInputCloud (input);
74  extract.setIndices (inliers);
75  extract.setNegative (true);
76  PointCloudPtr output (new PointCloud);
77  extract.filter (*output);
78 
79  return (output);
80 }
81 
82 /* Use EuclidieanClusterExtraction to group a cloud into contiguous clusters
83  * Inputs:
84  * input
85  * The input point cloud
86  * cluster_tolerance
87  * The maximum distance between neighboring points in a cluster
88  * min/max_cluster_size
89  * The minimum and maximum allowable cluster sizes
90  * Return (by reference): a vector of PointIndices containing the points indices in each cluster
91  */
92 void
93 clusterObjects (const PointCloudPtr & input,
94  float cluster_tolerance, int min_cluster_size, int max_cluster_size,
95  std::vector<pcl::PointIndices> & cluster_indices_out)
96 {
98  ec.setClusterTolerance (cluster_tolerance);
99  ec.setMinClusterSize (min_cluster_size);
100  ec.setMaxClusterSize (max_cluster_size);
101 
102  ec.setInputCloud (input);
103  ec.extract (cluster_indices_out);
104 }
105 
106 #endif
void setMaxIterations(int max_iterations)
Set the maximum number of iterations before giving up.
void setMethodType(int method)
The type of sample consensus method to use (user given parameter).
SACSegmentation represents the Nodelet segmentation class for Sample Consensus methods and models...
void setMaxClusterSize(int max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid...
void setMinClusterSize(int min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid...
EuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sen...
void filter(PointCloud &output)
boost::shared_ptr< ::pcl::ModelCoefficients > Ptr
void extract(std::vector< PointIndices > &clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
ExtractIndices extracts a set of indices from a point cloud.
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
static const int SAC_RANSAC
Definition: method_types.h:46
boost::shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:22
void setOptimizeCoefficients(bool optimize)
Set to true if a coefficient refinement is required.
void setModelType(int model)
The type of model to use (user given parameter).
void setDistanceThreshold(double threshold)
Distance to the model threshold (user given parameter).
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:66
void setNegative(bool negative)
Set whether the regular conditions for points filtering should apply, or the inverted conditions...
virtual void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
Definition: pcl_base.hpp:73
virtual void segment(PointIndices &inliers, ModelCoefficients &model_coefficients)
Base method for segmentation of a model in a PointCloud given by <setInputCloud (), setIndices ()>