Loading...
Searching...
No Matches
segmentation.h
#pragma once
#include "typedefs.h"
#include <pcl/ModelCoefficients.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/segmentation/extract_clusters.h>
/* Use SACSegmentation to find the dominant plane in the scene
* Inputs:
* input
* The input point cloud
* max_iterations
* The maximum number of RANSAC iterations to run
* distance_threshold
* The inlier/outlier threshold. Points within this distance
* from the hypothesized plane are scored as inliers.
* Return: A pointer to the ModelCoefficients (i.e., the 4 coefficients of the plane,
* represented in c0*x + c1*y + c2*z + c3 = 0 form)
*/
pcl::ModelCoefficients::Ptr
fitPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
{
// Initialize the SACSegmentation object
pcl::SACSegmentation<PointT> seg;
seg.setOptimizeCoefficients (true);
seg.setModelType (pcl::SACMODEL_PLANE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setDistanceThreshold (distance_threshold);
seg.setMaxIterations (max_iterations);
seg.setInputCloud (input);
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
seg.segment (*inliers, *coefficients);
return (coefficients);
}
/* Use SACSegmentation and an ExtractIndices filter to find the dominant plane and subtract it
* Inputs:
* input
* The input point cloud
* max_iterations
* The maximum number of RANSAC iterations to run
* distance_threshold
* The inlier/outlier threshold. Points within this distance
* from the hypothesized plane are scored as inliers.
* Return: A pointer to a new point cloud which contains only the non-plane points
*/
PointCloudPtr
findAndSubtractPlane (const PointCloudPtr & input, float distance_threshold, float max_iterations)
{
// Find the dominant plane
pcl::SACSegmentation<PointT> seg;
seg.setOptimizeCoefficients (false);
seg.setModelType (pcl::SACMODEL_PLANE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setDistanceThreshold (distance_threshold);
seg.setMaxIterations (max_iterations);
seg.setInputCloud (input);
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
seg.segment (*inliers, *coefficients);
// Extract the inliers
pcl::ExtractIndices<PointT> extract;
extract.setInputCloud (input);
extract.setIndices (inliers);
extract.setNegative (true);
PointCloudPtr output (new PointCloud);
extract.filter (*output);
return (output);
}
/* Use EuclidieanClusterExtraction to group a cloud into contiguous clusters
* Inputs:
* input
* The input point cloud
* cluster_tolerance
* The maximum distance between neighboring points in a cluster
* min/max_cluster_size
* The minimum and maximum allowable cluster sizes
* Return (by reference): a vector of PointIndices containing the points indices in each cluster
*/
void
clusterObjects (const PointCloudPtr & input,
float cluster_tolerance, int min_cluster_size, int max_cluster_size,
std::vector<pcl::PointIndices> & cluster_indices_out)
{
pcl::EuclideanClusterExtraction<PointT> ec;
ec.setClusterTolerance (cluster_tolerance);
ec.setMinClusterSize (min_cluster_size);
ec.setMaxClusterSize (max_cluster_size);
ec.setInputCloud (input);
ec.extract (cluster_indices_out);
}
pcl::EuclideanClusterExtraction
EuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sen...
Definition extract_clusters.h:328
pcl::EuclideanClusterExtraction::extract
void extract(std::vector< PointIndices > &clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
Definition extract_clusters.hpp:224
pcl::EuclideanClusterExtraction::setClusterTolerance
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition extract_clusters.h:368
pcl::EuclideanClusterExtraction::setMaxClusterSize
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.
Definition extract_clusters.h:400
pcl::EuclideanClusterExtraction::setMinClusterSize
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.
Definition extract_clusters.h:384
pcl::ExtractIndices
ExtractIndices extracts a set of indices from a point cloud.
Definition extract_indices.h:70
pcl::FilterIndices::filter
void filter(Indices &indices)
Calls the filtering method and returns the filtered point cloud indices.
Definition filter_indices.h:100
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:115
pcl::PCLBase::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition pcl_base.hpp:65
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::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition point_cloud.h:173
pcl::SACSegmentation
SACSegmentation represents the Nodelet segmentation class for Sample Consensus methods and models,...
Definition sac_segmentation.h:65
pcl::SACSegmentation::setMethodType
void setMethodType(int method)
The type of sample consensus method to use (user given parameter).
Definition sac_segmentation.h:113
pcl::SACSegmentation::setMaxIterations
void setMaxIterations(int max_iterations)
Set the maximum number of iterations before giving up.
Definition sac_segmentation.h:133
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:80
pcl::SACSegmentation::setModelType
void setModelType(int model)
The type of model to use (user given parameter).
Definition sac_segmentation.h:95
pcl::SACSegmentation::setDistanceThreshold
void setDistanceThreshold(double threshold)
Distance to the model threshold (user given parameter).
Definition sac_segmentation.h:123
pcl::SACSegmentation::setOptimizeCoefficients
void setOptimizeCoefficients(bool optimize)
Set to true if a coefficient refinement is required.
Definition sac_segmentation.h:160
pcl::SAC_RANSAC
constexpr int SAC_RANSAC
Definition method_types.h:45
pcl::SACMODEL_PLANE
@ SACMODEL_PLANE
Definition model_types.h:47
pcl::ModelCoefficients
Definition ModelCoefficients.h:12
pcl::ModelCoefficients::Ptr
shared_ptr< ::pcl::ModelCoefficients > Ptr
Definition ModelCoefficients.h:20
pcl::PointIndices
Definition PointIndices.h:12
pcl::PointIndices::Ptr
shared_ptr< ::pcl::PointIndices > Ptr
Definition PointIndices.h:13