Skip to content

Commit da7f34a

Browse files
committed
Set better default for RRANSAC and RMSAC
RRANSAC is a faster variant of RANSAC. It performs a pretest before counting all inliers of a candidate model (this is the costly part of RANSAC). The pretest consists of choosing a number of random sample points and then testing if _all_ these points are inliers. If just one is an outlier, the candidate model is discarded. The previous default was to choose 10 percent of all points for this pretest, which is far too much, and the probability of a candidate model (even a good one!) not being discarded is near zero. Therefore RRANSAC fails to find a model in many cases. The new default is to use one point for the pretest. This has a good chance of rejecting very bad models, but not reject any good models. This is a good default for many different datasets. The same is done for RMSAC, which is a faster variant of MSAC. MSAC uses a different way to judge the quality of a candidate model than RANSAC, but works very similar otherwise.
1 parent df3af0a commit da7f34a

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

sample_consensus/include/pcl/sample_consensus/impl/rmsac.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ pcl::RandomizedMEstimatorSampleConsensus<PointT>::computeModel (int debug_verbos
7272
const unsigned max_skip = max_iterations_ * 10;
7373

7474
// Number of samples to try randomly
75-
std::size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
75+
const std::size_t fraction_nr_points = (fraction_nr_pretest_ < 0.0 ? nr_samples_pretest_ : pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0));
76+
PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Using %lu points for RMSAC pre-test.\n", fraction_nr_points);
7677

7778
// Iterate
7879
while (iterations_ < k && skipped_count < max_skip)

sample_consensus/include/pcl/sample_consensus/impl/rransac.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pcl::RandomizedRandomSampleConsensus<PointT>::computeModel (int debug_verbosity_
7171
const unsigned max_skip = max_iterations_ * 10;
7272

7373
// Number of samples to try randomly
74-
const std::size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
74+
const std::size_t fraction_nr_points = (fraction_nr_pretest_ < 0.0 ? nr_samples_pretest_ : pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0));
75+
PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Using %lu points for RRANSAC pre-test.\n", fraction_nr_points);
7576

7677
// Iterate
7778
while (iterations_ < k)

sample_consensus/include/pcl/sample_consensus/rmsac.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ namespace pcl
7575
*/
7676
RandomizedMEstimatorSampleConsensus (const SampleConsensusModelPtr &model)
7777
: SampleConsensus<PointT> (model)
78-
, fraction_nr_pretest_ (10.0) // Number of samples to try randomly in percents
78+
, fraction_nr_pretest_ (-1.0)
79+
, nr_samples_pretest_ (1)
7980
{
8081
// Maximum number of trials before we give up.
8182
max_iterations_ = 10000;
@@ -87,7 +88,8 @@ namespace pcl
8788
*/
8889
RandomizedMEstimatorSampleConsensus (const SampleConsensusModelPtr &model, double threshold)
8990
: SampleConsensus<PointT> (model, threshold)
90-
, fraction_nr_pretest_ (10.0) // Number of samples to try randomly in percents
91+
, fraction_nr_pretest_ (-1.0)
92+
, nr_samples_pretest_ (1)
9193
{
9294
// Maximum number of trials before we give up.
9395
max_iterations_ = 10000;
@@ -100,18 +102,41 @@ namespace pcl
100102
computeModel (int debug_verbosity_level = 0) override;
101103

102104
/** \brief Set the percentage of points to pre-test.
105+
* This is an alternative to setNrSamplesPretest.
103106
* \param[in] nr_pretest percentage of points to pre-test
104107
*/
105108
inline void
106-
setFractionNrPretest (double nr_pretest) { fraction_nr_pretest_ = nr_pretest; }
109+
setFractionNrPretest (double nr_pretest)
110+
{
111+
fraction_nr_pretest_ = nr_pretest;
112+
nr_samples_pretest_ = 0;
113+
}
107114

108115
/** \brief Get the percentage of points to pre-test. */
109116
inline double
110117
getFractionNrPretest () const { return (fraction_nr_pretest_); }
111118

119+
/** \brief Set the absolute number of points to pre-test.
120+
* This is an alternative to setFractionNrPretest.
121+
* \param[in] nr_pretest absolute number of points to pre-test
122+
*/
123+
inline void
124+
setNrSamplesPretest (std::size_t nr_pretest)
125+
{
126+
nr_samples_pretest_ = nr_pretest;
127+
fraction_nr_pretest_ = -1.0;
128+
}
129+
130+
/** \brief Get the absolute number of points to pre-test. */
131+
inline std::size_t
132+
getNrSamplesPretest () const { return (nr_samples_pretest_); }
133+
112134
private:
113-
/** \brief Number of samples to randomly pre-test, in percents. */
135+
/** \brief Number of samples to randomly pre-test, in percents. This is an alternative and mutually exclusive to nr_samples_pretest_. */
114136
double fraction_nr_pretest_;
137+
138+
/** \brief Absolute number of samples to randomly pre-test. This is an alternative and mutually exclusive to fraction_nr_pretest_. */
139+
std::size_t nr_samples_pretest_;
115140
};
116141
}
117142

