Berkeley SfM
Public Member Functions | Private Attributes | List of all members
bsfm::Ransac< DataType, ModelType > Class Template Reference

#include <ransac.h>

Public Member Functions

 Ransac ()
 
 ~Ransac ()
 
void SetOptions (const RansacOptions &options)
 
bool Run (RansacProblem< DataType, ModelType > &problem) const
 

Private Attributes

RansacOptions options_
 

Detailed Description

template<typename DataType, typename ModelType>
class bsfm::Ransac< DataType, ModelType >

Definition at line 58 of file ransac.h.

Constructor & Destructor Documentation

template<typename DataType , typename ModelType >
bsfm::Ransac< DataType, ModelType >::Ransac ( )
inline

Definition at line 60 of file ransac.h.

60 { }
template<typename DataType , typename ModelType >
bsfm::Ransac< DataType, ModelType >::~Ransac ( )
inline

Definition at line 61 of file ransac.h.

61 { }

Member Function Documentation

template<typename DataType , typename ModelType >
bool bsfm::Ransac< DataType, ModelType >::Run ( RansacProblem< DataType, ModelType > &  problem) const
inline

Definition at line 83 of file ransac.h.

84  {
85  // By default, a valid model has not been found.
86  problem.SetSolutionFound(false);
87 
88  // Set the initial error to something very large.
89  double best_error = std::numeric_limits<double>::infinity();
90 
91  // Proceed for options_.iterations iterations of RANSAC.
92  for (unsigned int iter = 0; iter < options_.iterations; ++iter) {
93  // Sample data points.
94  std::vector<DataType> sampled = problem.SampleData(options_.num_samples);
95 
96  // Fit a model to the sampled data points.
97  ModelType initial_model = problem.FitModel(sampled);
98 
99  // Which of the remaining points are also inliers under this model?
100  std::vector<DataType> unsampled = problem.RemainingData(options_.num_samples);
101  std::vector<DataType> inliers(sampled);
102  for (const auto& not_sampled_data_point : unsampled) {
103  if (initial_model.IsGoodFit(not_sampled_data_point,
105  inliers.push_back(not_sampled_data_point);
106  }
107  }
108 
109  // Check if we have enough inliers to consider this a good model.
110  if (inliers.size() >= options_.minimum_num_inliers) {
111 
112  // Test how good this model is.
113  ModelType better_model = problem.FitModel(inliers);
114 
115  // Is this the best model yet?
116  double this_error = better_model.Error();
117  if (this_error < best_error) {
118  best_error = this_error;
119  problem.SetModel(better_model);
120  problem.SetInliers(inliers);
121  problem.SetSolutionFound(true);
122  }
123  }
124  }
125 
126  // See if RANSAC found a solution.
127  if (!problem.SolutionFound()) {
128  VLOG(1) << "RANSAC failed to find a solution in " << options_.iterations
129  << " iterations.";
130  return false;
131  }
132 
133  return true;
134 }
unsigned int minimum_num_inliers
unsigned int iterations
unsigned int num_samples
RansacOptions options_
Definition: ransac.h:72
template<typename DataType , typename ModelType >
void bsfm::Ransac< DataType, ModelType >::SetOptions ( const RansacOptions options)
inline

Definition at line 78 of file ransac.h.

78  {
79  options_ = options;
80 }
RansacOptions options_
Definition: ransac.h:72

Member Data Documentation

template<typename DataType , typename ModelType >
RansacOptions bsfm::Ransac< DataType, ModelType >::options_
private

Definition at line 72 of file ransac.h.


The documentation for this class was generated from the following file: