00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00033 #include <limits>
00034 #include <door_handle_detector/sample_consensus/rransac.h>
00035
00036 namespace sample_consensus
00037 {
00039
00043 RRANSAC::RRANSAC (SACModel *model, double threshold) : SAC (model)
00044 {
00045 this->threshold_ = threshold;
00046
00047 this->probability_ = 0.99;
00048
00049 this->max_iterations_ = 10000;
00050
00051 this->iterations_ = 0;
00052
00053
00054 fraction_nr_pretest_ = 10;
00055 }
00056
00058
00061 RRANSAC::RRANSAC (SACModel* model) : SAC (model) { }
00062
00064
00067 bool
00068 RRANSAC::computeModel (int debug)
00069 {
00070 iterations_ = 0;
00071 int n_best_inliers_count = -INT_MAX;
00072 double k = 1.0;
00073
00074 std::vector<int> best_model;
00075 std::vector<int> best_inliers, inliers;
00076 std::vector<int> selection;
00077
00078 int n_inliers_count = 0;
00079
00080
00081 int fraction_nr_points = lrint (sac_model_->getIndices ()->size () * fraction_nr_pretest_ / 100.0);
00082
00083
00084 while (iterations_ < k)
00085 {
00086
00087 sac_model_->getSamples (iterations_, selection);
00088
00089 if (selection.size () == 0) break;
00090
00091
00092 sac_model_->computeModelCoefficients (selection);
00093
00094
00095
00096 std::set<int> fraction_idx = getRandomSamples (*sac_model_->getCloud (), *sac_model_->getIndices (), fraction_nr_points);
00097
00098 if (!sac_model_->doSamplesVerifyModel (fraction_idx, threshold_))
00099 {
00100
00101 if (k != 1.0)
00102 {
00103 iterations_ += 1;
00104 continue;
00105 }
00106 }
00107
00108 sac_model_->selectWithinDistance (sac_model_->getModelCoefficients (), threshold_, inliers);
00109 n_inliers_count = inliers.size ();
00110
00111
00112 if (n_inliers_count > n_best_inliers_count)
00113 {
00114 n_best_inliers_count = n_inliers_count;
00115 best_inliers = inliers;
00116
00117 best_model = selection;
00118
00119
00120 double w = (double)((double)n_inliers_count / (double)sac_model_->getIndices ()->size ());
00121 double p_no_outliers = 1 - pow (w, (double)selection.size ());
00122 p_no_outliers = std::max (std::numeric_limits<double>::epsilon (), p_no_outliers);
00123 p_no_outliers = std::min (1 - std::numeric_limits<double>::epsilon (), p_no_outliers);
00124 k = log (1 - probability_) / log (p_no_outliers);
00125 }
00126
00127 iterations_ += 1;
00128 if (debug > 1)
00129 std::cerr << "[RRANSAC::computeModel] Trial " << iterations_ << " out of " << ceil (k) << ": " << n_inliers_count << " inliers (best is: " << n_best_inliers_count << " so far)." << std::endl;
00130 if (iterations_ > max_iterations_)
00131 {
00132 if (debug > 0)
00133 std::cerr << "[RRANSAC::computeModel] RANSAC reached the maximum number of trials." << std::endl;
00134 break;
00135 }
00136 }
00137
00138 if (best_model.size () != 0)
00139 {
00140 if (debug > 0)
00141 std::cerr << "[RRANSAC::computeModel] Model found: " << n_best_inliers_count << " inliers." << std::endl;
00142 sac_model_->setBestModel (best_model);
00143 sac_model_->setBestInliers (best_inliers);
00144 return (true);
00145 }
00146 else
00147 if (debug > 0)
00148 std::cerr << "[RRANSAC::computeModel] Unable to find a solution!" << std::endl;
00149 return (false);
00150 }
00151
00153
00156 void
00157 RRANSAC::setFractionNrPretest (int nr_pretest)
00158 {
00159 fraction_nr_pretest_ = nr_pretest;
00160 }
00161
00162 }