Point Cloud Library (PCL)  1.14.1-dev
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 }
EuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sen...
void extract(std::vector< PointIndices > &clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
void setMaxClusterSize(pcl::uindex_t max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid.
void setMinClusterSize(pcl::uindex_t min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid.
ExtractIndices extracts a set of indices from a point cloud.
void filter(Indices &indices)
Calls the filtering method and returns the filtered point cloud indices.
void setNegative(bool negative)
Set whether the regular conditions for points filtering should apply, or the inverted conditions.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:65
virtual void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
Definition: pcl_base.hpp:72
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
SACSegmentation represents the Nodelet segmentation class for Sample Consensus methods and models,...
void setMethodType(int method)
The type of sample consensus method to use (user given parameter).
void setMaxIterations(int max_iterations)
Set the maximum number of iterations before giving up.
virtual void segment(PointIndices &inliers, ModelCoefficients &model_coefficients)
Base method for segmentation of a model in a PointCloud given by <setInputCloud (),...
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).
void setOptimizeCoefficients(bool optimize)
Set to true if a coefficient refinement is required.
constexpr int SAC_RANSAC
Definition: method_types.h:45
@ SACMODEL_PLANE
Definition: model_types.h:47
shared_ptr< ::pcl::ModelCoefficients > Ptr
shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:13