Point Cloud Library (PCL)  1.10.1
segmentation.h
1 #pragma once
2 
3 #include "typedefs.h"
4 
5 #include <pcl/ModelCoefficients.h>
6 #include <pcl/sample_consensus/method_types.h>
7 #include <pcl/sample_consensus/model_types.h>
8 #include <pcl/segmentation/sac_segmentation.h>
9 #include <pcl/filters/extract_indices.h>
10 #include <pcl/segmentation/extract_clusters.h>
11 
12 
13 /* Use SACSegmentation to find the dominant plane in the scene
14  * Inputs:
15  * input
16  * The input point cloud
17  * max_iterations
18  * The maximum number of RANSAC iterations to run
19  * distance_threshold
20  * The inlier/outlier threshold. Points within this distance
21  * from the hypothesized plane are scored as inliers.
22  * Return: A pointer to the ModelCoefficients (i.e., the 4 coefficients of the plane,
23  * represented in c0*x + c1*y + c2*z + c3 = 0 form)
24  */
26 fitPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
27 {
28  // Initialize the SACSegmentation object
30  seg.setOptimizeCoefficients (true);
33  seg.setDistanceThreshold (distance_threshold);
34  seg.setMaxIterations (max_iterations);
35 
36  seg.setInputCloud (input);
39  seg.segment (*inliers, *coefficients);
40 
41  return (coefficients);
42 }
43 
44 /* Use SACSegmentation and an ExtractIndices filter to find the dominant plane and subtract it
45  * Inputs:
46  * input
47  * The input point cloud
48  * max_iterations
49  * The maximum number of RANSAC iterations to run
50  * distance_threshold
51  * The inlier/outlier threshold. Points within this distance
52  * from the hypothesized plane are scored as inliers.
53  * Return: A pointer to a new point cloud which contains only the non-plane points
54  */
55 PointCloudPtr
56 findAndSubtractPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
57 {
58  // Find the dominant plane
60  seg.setOptimizeCoefficients (false);
63  seg.setDistanceThreshold (distance_threshold);
64  seg.setMaxIterations (max_iterations);
65  seg.setInputCloud (input);
68  seg.segment (*inliers, *coefficients);
69 
70  // Extract the inliers
72  extract.setInputCloud (input);
73  extract.setIndices (inliers);
74  extract.setNegative (true);
75  PointCloudPtr output (new PointCloud);
76  extract.filter (*output);
77 
78  return (output);
79 }
80 
81 /* Use EuclidieanClusterExtraction to group a cloud into contiguous clusters
82  * Inputs:
83  * input
84  * The input point cloud
85  * cluster_tolerance
86  * The maximum distance between neighboring points in a cluster
87  * min/max_cluster_size
88  * The minimum and maximum allowable cluster sizes
89  * Return (by reference): a vector of PointIndices containing the points indices in each cluster
90  */
91 void
92 clusterObjects (const PointCloudPtr & input,
93  float cluster_tolerance, int min_cluster_size, int max_cluster_size,
94  std::vector<pcl::PointIndices> & cluster_indices_out)
95 {
97  ec.setClusterTolerance (cluster_tolerance);
98  ec.setMinClusterSize (min_cluster_size);
99  ec.setMaxClusterSize (max_cluster_size);
100 
101  ec.setInputCloud (input);
102  ec.extract (cluster_indices_out);
103 }
pcl::SAC_RANSAC
const static int SAC_RANSAC
Definition: method_types.h:45
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:355
pcl::EuclideanClusterExtraction::setClusterTolerance
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition: extract_clusters.h:339
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:371
pcl::ExtractIndices
ExtractIndices extracts a set of indices from a point cloud.
Definition: extract_indices.h:69
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:52
pcl::PCLBase::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:65
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:126
pcl::EuclideanClusterExtraction
EuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sen...
Definition: extract_clusters.h:294
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:72
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::PointIndices::Ptr
shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:22
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:176
pcl::ModelCoefficients::Ptr
shared_ptr< ::pcl::ModelCoefficients > Ptr
Definition: ModelCoefficients.h:23
pcl::FilterIndices::filter
void filter(PointCloud &output)
Definition: filter_indices.h:102
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:47
pcl::SACSegmentation
SACSegmentation represents the Nodelet segmentation class for Sample Consensus methods and models,...
Definition: sac_segmentation.h:64