/******************************************************************** ** Image Component Library (ICL) ** ** ** ** Copyright (C) 2006-2012 CITEC, University of Bielefeld ** ** Neuroinformatics Group ** ** Website: www.iclcv.org and ** ** http://opensource.cit-ec.de/projects/icl ** ** ** ** File : include/ICLUtils/RansacFitter.h ** ** Module : ICLUtils ** ** Authors: Christof Elbrechter ** ** ** ** ** ** Commercial License ** ** ICL can be used commercially, please refer to our website ** ** www.iclcv.org for more details. ** ** ** ** GNU General Public License Usage ** ** Alternatively, this file may be used under the terms of the ** ** GNU General Public License version 3.0 as published by the ** ** Free Software Foundation and appearing in the file LICENSE.GPL ** ** included in the packaging of this file. Please review the ** ** following information to ensure the GNU General Public License ** ** version 3.0 requirements will be met: ** ** http://www.gnu.org/copyleft/gpl.html. ** ** ** ** The development of this software was supported by the ** ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** ** Forschungsgemeinschaft (DFG) in the context of the German ** ** Excellence Initiative. ** ** ** *********************************************************************/ #ifndef ICL_RANSAC_FITTER_H #define ICL_RANSAC_FITTER_H #include #include #include #include #include namespace icl{ /// Generic RANSAC (RAndom SAmpling Consensus) Implementation /** The RansacFitter provides a generic framework, for RANSAC based model fitting. \section ALG RANSAC Algorithm The RANSAC Algorithm is well described on Wikipedia @see http://de.wikipedia.org/wiki/RANSAC-Algorithmus \section EX Example We will here show an example application that is used for fitting a simple 2D line: The example code is also available as an example application: ICL/ICLUtils/examples/ransac-test.cpp \code #include #include #include #include #include #include using namespace icl; // the line model (y=mx+b) is defined by two values typedef FixedColVector Line; // the fitting is done using standard least squares approach Line fit_line(const std::vector &pts){ int n = pts.size(); DynMatrix xs(n,2), ys(n,1); for(int i=0;i fit = ys * xs.pinv(true); return Line(fit[0],fit[1]); } // distance function for single points (y-distance here) double line_dist(const Line &line, const Point32f &p){ return sqr(line[0] + line[1]*p.x - p.y); } // the original line static const Line THE_LINE(1.23456, 7.89); // create test data: // 50% noisy point on the line // 50% random outliers const std::vector get_line_points(){ Line l = THE_LINE; std::vector pts(100); URand r(-100,100); GRand gr(0,1); for(int i=0;i<50;++i){ pts[i].x = r; pts[i].y = l[0] + l[1]* pts[i].x + gr; } for(int i=0;i<50;++i){ pts[i+50] = Point32f(r,r); } return pts; } int main(int n, char **ppc){ randomSeed(); // create the fitter RansacFitter fitLine(2,1000,fit_line,line_dist,1.5,30); // fit ... RansacFitter::Result r = fitLine.fit(get_line_points()); // show results std::cout << "original line was " << THE_LINE.transp() << std::endl; std::cout << "fitted result was " << r.model.transp() << std::endl; std::cout << "fitting error was " << r.error << std::endl; } // output could be something like: // original line was | 1.23456 7.89 | // fitted result was | 1.3565 7.88645 | // fitting error was 0.188892 \endcode \section TEM Template Parameters The two tempalte parameters are kept very general. Therefore, there are just a few restrictions for the DataPoint and Model classes. - default constructable - copyable */ template, class Model=std::vector > class RansacFitter{ public: /// DataSet type (just a set of DataPoint instances) typedef std::vector DataSet; /// Function for the fitting module (gets a dataset and returns the fitted model) typedef Function ModelFitting; /// Error function for single points typedef Function PointError; private: /// minimum points that are used to create a coarse model int m_minPointsForModel; /// number of iterations int m_iterations; /// maximum distance of a point to the model to become an inlier icl64f m_maxModelDistance; /// minimum amount of inliers for a 'good' model int m_minClosePointsForGoodModel; /// fitting function ModelFitting m_fitting; /// point-model error function PointError m_err; /// min error criterion for early exit icl64f m_minErrorExit; public: /// result structure struct Result{ /// reached error icl64f error; /// model (zero sized if no model was found) Model model; /// consensus set of best match (i.e. inliers) /** empty if no model was found */ DataSet consensusSet; /// number of iterations needed int iterationCount; /// returns whether any model was found bool found() const { return consensusSet.size(); } }; private: /// internal result buffer Result m_result; /// internal utility method static inline bool find_in(const std::vector &v, int i, int n){ return std::find(v.data(), v.data()+n, i) != v.data()+n; } /// internal utility method void find_random_consensus_set(DataSet &currConsensusSet, const DataSet &allPoints, std::vector &usedIndices){ const int n = currConsensusSet.size(); URandI r(allPoints.size()-1); for(int i=0;i consensusSet(m_minPointsForModel); std::vector usedIndices(m_minPointsForModel); int i = 0; for(i=0;i= m_minClosePointsForGoodModel){ model = m_fitting(consensusSet); double error = 0; for(unsigned int j=0;j