Point Cloud Library (PCL)  1.9.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  // Initialize 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
pcl::SAC_RANSAC
const static int SAC_RANSAC
Definition: method_types.h:46
pcl::EuclideanClusterExtraction::setMinClusterSize
void setMinClusterSize(int min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid.
Definition: extract_clusters.h:356
pcl::EuclideanClusterExtraction::setClusterTolerance
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition: extract_clusters.h:340
pcl::EuclideanClusterExtraction::extract
void extract(std::vector< PointIndices > &clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
Definition: extract_clusters.hpp:210
pcl::EuclideanClusterExtraction::setMaxClusterSize
void setMaxClusterSize(int max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid.
Definition: extract_clusters.h:372
pcl::ExtractIndices
ExtractIndices extracts a set of indices from a point cloud.
Definition: extract_indices.h:70
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:53
pcl::PCLBase::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:66
pcl::ModelCoefficients::Ptr
boost::shared_ptr< ::pcl::ModelCoefficients > Ptr
Definition: ModelCoefficients.h:23
pcl::FilterIndices::setNegative
void setNegative(bool negative)
Set whether the regular conditions for points filtering should apply, or the inverted conditions.
Definition: filter_indices.h:127
pcl::PointIndices::Ptr
boost::shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:22
pcl::EuclideanClusterExtraction
EuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sen...
Definition: extract_clusters.h:295
pcl::ModelCoefficients
Definition: ModelCoefficients.h:12
pcl::SACSegmentation::setMethodType
void setMethodType(int method)
The type of sample consensus method to use (user given parameter).
Definition: sac_segmentation.h:129
pcl::PCLBase::setIndices
virtual void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
Definition: pcl_base.hpp:73
pcl::SACSegmentation::setMaxIterations
void setMaxIterations(int max_iterations)
Set the maximum number of iterations before giving up.
Definition: sac_segmentation.h:149
pcl::SACSegmentation::setModelType
void setModelType(int model)
The type of model to use (user given parameter).
Definition: sac_segmentation.h:111
pcl::PointIndices
Definition: PointIndices.h:12
pcl::SACSegmentation::segment
virtual void segment(PointIndices &inliers, ModelCoefficients &model_coefficients)
Base method for segmentation of a model in a PointCloud given by <setInputCloud (),...
Definition: sac_segmentation.hpp:75
pcl::SACSegmentation::setOptimizeCoefficients
void setOptimizeCoefficients(bool optimize)
Set to true if a coefficient refinement is required.
Definition: sac_segmentation.h:169
pcl::FilterIndices::filter
void filter(PointCloud &output)
Definition: filter_indices.h:103
pcl::SACSegmentation::setDistanceThreshold
void setDistanceThreshold(double threshold)
Distance to the model threshold (user given parameter).
Definition: sac_segmentation.h:139
pcl::SACMODEL_PLANE
@ SACMODEL_PLANE
Definition: model_types.h:48
pcl::SACSegmentation
SACSegmentation represents the Nodelet segmentation class for Sample Consensus methods and models,...
Definition: sac_segmentation.h:65