Point Cloud Library (PCL) 1.12.0
Loading...
Searching...
No Matches
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 */
26fitPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
27{
28 // Initialize the SACSegmentation object
30 seg.setOptimizeCoefficients (true);
31 seg.setModelType (pcl::SACMODEL_PLANE);
32 seg.setMethodType (pcl::SAC_RANSAC);
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 */
55PointCloudPtr
56findAndSubtractPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
57{
58 // Find the dominant plane
60 seg.setOptimizeCoefficients (false);
61 seg.setModelType (pcl::SACMODEL_PLANE);
62 seg.setMethodType (pcl::SAC_RANSAC);
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 */
91void
92clusterObjects (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}
Iterator class for point clouds with or without given indices.
PointCloud represents the base class in PCL for storing collections of 3D points.
@ SACMODEL_PLANE
Definition model_types.h:47
static const int SAC_RANSAC
shared_ptr< ::pcl::ModelCoefficients > Ptr
shared_ptr< ::pcl::PointIndices > Ptr