38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
41 #include <boost/math/distributions/binomial.hpp>
46 template<
typename Po
intT>
bool
50 if (threshold_ == DBL_MAX)
52 PCL_ERROR (
"[pcl::ProgressiveSampleConsensus::computeModel] No threshold set!\n");
57 const int T_N = 200000;
58 const size_t N = sac_model_->indices_->size ();
59 const size_t m = sac_model_->getSampleSize ();
60 float T_n =
static_cast<float> (T_N);
61 for (
unsigned int i = 0; i < m; ++i)
62 T_n *= static_cast<float> (m - i) /
static_cast<float> (N - i);
63 float T_prime_n = 1.0f;
65 float n =
static_cast<float> (m);
68 float n_star =
static_cast<float> (N);
69 float epsilon_n_star = 0.0;
70 size_t k_n_star = T_N;
73 std::vector<unsigned int> I_n_star_min (N);
78 std::vector<int> inliers;
79 std::vector<int> selection;
80 Eigen::VectorXf model_coefficients;
83 std::vector<int> index_pool;
84 index_pool.reserve (N);
85 for (
unsigned int i = 0; i < n; ++i)
86 index_pool.push_back (sac_model_->indices_->operator[](i));
89 while (static_cast<unsigned int> (iterations_) < k_n_star)
95 if ((iterations_ == T_prime_n) && (n < n_star))
101 index_pool.push_back (sac_model_->indices_->at(static_cast<unsigned int> (n - 1)));
103 float T_n_minus_1 = T_n;
104 T_n *= (
static_cast<float>(n) + 1.0f) / (
static_cast<float>(n) + 1.0f - static_cast<float>(m));
105 T_prime_n += ceilf (T_n - T_n_minus_1);
109 sac_model_->indices_->swap (index_pool);
111 sac_model_->getSamples (iterations_, selection);
112 if (T_prime_n < iterations_)
114 selection.pop_back ();
115 selection.push_back (sac_model_->indices_->at(static_cast<unsigned int> (n - 1)));
119 sac_model_->indices_->swap (index_pool);
121 if (selection.empty ())
123 PCL_ERROR (
"[pcl::ProgressiveSampleConsensus::computeModel] No samples could be selected!\n");
128 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
136 sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
138 size_t I_N = inliers.size ();
148 model_coefficients_ = model_coefficients;
151 std::sort (inliers.begin (), inliers.end ());
155 size_t possible_n_star_best = N, I_possible_n_star_best = I_N;
156 float epsilon_possible_n_star_best =
static_cast<float>(I_possible_n_star_best) / static_cast<float>(possible_n_star_best);
159 size_t I_possible_n_star = I_N;
160 for (std::vector<int>::const_reverse_iterator last_inlier = inliers.rbegin (),
161 inliers_end = inliers.rend ();
162 last_inlier != inliers_end;
163 ++last_inlier, --I_possible_n_star)
166 unsigned int possible_n_star = (*last_inlier) + 1;
167 if (possible_n_star <= m)
171 float epsilon_possible_n_star =
static_cast<float>(I_possible_n_star) / static_cast<float>(possible_n_star);
173 if ((epsilon_possible_n_star > epsilon_n_star) && (epsilon_possible_n_star > epsilon_possible_n_star_best))
175 using namespace boost::math;
177 size_t I_possible_n_star_min = m
178 +
static_cast<size_t> (ceil (quantile (complement (binomial_distribution<float>(static_cast<float> (possible_n_star), 0.1f), 0.05))));
180 if (I_possible_n_star < I_possible_n_star_min)
183 possible_n_star_best = possible_n_star;
184 I_possible_n_star_best = I_possible_n_star;
185 epsilon_possible_n_star_best = epsilon_possible_n_star;
190 if (epsilon_possible_n_star_best > epsilon_n_star)
193 epsilon_n_star = epsilon_possible_n_star_best;
196 float bottom_log = 1 - std::pow (epsilon_n_star, static_cast<float>(m));
199 else if (bottom_log == 1)
202 k_n_star =
static_cast<int> (ceil (log (0.05) / log (bottom_log)));
204 k_n_star = (std::max)(k_n_star, 2 * m);
209 if (debug_verbosity_level > 1)
210 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);
211 if (iterations_ > max_iterations_)
213 if (debug_verbosity_level > 0)
214 PCL_DEBUG (
"[pcl::ProgressiveSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
219 if (debug_verbosity_level > 0)
220 PCL_DEBUG (
"[pcl::ProgressiveSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), I_N_best);
233 #define PCL_INSTANTIATE_ProgressiveSampleConsensus(T) template class PCL_EXPORTS pcl::ProgressiveSampleConsensus<T>;
235 #endif // PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_