41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
45 # pragma GCC system_header
50 #include <boost/math/distributions/binomial.hpp>
51 #include <pcl/sample_consensus/prosac.h>
55 template<
typename Po
intT>
bool
59 if (threshold_ == std::numeric_limits<double>::max())
61 PCL_ERROR (
"[pcl::ProgressiveSampleConsensus::computeModel] No threshold set!\n");
66 const int T_N = 200000;
67 const std::size_t N = sac_model_->indices_->size ();
68 const std::size_t m = sac_model_->getSampleSize ();
69 float T_n =
static_cast<float> (T_N);
70 for (
unsigned int i = 0; i < m; ++i)
71 T_n *=
static_cast<float> (m - i) /
static_cast<float> (N - i);
72 float T_prime_n = 1.0f;
73 std::size_t I_N_best = 0;
74 float n =
static_cast<float> (m);
77 float n_star =
static_cast<float> (N);
78 float epsilon_n_star = 0.0;
79 std::size_t k_n_star = T_N;
82 std::vector<unsigned int> I_n_star_min (N);
89 Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
93 index_pool.reserve (N);
94 for (
unsigned int i = 0; i < n; ++i)
95 index_pool.push_back (sac_model_->indices_->operator[](i));
98 while (
static_cast<unsigned int> (iterations_) < k_n_star)
104 if ((iterations_ == T_prime_n) && (n < n_star))
110 index_pool.push_back (sac_model_->indices_->at(
static_cast<unsigned int> (n - 1)));
112 float T_n_minus_1 = T_n;
113 T_n *= (
static_cast<float>(n) + 1.0f) / (
static_cast<float>(n) + 1.0f -
static_cast<float>(m));
114 T_prime_n += std::ceil (T_n - T_n_minus_1);
118 sac_model_->indices_->swap (index_pool);
120 sac_model_->getSamples (iterations_, selection);
121 if (T_prime_n < iterations_)
123 selection.pop_back ();
124 selection.push_back (sac_model_->indices_->at(
static_cast<unsigned int> (n - 1)));
128 sac_model_->indices_->swap (index_pool);
130 if (selection.empty ())
132 PCL_ERROR (
"[pcl::ProgressiveSampleConsensus::computeModel] No samples could be selected!\n");
137 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
145 sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
147 std::size_t I_N = inliers.size ();
157 model_coefficients_ = model_coefficients;
160 std::sort (inliers.begin (), inliers.end ());
164 std::size_t possible_n_star_best = N, I_possible_n_star_best = I_N;
165 float epsilon_possible_n_star_best =
static_cast<float>(I_possible_n_star_best) /
static_cast<float>(possible_n_star_best);
168 std::size_t I_possible_n_star = I_N;
169 for (
auto last_inlier = inliers.crbegin (), inliers_end = inliers.crend ();
170 last_inlier != inliers_end;
171 ++last_inlier, --I_possible_n_star)
174 unsigned int possible_n_star = (*last_inlier) + 1;
175 if (possible_n_star <= m)
179 float epsilon_possible_n_star =
static_cast<float>(I_possible_n_star) /
static_cast<float>(possible_n_star);
181 if ((epsilon_possible_n_star > epsilon_n_star) && (epsilon_possible_n_star > epsilon_possible_n_star_best))
184 std::size_t I_possible_n_star_min = m
185 +
static_cast<std::size_t
> (std::ceil (boost::math::quantile (boost::math::complement (boost::math::binomial_distribution<float>(
static_cast<float> (possible_n_star), 0.1f), 0.05))));
187 if (I_possible_n_star < I_possible_n_star_min)
190 possible_n_star_best = possible_n_star;
191 I_possible_n_star_best = I_possible_n_star;
192 epsilon_possible_n_star_best = epsilon_possible_n_star;
197 if (epsilon_possible_n_star_best > epsilon_n_star)
200 epsilon_n_star = epsilon_possible_n_star_best;
203 float bottom_log = 1 - std::pow (epsilon_n_star,
static_cast<float>(m));
206 else if (bottom_log == 1)
209 k_n_star =
static_cast<int> (std::ceil (std::log (0.05) / std::log (bottom_log)));
211 k_n_star = (std::max)(k_n_star, 2 * m);
216 if (debug_verbosity_level > 1)
217 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);
218 if (iterations_ > max_iterations_)
220 if (debug_verbosity_level > 0)
221 PCL_DEBUG (
"[pcl::ProgressiveSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
226 if (debug_verbosity_level > 0)
227 PCL_DEBUG (
"[pcl::ProgressiveSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), I_N_best);
238 #define PCL_INSTANTIATE_ProgressiveSampleConsensus(T) template class PCL_EXPORTS pcl::ProgressiveSampleConsensus<T>;
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
IndicesAllocator<> Indices
Type used for indices in PCL.