Go to the documentation of this file.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
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
00039 #define PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
00040
00041 #include <pcl/sample_consensus/ransac.h>
00042
00044 template <typename PointT> bool
00045 pcl::RandomSampleConsensus<PointT>::computeModel (int debug_verbosity_level)
00046 {
00047
00048 if (threshold_ == std::numeric_limits<double>::max())
00049 {
00050 PCL_ERROR ("[pcl::RandomSampleConsensus::computeModel] No threshold set!\n");
00051 return (false);
00052 }
00053
00054 iterations_ = 0;
00055 int n_best_inliers_count = -INT_MAX;
00056 double k = 1.0;
00057
00058 std::vector<int> selection;
00059 Eigen::VectorXf model_coefficients;
00060
00061 int n_inliers_count = 0;
00062 unsigned skipped_count = 0;
00063
00064 const unsigned max_skip = max_iterations_ * 10;
00065
00066
00067 while (iterations_ < k && skipped_count < max_skip)
00068 {
00069
00070 sac_model_->getSamples (iterations_, selection);
00071
00072 if (selection.empty ())
00073 {
00074 PCL_ERROR ("[pcl::RandomSampleConsensus::computeModel] No samples could be selected!\n");
00075 break;
00076 }
00077
00078
00079 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
00080 {
00081
00082 ++ skipped_count;
00083 continue;
00084 }
00085
00086
00087
00088
00089
00090
00091 n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
00092
00093
00094 if (n_inliers_count > n_best_inliers_count)
00095 {
00096 n_best_inliers_count = n_inliers_count;
00097
00098
00099 model_ = selection;
00100 model_coefficients_ = model_coefficients;
00101
00102
00103 double w = static_cast<double> (n_best_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
00104 double p_no_outliers = 1.0 - pow (w, static_cast<double> (selection.size ()));
00105 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
00106 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
00107 k = log (1.0 - probability_) / log (p_no_outliers);
00108 }
00109
00110 ++iterations_;
00111 if (debug_verbosity_level > 1)
00112 PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Trial %d out of %f: %d inliers (best is: %d so far).\n", iterations_, k, n_inliers_count, n_best_inliers_count);
00113 if (iterations_ > max_iterations_)
00114 {
00115 if (debug_verbosity_level > 0)
00116 PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
00117 break;
00118 }
00119 }
00120
00121 if (debug_verbosity_level > 0)
00122 PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_best_inliers_count);
00123
00124 if (model_.empty ())
00125 {
00126 inliers_.clear ();
00127 return (false);
00128 }
00129
00130
00131 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
00132 return (true);
00133 }
00134
00135 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomSampleConsensus<T>;
00136
00137 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
00138