sample_consensus/include/pcl/sample_consensus/rransac.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ namespace pcl
7979
*/
8080
RandomizedRandomSampleConsensus (const SampleConsensusModelPtr &model)
8181
: SampleConsensus<PointT> (model)
82-
, fraction_nr_pretest_ (10.0) // Number of samples to try randomly in percents
82+
, fraction_nr_pretest_ (-1.0)
83+
, nr_samples_pretest_ (1)
8384
{
8485
// Maximum number of trials before we give up.
8586
max_iterations_ = 10000;
@@ -91,7 +92,8 @@ namespace pcl
9192
*/
9293
RandomizedRandomSampleConsensus (const SampleConsensusModelPtr &model, double threshold)
9394
: SampleConsensus<PointT> (model, threshold)
94-
, fraction_nr_pretest_ (10.0) // Number of samples to try randomly in percents
95+
, fraction_nr_pretest_ (-1.0)
96+
, nr_samples_pretest_ (1)
9597
{
9698
// Maximum number of trials before we give up.
9799
max_iterations_ = 10000;
@@ -104,18 +106,41 @@ namespace pcl
104106
computeModel (int debug_verbosity_level = 0) override;
105107

106108
/** \brief Set the percentage of points to pre-test.
109+
* This is an alternative to setNrSamplesPretest.
107110
* \param[in] nr_pretest percentage of points to pre-test
108111
*/
109112
inline void
110-
setFractionNrPretest (double nr_pretest) { fraction_nr_pretest_ = nr_pretest; }
113+
setFractionNrPretest (double nr_pretest)
114+
{
115+
fraction_nr_pretest_ = nr_pretest;
116+
nr_samples_pretest_ = 0;
117+
}
111118

112119
/** \brief Get the percentage of points to pre-test. */
113120
inline double
114121
getFractionNrPretest () const { return (fraction_nr_pretest_); }
115122

123+
/** \brief Set the absolute number of points to pre-test.
124+
* This is an alternative to setFractionNrPretest.
125+
* \param[in] nr_pretest absolute number of points to pre-test
126+
*/
127+
inline void
128+
setNrSamplesPretest (std::size_t nr_pretest)
129+
{
130+
nr_samples_pretest_ = nr_pretest;
131+
fraction_nr_pretest_ = -1.0;
132+
}
133+
134+
/** \brief Get the absolute number of points to pre-test. */
135+
inline std::size_t
136+
getNrSamplesPretest () const { return (nr_samples_pretest_); }
137+
116138
private:
117-
/** \brief Number of samples to randomly pre-test, in percents. */
139+
/** \brief Number of samples to randomly pre-test, in percents. This is an alternative and mutually exclusive to nr_samples_pretest_. */
118140
double fraction_nr_pretest_;
141+
142+
/** \brief Absolute number of samples to randomly pre-test. This is an alternative and mutually exclusive to fraction_nr_pretest_. */
143+
std::size_t nr_samples_pretest_;
119144
};
120145
}
121146

0 commit comments

Comments
 (0)