13

TL;DR : Is there a C++ implementation of RANSAC or other robust correspondence algorithms that is freely usable with arbitrary 2D point sets?

I know that many implementations exist that include or make use of correspondence algorithms such as RANSAC (Random Sampling Consensus). They are often used in computer vision applications and found in libraries such as OpenCV, PCL, etc. The general algorithm is well known and various site lists the different steps.

Now, all the "advanced" implementations (done for OpenCV, PCL, etc.) I have found are for specific types of problem with an underlying set of assumptions. In OpenCV, you want to find the homography matrix between a first image and a portion of a second image (this example). In PCL, you are in the realm of 3D point clouds and you are (to my knowledge) only able to match specific, already defined shapes (a line, a sphere, etc.).

What I "simply" want to do is to take an arbitrary 2D set of points (which may contain some noise) and find a correspondence in a bigger set of 2D points (which contain some noise and other points too). It has to require no specific model training other than inputting the two sets of points. I am in the process of implementing it myself in C++, but:

  • I am by no mean an experienced programmer and I need the whole thing to executed quite fast; previous implementation done by myself of well known algorithms (edge detection, Gaussian blurring, etc.) have proven to be significantly slower (>10x) than proven implementation.

  • Simply ripping off an already existing open source implementation (such as OpenCV's) have proven to be beyond my current capabilities (too much dependencies and virtual implementation-template and else...)

So, if anyone knows of a freely usable (BSD like) and proven C++ implementation that I have missed...

Community
  • 1
  • 1
Doombot
  • 553
  • 3
  • 8
  • 18
  • do you want to achieve sth. similar like this but with RANSAC? http://stackoverflow.com/questions/20642641/opencv-templates-in-2d-point-data-set/20975486#20975486 – Micka Apr 21 '15 at 19:32
  • 1
    Well yes! Actually, the specific method isn't that important, but I am in a similar situation to the OP of this question. Thanks, that will be something to start with! :) – Doombot Apr 21 '15 at 19:37

4 Answers4

21

It's surprisingly hard to find a popular, lightweight, generic C++ implementation of RANSAC. I just released my generic RANSAC implementation under the MIT license.

https://github.com/drsrinathsridhar/GRANSAC

GRANSAC is generic, templated, header-only, and multithreaded. The user has to implement a class that inherits the AbstractModel. RANSAC estimation can then be done for any kind of model (eg.: 2D lines, 3D planes).

I have tested this only for 2D line fitting but should work for other problems too. Would be happy to add more features (such as automatically choosing number of iterations, etc.)

enter image description here

Community
  • 1
  • 1
Srinath Sridhar
  • 972
  • 1
  • 10
  • 15
  • Not sure what you mean by two arbitrary point sets. Do you mean to ask if it would fit two separate models for the two point sets? – Srinath Sridhar Sep 09 '15 at 10:33
  • Well, I mean that instead of fitting an arbitrary line to a point sets, I would like to fit a first point set A to a second point set B. Of course, there would be outliers in both sets, but it is the point of RANSAC of rejecting outliers. Is that precise enough? – Doombot Sep 09 '15 at 18:06
  • 1
    RANSAC is an estimation method used when you know what the mathematical model of your observations are. In the above example, we know that the points roughly form a line. So unless you have a parametric representation of the model you cannot use RANSAC. The problem you describe seems to be more of a matching problem best solved by optimization or perhaps Procrustes: https://en.wikipedia.org/wiki/Procrustes_analysis – Srinath Sridhar Sep 10 '15 at 11:26
  • Ah, thanks for the Procrustes terminology, didn't know that. Actually, RANSAC-like algorithms are used the way I described it in Computer Vision, such as in the section 3.2 of this paper https://vision.ece.ucsb.edu/sites/vision.ece.ucsb.edu/files/publications/05ICIPMarco.pdf . It is often used as an intermediary step in the process of computing an homography. – Doombot Sep 10 '15 at 14:55
  • Sure, estimating homographies is the most common RANSAC application and my implementation can be used for this. In this case, a mathematical model is already defined (homography parametrized by 4 points). The output would be a "best" homography and point correspondences that are inliers to this homography. But this is not really aligning point sets, is it? – Srinath Sridhar Sep 10 '15 at 15:37
  • 1
    Adding to the above comment. I realize that alignment/fitting usually means finding a rigid transform between the point sets. A homography is a projective transform (a superset containing rigid transforms). So you could do the same but with a rigid transform matrix. But remember that RANSAC only gives a model that has most of the input as inliers (aka removes outliers). You would have to run something on top of the inliers to get your final model. This could be a least squares fit for example or Procrustes (which IIRC is equivalent to the least squares solution for point set matching). – Srinath Sridhar Sep 10 '15 at 15:50
  • Thanks for the precision :) – Doombot Sep 10 '15 at 17:57
  • this post should take more attention – Humam Helfawi Nov 12 '15 at 09:17
3

