Point Cloud Library (PCL)  1.9.1-dev
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 
46 namespace pcl
47 {
48  /** \brief @b MaximumLikelihoodSampleConsensus represents an implementation of the MLESAC (Maximum Likelihood
49  * Estimator SAmple Consensus) algorithm, as described in: "MLESAC: A new robust estimator with application to
50  * estimating image geometry", P.H.S. Torr and A. Zisserman, Computer Vision and Image Understanding, vol 78, 2000.
51  * \note MLESAC is useful in situations where most of the data samples belong to the model, and a fast outlier rejection algorithm is needed.
52  * \author Radu B. Rusu
53  * \ingroup sample_consensus
54  */
55  template <typename PointT>
57  {
58  using SampleConsensusModelPtr = typename SampleConsensusModel<PointT>::Ptr;
59  using PointCloudConstPtr = typename SampleConsensusModel<PointT>::PointCloudConstPtr;
60 
61  public:
62  using Ptr = boost::shared_ptr<MaximumLikelihoodSampleConsensus<PointT> >;
63  using ConstPtr = boost::shared_ptr<const MaximumLikelihoodSampleConsensus<PointT> >;
64 
73 
74  /** \brief MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor
75  * \param[in] model a Sample Consensus model
76  */
77  MaximumLikelihoodSampleConsensus (const SampleConsensusModelPtr &model) :
78  SampleConsensus<PointT> (model),
79  iterations_EM_ (3), // Max number of EM (Expectation Maximization) iterations
80  sigma_ (0)
81  {
82  max_iterations_ = 10000; // Maximum number of trials before we give up.
83  }
84 
85  /** \brief MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor
86  * \param[in] model a Sample Consensus model
87  * \param[in] threshold distance to model threshold
88  */
89  MaximumLikelihoodSampleConsensus (const SampleConsensusModelPtr &model, double threshold) :
90  SampleConsensus<PointT> (model, threshold),
91  iterations_EM_ (3), // Max number of EM (Expectation Maximization) iterations
92  sigma_ (0)
93  {
94  max_iterations_ = 10000; // Maximum number of trials before we give up.
95  }
96 
97  /** \brief Compute the actual model and find the inliers
98  * \param[in] debug_verbosity_level enable/disable on-screen debug information and set the verbosity level
99  */
100  bool
101  computeModel (int debug_verbosity_level = 0) override;
102 
103  /** \brief Set the number of EM iterations.
104  * \param[in] iterations the number of EM iterations
105  */
106  inline void
107  setEMIterations (int iterations) { iterations_EM_ = iterations; }
108 
109  /** \brief Get the number of EM iterations. */
110  inline int
111  getEMIterations () const { return (iterations_EM_); }
112 
113 
114  protected:
115  /** \brief Compute the median absolute deviation:
116  * \f[
117  * MAD = \sigma * median_i (| Xi - median_j(Xj) |)
118  * \f]
119  * \note Sigma needs to be chosen carefully (a good starting sigma value is 1.4826)
120  * \param[in] cloud the point cloud data message
121  * \param[in] indices the set of point indices to use
122  * \param[in] sigma the sigma value
123  */
124  double
125  computeMedianAbsoluteDeviation (const PointCloudConstPtr &cloud,
126  const boost::shared_ptr <std::vector<int> > &indices,
127  double sigma) const;
128 
129  /** \brief Determine the minimum and maximum 3D bounding box coordinates for a given set of points
130  * \param[in] cloud the point cloud message
131  * \param[in] indices the set of point indices to use
132  * \param[out] min_p the resultant minimum bounding box coordinates
133  * \param[out] max_p the resultant maximum bounding box coordinates
134  */
135  void
136  getMinMax (const PointCloudConstPtr &cloud,
137  const boost::shared_ptr <std::vector<int> > &indices,
138  Eigen::Vector4f &min_p,
139  Eigen::Vector4f &max_p) const;
140 
141  /** \brief Compute the median value of a 3D point cloud using a given set point indices and return it as a Point32.
142  * \param[in] cloud the point cloud data message
143  * \param[in] indices the point indices
144  * \param[out] median the resultant median value
145  */
146  void
147  computeMedian (const PointCloudConstPtr &cloud,
148  const boost::shared_ptr <std::vector<int> > &indices,
149  Eigen::Vector4f &median) const;
150 
151  private:
152  /** \brief Maximum number of EM (Expectation Maximization) iterations. */
153  int iterations_EM_;
154  /** \brief The MLESAC sigma parameter. */
155  double sigma_;
156  };
157 }
158 
159 #ifdef PCL_NO_PRECOMPILE
160 #include <pcl/sample_consensus/impl/mlesac.hpp>
161 #endif
boost::shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:75
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
void getMinMax(const PointCloudConstPtr &cloud, const boost::shared_ptr< std::vector< int > > &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
MaximumLikelihoodSampleConsensus(const SampleConsensusModelPtr &model)
MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor.
Definition: mlesac.h:77
MaximumLikelihoodSampleConsensus represents an implementation of the MLESAC (Maximum Likelihood Estim...
Definition: mlesac.h:56
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:71
void computeMedian(const PointCloudConstPtr &cloud, const boost::shared_ptr< std::vector< int > > &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
boost::shared_ptr< const MaximumLikelihoodSampleConsensus< PointT > > ConstPtr
Definition: mlesac.h:63
boost::shared_ptr< MaximumLikelihoodSampleConsensus< PointT > > Ptr
Definition: mlesac.h:62
MaximumLikelihoodSampleConsensus(const SampleConsensusModelPtr &model, double threshold)
MLESAC (Maximum Likelihood Estimator SAmple Consensus) main constructor.
Definition: mlesac.h:89
int getEMIterations() const
Get the number of EM iterations.
Definition: mlesac.h:111
double computeMedianAbsoluteDeviation(const PointCloudConstPtr &cloud, const boost::shared_ptr< std::vector< int > > &indices, double sigma) const
Compute the median absolute deviation: .
Definition: mlesac.hpp:207
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.
int max_iterations_
Maximum number of iterations before giving up.
Definition: sac.h:324
SampleConsensus represents the base class.
Definition: sac.h:57
void setEMIterations(int iterations)
Set the number of EM iterations.
Definition: mlesac.h:107