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 <ransac.h>
00035
00036 namespace sample_consensus
00037 {
00039
00043 RANSAC::RANSAC (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
00055
00058 RANSAC::RANSAC (SACModel* model) : SAC (model) { }
00059
00061
00064 bool
00065 RANSAC::computeModel (int debug)
00066 {
00067 iterations_ = 0;
00068 int n_best_inliers_count = -INT_MAX;
00069 double k = 1.0;
00070
00071 std::vector<int> best_model;
00072 std::vector<int> best_inliers, inliers;
00073 std::vector<int> selection;
00074
00075 int n_inliers_count = 0;
00076
00077
00078 while (iterations_ < k)
00079 {
00080
00081 sac_model_->getSamples (iterations_, selection);
00082
00083 if (selection.size () == 0) break;
00084
00085
00086 sac_model_->computeModelCoefficients (selection);
00087
00088 sac_model_->selectWithinDistance (sac_model_->getModelCoefficients (), threshold_, inliers);
00089 n_inliers_count = inliers.size ();
00090
00091
00092 if (n_inliers_count > n_best_inliers_count)
00093 {
00094 n_best_inliers_count = n_inliers_count;
00095 best_inliers = inliers;
00096
00097 best_model = selection;
00098
00099
00100 double w = (double)((double)n_inliers_count / (double)sac_model_->getIndices ()->size ());
00101 double p_no_outliers = 1 - pow (w, (double)selection.size ());
00102 p_no_outliers = std::max (std::numeric_limits<double>::epsilon (), p_no_outliers);
00103 p_no_outliers = std::min (1 - std::numeric_limits<double>::epsilon (), p_no_outliers);
00104 k = log (1 - probability_) / log (p_no_outliers);
00105 }
00106
00107 iterations_ += 1;
00108 if (debug > 1)
00109 std::cerr << "[RANSAC::computeModel] Trial " << iterations_ << " out of " << ceil (k) << ": " << n_inliers_count << " inliers (best is: " << n_best_inliers_count << " so far)." << std::endl;
00110 if (iterations_ > max_iterations_)
00111 {
00112 if (debug > 0)
00113 std::cerr << "[RANSAC::computeModel] RANSAC reached the maximum number of trials." << std::endl;
00114 break;
00115 }
00116 }
00117
00118 if (best_model.size () != 0)
00119 {
00120 if (debug > 0)
00121 std::cerr << "[RANSAC::computeModel] Model found: " << n_best_inliers_count << " inliers." << std::endl;
00122 sac_model_->setBestModel (best_model);
00123 sac_model_->setBestInliers (best_inliers);
00124 return (true);
00125 }
00126 else
00127 if (debug > 0)
00128 std::cerr << "[RANSAC::computeModel] Unable to find a solution!" << std::endl;
00129 return (false);
00130 }
00131 }