35 #ifndef OPENMS_MATH_MISC_RANSAC_H
36 #define OPENMS_MATH_MISC_RANSAC_H
38 #include <OpenMS/config.h>
61 template<
typename TModelType = RansacModelLinear>
83 static std::vector<std::pair<double, double> >
ransac(
84 const std::vector<std::pair<double, double> >& pairs,
89 int (*rng)(
int) = NULL)
95 if (pairs.size() <= n)
98 String(
"RANSAC: Number of pairs (") +
String(pairs.size()) +
") must be larger than number of initial points (n=" +
String(n) +
").");
101 std::vector< std::pair<double, double> > alsoinliers, betterdata, bestdata;
103 double besterror = std::numeric_limits<double>::max();
104 #ifdef DEBUG_MRMRTNORMALIZER
105 std::pair<double, double > bestcoeff;
106 double betterrsq = 0;
111 std::vector<std::pair<double, double> > pairs_shuffled = pairs;
113 for (
size_t ransac_int=0; ransac_int<
k; ransac_int++)
116 if (bestdata.size() == pairs.size())
break;
120 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end(), rng);
122 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end());
126 typename TModelType::ModelParameters coeff = model.rm_fit(pairs_shuffled.begin(), pairs_shuffled.begin()+n);
128 alsoinliers = model.rm_inliers(pairs_shuffled.begin()+n, pairs_shuffled.end(), coeff, t);
130 if (alsoinliers.size() > d)
133 std::copy( pairs_shuffled.begin(), pairs_shuffled.begin()+n, back_inserter(betterdata) );
134 betterdata.insert( betterdata.end(), alsoinliers.begin(), alsoinliers.end() );
135 typename TModelType::ModelParameters bettercoeff = model.rm_fit(betterdata.begin(), betterdata.end());
136 double bettererror = model.rm_rss(betterdata.begin(), betterdata.end(), bettercoeff);
137 #ifdef DEBUG_MRMRTNORMALIZER
138 betterrsq = model.rm_rsq(betterdata);
141 if (bettererror < besterror)
143 besterror = bettererror;
144 bestdata = betterdata;
145 #ifdef DEBUG_MRMRTNORMALIZER
146 bestcoeff = bettercoeff;
148 std::cout <<
"RANSAC " << ransac_int <<
": Points: " << betterdata.size() <<
" RSQ: " << bestrsq <<
" Error: " << besterror <<
" c0: " << bestcoeff.first <<
" c1: " << bestcoeff.second << std::endl;
154 #ifdef DEBUG_MRMRTNORMALIZER
155 std::cout <<
"=======STARTPOINTS=======" << std::endl;
156 for (std::vector<std::pair<double, double> >::iterator it = bestdata.begin(); it != bestdata.end(); ++it)
158 std::cout << it->first <<
"\t" << it->second << std::endl;
160 std::cout <<
"=======ENDPOINTS=======" << std::endl;
172 #endif // OPENMS_MATH_MISC_RANSAC_H
A more convenient string class.
Definition: String.h:57
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Precondition failed exception.
Definition: Exception.h:167
static std::vector< std::pair< double, double > > ransac(const std::vector< std::pair< double, double > > &pairs, size_t n, size_t k, double t, size_t d, int(*rng)(int)=NULL)
This function provides a generic implementation of the RANSAC outlier detection algorithm. Is implemented and tested after the SciPy reference: http://wiki.scipy.org/Cookbook/RANSAC.
Definition: RANSAC.h:83
This class provides a generic implementation of the RANSAC outlier detection algorithm. Is implemented and tested after the SciPy reference: http://wiki.scipy.org/Cookbook/RANSAC.
Definition: RANSAC.h:62