Point Cloud Library (PCL)  1.10.0
mlesac.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #pragma once
42 
43 #include <pcl/sample_consensus/sac.h>
44 #include <pcl/sample_consensus/sac_model.h>
45 #include <pcl/pcl_base.h>
46 
47 namespace pcl
48 {
49  /** \brief @b MaximumLikelihoodSampleConsensus represents an implementation of the MLESAC (Maximum Likelihood
50  * Estimator SAmple Consensus) algorithm, as described in: "MLESAC: A new robust estimator with application to
51  * estimating image geometry", P.H.S. Torr and A. Zisserman, Computer Vision and Image Understanding, vol 78, 2000.
52  * \note MLESAC is useful in situations where most of the data samples belong to the model, and a fast outlier rejection algorithm is needed.
53  * \author Radu B. Rusu
54  * \ingroup sample_consensus
55  */
56  template <typename PointT>
58  {
59  using SampleConsensusModelPtr = typename SampleConsensusModel<PointT>::Ptr;
60  using PointCloudConstPtr = typename SampleConsensusModel<PointT>::PointCloudConstPtr;
61 
62  public:
65 
74 
75  /** \brief MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor
76  * \param[in] model a Sample Consensus model
77  */
78  MaximumLikelihoodSampleConsensus (const SampleConsensusModelPtr &model) :
79  SampleConsensus<PointT> (model),
80  iterations_EM_ (3), // Max number of EM (Expectation Maximization) iterations
81  sigma_ (0)
82  {
83  max_iterations_ = 10000; // Maximum number of trials before we give up.
84  }
85 
86  /** \brief MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor
87  * \param[in] model a Sample Consensus model
88  * \param[in] threshold distance to model threshold
89  */
90  MaximumLikelihoodSampleConsensus (const SampleConsensusModelPtr &model, double threshold) :
91  SampleConsensus<PointT> (model, threshold),
92  iterations_EM_ (3), // Max number of EM (Expectation Maximization) iterations
93  sigma_ (0)
94  {
95  max_iterations_ = 10000; // Maximum number of trials before we give up.
96  }
97 
98  /** \brief Compute the actual model and find the inliers
99  * \param[in] debug_verbosity_level enable/disable on-screen debug information and set the verbosity level
100  */
101  bool
102  computeModel (int debug_verbosity_level = 0) override;
103 
104  /** \brief Set the number of EM iterations.
105  * \param[in] iterations the number of EM iterations
106  */
107  inline void
108  setEMIterations (int iterations) { iterations_EM_ = iterations; }
109 
110  /** \brief Get the number of EM iterations. */
111  inline int
112  getEMIterations () const { return (iterations_EM_); }
113 
114 
115  protected:
116  /** \brief Compute the median absolute deviation:
117  * \f[
118  * MAD = \sigma * median_i (| Xi - median_j(Xj) |)
119  * \f]
120  * \note Sigma needs to be chosen carefully (a good starting sigma value is 1.4826)
121  * \param[in] cloud the point cloud data message
122  * \param[in] indices the set of point indices to use
123  * \param[in] sigma the sigma value
124  */
125  double
126  computeMedianAbsoluteDeviation (const PointCloudConstPtr &cloud,
127  const IndicesPtr &indices,
128  double sigma) const;
129 
130  /** \brief Determine the minimum and maximum 3D bounding box coordinates for a given set of points
131  * \param[in] cloud the point cloud message
132  * \param[in] indices the set of point indices to use
133  * \param[out] min_p the resultant minimum bounding box coordinates
134  * \param[out] max_p the resultant maximum bounding box coordinates
135  */
136  void
137  getMinMax (const PointCloudConstPtr &cloud,
138  const IndicesPtr &indices,
139  Eigen::Vector4f &min_p,
140  Eigen::Vector4f &max_p) const;
141 
142  /** \brief Compute the median value of a 3D point cloud using a given set point indices and return it as a Point32.
143  * \param[in] cloud the point cloud data message
144  * \param[in] indices the point indices
145  * \param[out] median the resultant median value
146  */
147  void
148  computeMedian (const PointCloudConstPtr &cloud,
149  const IndicesPtr &indices,
150  Eigen::Vector4f &median) const;
151 
152  private:
153  /** \brief Maximum number of EM (Expectation Maximization) iterations. */
154  int iterations_EM_;
155  /** \brief The MLESAC sigma parameter. */
156  double sigma_;
157  };
158 }
159 
160 #ifdef PCL_NO_PRECOMPILE
161 #include <pcl/sample_consensus/impl/mlesac.hpp>
162 #endif
double computeMedianAbsoluteDeviation(const PointCloudConstPtr &cloud, const IndicesPtr &indices, double sigma) const
Compute the median absolute deviation: .
Definition: mlesac.hpp:207
shared_ptr< MaximumLikelihoodSampleConsensus< PointT > > Ptr
Definition: mlesac.h:63
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:61
MaximumLikelihoodSampleConsensus(const SampleConsensusModelPtr &model)
MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor.
Definition: mlesac.h:78
MaximumLikelihoodSampleConsensus represents an implementation of the MLESAC (Maximum Likelihood Estim...
Definition: mlesac.h:57
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:72
void getMinMax(const PointCloudConstPtr &cloud, const IndicesPtr &indices, Eigen::Vector4f &min_p, Eigen::Vector4f &max_p) const
Determine the minimum and maximum 3D bounding box coordinates for a given set of points.
Definition: mlesac.hpp:240
shared_ptr< const MaximumLikelihoodSampleConsensus< PointT > > ConstPtr
Definition: mlesac.h:64
void computeMedian(const PointCloudConstPtr &cloud, const IndicesPtr &indices, Eigen::Vector4f &median) const
Compute the median value of a 3D point cloud using a given set point indices and return it as a Point...
Definition: mlesac.hpp:264
MaximumLikelihoodSampleConsensus(const SampleConsensusModelPtr &model, double threshold)
MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor.
Definition: mlesac.h:90
int getEMIterations() const
Get the number of EM iterations.
Definition: mlesac.h:112
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition: mlesac.hpp:49
A point structure representing Euclidean xyz coordinates, and the RGB color.
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:76
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
int max_iterations_
Maximum number of iterations before giving up.
Definition: sac.h:339
SampleConsensus represents the base class.
Definition: sac.h:58
void setEMIterations(int iterations)
Set the number of EM iterations.
Definition: mlesac.h:108