41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
45 # pragma GCC system_header
48 #include <boost/math/distributions/binomial.hpp>
49 #include <pcl/sample_consensus/prosac.h>
53 template<
typename Po
intT>
bool
57 if (threshold_ == DBL_MAX)
59 PCL_ERROR (
"[pcl::ProgressiveSampleConsensus::computeModel] No threshold set!\n");
64 const int T_N = 200000;
65 const size_t N = sac_model_->indices_->size ();
66 const size_t m = sac_model_->getSampleSize ();
67 float T_n =
static_cast<float> (T_N);
68 for (
unsigned int i = 0; i < m; ++i)
69 T_n *= static_cast<float> (m - i) /
static_cast<float> (N - i);
70 float T_prime_n = 1.0f;
72 float n =
static_cast<float> (m);
75 float n_star =
static_cast<float> (N);
76 float epsilon_n_star = 0.0;
77 size_t k_n_star = T_N;
80 std::vector<unsigned int> I_n_star_min (N);
85 std::vector<int> inliers;
86 std::vector<int> selection;
87 Eigen::VectorXf model_coefficients;
90 std::vector<int> index_pool;
91 index_pool.reserve (N);
92 for (
unsigned int i = 0; i < n; ++i)
93 index_pool.push_back (sac_model_->indices_->operator[](i));
96 while (static_cast<unsigned int> (iterations_) < k_n_star)
102 if ((iterations_ == T_prime_n) && (n < n_star))
108 index_pool.push_back (sac_model_->indices_->at(static_cast<unsigned int> (n - 1)));
110 float T_n_minus_1 = T_n;
111 T_n *= (
static_cast<float>(n) + 1.0f) / (
static_cast<float>(n) + 1.0f - static_cast<float>(m));
112 T_prime_n += ceilf (T_n - T_n_minus_1);
116 sac_model_->indices_->swap (index_pool);
118 sac_model_->getSamples (iterations_, selection);
119 if (T_prime_n < iterations_)
121 selection.pop_back ();
122 selection.push_back (sac_model_->indices_->at(static_cast<unsigned int> (n - 1)));
126 sac_model_->indices_->swap (index_pool);
128 if (selection.empty ())
130 PCL_ERROR (
"[pcl::ProgressiveSampleConsensus::computeModel] No samples could be selected!\n");
135 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
143 sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
145 size_t I_N = inliers.size ();
155 model_coefficients_ = model_coefficients;
158 std::sort (inliers.begin (), inliers.end ());
162 size_t possible_n_star_best = N, I_possible_n_star_best = I_N;
163 float epsilon_possible_n_star_best =
static_cast<float>(I_possible_n_star_best) / static_cast<float>(possible_n_star_best);
166 size_t I_possible_n_star = I_N;
167 for (std::vector<int>::const_reverse_iterator last_inlier = inliers.rbegin (),
168 inliers_end = inliers.rend ();
169 last_inlier != inliers_end;
170 ++last_inlier, --I_possible_n_star)
173 unsigned int possible_n_star = (*last_inlier) + 1;
174 if (possible_n_star <= m)
178 float epsilon_possible_n_star =
static_cast<float>(I_possible_n_star) / static_cast<float>(possible_n_star);
180 if ((epsilon_possible_n_star > epsilon_n_star) && (epsilon_possible_n_star > epsilon_possible_n_star_best))
183 size_t I_possible_n_star_min = m
184 +
static_cast<size_t> (ceil (boost::math::quantile (boost::math::complement (boost::math::binomial_distribution<float>(static_cast<float> (possible_n_star), 0.1f), 0.05))));
186 if (I_possible_n_star < I_possible_n_star_min)
189 possible_n_star_best = possible_n_star;
190 I_possible_n_star_best = I_possible_n_star;
191 epsilon_possible_n_star_best = epsilon_possible_n_star;
196 if (epsilon_possible_n_star_best > epsilon_n_star)
199 epsilon_n_star = epsilon_possible_n_star_best;
202 float bottom_log = 1 - std::pow (epsilon_n_star, static_cast<float>(m));
205 else if (bottom_log == 1)
208 k_n_star =
static_cast<int> (ceil (log (0.05) / log (bottom_log)));
210 k_n_star = (std::max)(k_n_star, 2 * m);
215 if (debug_verbosity_level > 1)
216 PCL_DEBUG (
"[pcl::ProgressiveSampleConsensus::computeModel] Trial %d out of %d: %d inliers (best is: %d so far).\n", iterations_, k_n_star, I_N, I_N_best);
217 if (iterations_ > max_iterations_)
219 if (debug_verbosity_level > 0)
220 PCL_DEBUG (
"[pcl::ProgressiveSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
225 if (debug_verbosity_level > 0)
226 PCL_DEBUG (
"[pcl::ProgressiveSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), I_N_best);
237 #define PCL_INSTANTIATE_ProgressiveSampleConsensus(T) template class PCL_EXPORTS pcl::ProgressiveSampleConsensus<T>;
239 #endif // PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
bool computeModel(int debug_verbosity_level=0)
Compute the actual model and find the inliers.