A good-looking RANSAC, LMedS, MSAC, MLESAC C++ implementation for Windows and Linux is here: https://github.com/sunglok/rtl.

RTL: RANSAC Template Library RANSAC Template Library (RTL) is an open-source robust regression tool especially with RANSAC family. RTL aims to provide fast, accurate, and easy ways to estimate any model parameters with data contaminated with outliers (incorrect data). RTL includes recent RANSAC variants with their performance evaluation with several models with synthetic and real data. RTL is written in generic programming style (template in C++) for its further applications with user-defined models. RTL is distributed under Simplified BSD License.

The basic class is RANSAC:

template <class Model, class Datum, class Data>
class RANSAC;

Other classes are inherited from it:

template <class Model, class Datum, class Data>
class MLESAC : virtual public RANSAC<Model, Datum, Data>
...

The usage is simple (an example from README):

// Find the best model using RANSAC
LineEstimator estimator;
RTL::RANSAC<Line, Point, std::vector<Point> > ransac(&estimator);
Line model;
double loss = ransac.FindBest(model, data, data.size(), 2);

// Determine inliers using the best model if necessary
std::vector<int> inliers = ransac.FindInliers(model, data, data.size());

enter image description here

The paper: https://sites.google.com/site/sunglok/files/Choi09_bmvc.pdf?attredirects=0

trig-ger
  • 1,195
  • 1
  • 11
  • 18
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – geisterfurz007 Feb 21 '18 at 09:45
  • Hello @trig-ger, I am looking for python implementation of MSAC, MLESAC, can't find them. Do you know any? Thanks. – CognitiveRobot Oct 21 '20 at 10:40
1

I was looking for something like that and then I found this.

The code is in c++ at bottom part.

The function below was originaly extracted from this class.

cv::Mat ransacTest(const std::vector<cv::DMatch>& matches, const std::vector<cv::KeyPoint>& keypoints1,const std::vector<cv::KeyPoint>& keypoints2, std::vector<cv::DMatch>& outMatches) {

   // Convert keypoints into Point2f
   std::vector<cv::Point2f> points1, points2;
   cv::Mat fundemental;

   for (std::vector<cv::DMatch>::const_iterator it= matches.begin(); it!= matches.end(); ++it) {
       // Get the position of left keypoints
       float x= keypoints1[it->queryIdx].pt.x;
       float y= keypoints1[it->queryIdx].pt.y;
       points1.push_back(cv::Point2f(x,y));
       // Get the position of right keypoints
       x= keypoints2[it->trainIdx].pt.x;
       y= keypoints2[it->trainIdx].pt.y;
       points2.push_back(cv::Point2f(x,y));
   }

   // Compute F matrix using RANSAC
   std::vector<uchar> inliers(points1.size(),0);

   if ( points1.size() > 0 && points2.size() > 0 ){

      cv::Mat fundemental= cv::findFundamentalMat(
            cv::Mat(points1),cv::Mat(points2), // matching points
            inliers,       // match status (inlier or outlier)
            CV_FM_RANSAC,  // RANSAC method
            3.0,           // distance to epipolar line
            0.99);         // confidence probability

      // extract the surviving (inliers) matches
      std::vector<uchar>::const_iterator itIn= inliers.begin();
      std::vector<cv::DMatch>::const_iterator itM= matches.begin();

      // for all matches
      for ( ;itIn!= inliers.end(); ++itIn, ++itM) {
         if (*itIn) { // it is a valid match
            outMatches.push_back(*itM);
         }
      }

      // The F matrix will be recomputed with all accepted matches
      // Convert keypoints into Point2f for final F computation

      points1.clear();
      points2.clear();

      for (std::vector<cv::DMatch>::const_iterator it= outMatches.begin(); it!=outMatches.end(); ++it) {
        // Get the position of left keypoints
        float x= keypoints1[it->queryIdx].pt.x;
        float y= keypoints1[it->queryIdx].pt.y;
        points1.push_back(cv::Point2f(x,y));
        // Get the position of right keypoints
        x= keypoints2[it->trainIdx].pt.x;
        y= keypoints2[it->trainIdx].pt.y;
        points2.push_back(cv::Point2f(x,y));
     }

     // Compute 8-point F from all accepted matches
     if( points1.size() > 0 && points2.size() > 0){
        fundemental= cv::findFundamentalMat(
        cv::Mat(points1),cv::Mat(points2), // matches
        CV_FM_8POINT); // 8-point method
     }

   }

   return fundemental;

}
Community
  • 1
  • 1
0

Might be too late, but I have found the RANSAC implementation from Theiasfm to be very efficient, well written and easy to implement. The author of the library documents how to also include your own estimator module in the documentation. It might be an overkill to get the whole library for such a small requirement, but I am posting this here since it works super well. One thing is that it uses Eigen v3.3.7 and Ceres v1.14. To get a feel of how it works, you could check the unit tests that the author has for RANSAC. Moreover, it implements several version of RANSAC for example: USAC, PROSAC etc...

AlAhm
  • 11
  • 4