41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_MLESAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_MLESAC_H_
45 #include <pcl/sample_consensus/mlesac.h>
49 template <
typename Po
intT>
bool
53 if (threshold_ == std::numeric_limits<double>::max())
55 PCL_ERROR (
"[pcl::MaximumLikelihoodSampleConsensus::computeModel] No threshold set!\n");
60 double d_best_penalty = std::numeric_limits<double>::max();
63 const double log_probability = std::log (1.0 - probability_);
64 const double one_over_indices = 1.0 /
static_cast<double> (sac_model_->getIndices ()->size ());
67 Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
68 std::vector<double> distances;
71 sigma_ = computeMedianAbsoluteDeviation (sac_model_->getInputCloud (), sac_model_->getIndices (), threshold_);
72 const double dist_scaling_factor = -1.0 / (2.0 * sigma_ * sigma_);
73 const double normalization_factor = 1.0 / (sqrt (2 *
M_PI) * sigma_);
74 if (debug_verbosity_level > 1)
75 PCL_DEBUG (
"[pcl::MaximumLikelihoodSampleConsensus::computeModel] Estimated sigma value: %f.\n", sigma_);
78 Eigen::Vector4f min_pt, max_pt;
79 getMinMax (sac_model_->getInputCloud (), sac_model_->getIndices (), min_pt, max_pt);
81 double v = sqrt (max_pt.dot (max_pt));
83 int n_inliers_count = 0;
84 std::size_t indices_size;
85 unsigned skipped_count = 0;
87 const unsigned max_skip = max_iterations_ * 10;
90 while (iterations_ < k && skipped_count < max_skip)
93 sac_model_->getSamples (iterations_, selection);
95 if (selection.empty ())
break;
98 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
106 sac_model_->getDistancesToModel (model_coefficients, distances);
108 if (distances.empty ())
118 double p_outlier_prob = 0;
120 indices_size = sac_model_->getIndices ()->size ();
121 std::vector<double> p_inlier_prob (indices_size);
122 for (
int j = 0; j < iterations_EM_; ++j)
124 const double weighted_normalization_factor = gamma * normalization_factor;
126 for (std::size_t i = 0; i < indices_size; ++i)
127 p_inlier_prob[i] = weighted_normalization_factor * std::exp ( dist_scaling_factor * distances[i] * distances[i] );
130 p_outlier_prob = (1 - gamma) / v;
133 for (std::size_t i = 0; i < indices_size; ++i)
134 gamma += p_inlier_prob [i] / (p_inlier_prob[i] + p_outlier_prob);
135 gamma /=
static_cast<double>(sac_model_->getIndices ()->size ());
139 double d_cur_penalty = 0;
140 for (std::size_t i = 0; i < indices_size; ++i)
141 d_cur_penalty += std::log (p_inlier_prob[i] + p_outlier_prob);
142 d_cur_penalty = - d_cur_penalty;
145 if (d_cur_penalty < d_best_penalty)
147 d_best_penalty = d_cur_penalty;
151 model_coefficients_ = model_coefficients;
155 for (
const double &
distance : distances)
160 const double w =
static_cast<double> (n_inliers_count) * one_over_indices;
161 double p_outliers = 1.0 - std::pow (w,
static_cast<double> (selection.size ()));
162 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers);
163 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers);
164 k = log_probability / std::log (p_outliers);
168 if (debug_verbosity_level > 1)
169 PCL_DEBUG (
"[pcl::MaximumLikelihoodSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_,
static_cast<int> (std::ceil (k)), d_best_penalty);
170 if (iterations_ > max_iterations_)
172 if (debug_verbosity_level > 0)
173 PCL_DEBUG (
"[pcl::MaximumLikelihoodSampleConsensus::computeModel] MLESAC reached the maximum number of trials.\n");
180 if (debug_verbosity_level > 0)
181 PCL_DEBUG (
"[pcl::MaximumLikelihoodSampleConsensus::computeModel] Unable to find a solution!\n");
186 sac_model_->getDistancesToModel (model_coefficients_, distances);
187 Indices &indices = *sac_model_->getIndices ();
188 if (distances.size () != indices.size ())
190 PCL_ERROR (
"[pcl::MaximumLikelihoodSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
194 inliers_.resize (distances.size ());
197 for (std::size_t i = 0; i < distances.size (); ++i)
198 if (distances[i] <= 2 * sigma_)
199 inliers_[n_inliers_count++] = indices[i];
202 inliers_.resize (n_inliers_count);
204 if (debug_verbosity_level > 0)
205 PCL_DEBUG (
"[pcl::MaximumLikelihoodSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
211 template <
typename Po
intT>
double
213 const PointCloudConstPtr &cloud,
217 std::vector<double> distances (indices->size ());
219 Eigen::Vector4f median;
223 for (std::size_t i = 0; i < indices->size (); ++i)
226 Eigen::Vector4f ptdiff = pt - median;
228 distances[i] = ptdiff.dot (ptdiff);
231 const double result =
pcl::computeMedian (distances.begin (), distances.end (),
static_cast<double(*)(
double)
>(std::sqrt));
232 return (sigma * result);
236 template <
typename Po
intT>
void
238 const PointCloudConstPtr &cloud,
240 Eigen::Vector4f &min_p,
241 Eigen::Vector4f &max_p)
const
243 min_p.setConstant (std::numeric_limits<float>::max());
244 max_p.setConstant (std::numeric_limits<float>::lowest());
245 min_p[3] = max_p[3] = 0;
247 for (std::size_t i = 0; i < indices->size (); ++i)
249 if ((*cloud)[(*indices)[i]].x < min_p[0]) min_p[0] = (*cloud)[(*indices)[i]].x;
250 if ((*cloud)[(*indices)[i]].y < min_p[1]) min_p[1] = (*cloud)[(*indices)[i]].y;
251 if ((*cloud)[(*indices)[i]].z < min_p[2]) min_p[2] = (*cloud)[(*indices)[i]].z;
253 if ((*cloud)[(*indices)[i]].x > max_p[0]) max_p[0] = (*cloud)[(*indices)[i]].x;
254 if ((*cloud)[(*indices)[i]].y > max_p[1]) max_p[1] = (*cloud)[(*indices)[i]].y;
255 if ((*cloud)[(*indices)[i]].z > max_p[2]) max_p[2] = (*cloud)[(*indices)[i]].z;
260 template <
typename Po
intT>
void
262 const PointCloudConstPtr &cloud,
264 Eigen::Vector4f &median)
const
267 std::vector<float> x (indices->size ());
268 std::vector<float> y (indices->size ());
269 std::vector<float> z (indices->size ());
270 for (std::size_t i = 0; i < indices->size (); ++i)
272 x[i] = (*cloud)[(*indices)[i]].x;
273 y[i] = (*cloud)[(*indices)[i]].y;
274 z[i] = (*cloud)[(*indices)[i]].z;
283 #define PCL_INSTANTIATE_MaximumLikelihoodSampleConsensus(T) template class PCL_EXPORTS pcl::MaximumLikelihoodSampleConsensus<T>;
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...
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.
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
double computeMedianAbsoluteDeviation(const PointCloudConstPtr &cloud, const IndicesPtr &indices, double sigma) const
Compute the median absolute deviation:
Define standard C methods and C++ classes that are common to all methods.
auto computeMedian(IteratorT begin, IteratorT end, Functor f) noexcept -> std::result_of_t< Functor(decltype(*begin))>
Compute the median of a list of values (fast).
void getMinMax(const PointT &histogram, int len, float &min_p, float &max_p)
Get the minimum and maximum values on a point histogram.
float distance(const PointT &p1, const PointT &p2)
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst
IndicesAllocator<> Indices
Type used for indices in PCL.
shared_ptr< Indices > IndicesPtr