![]() |
ImFusion SDK 4.3
|
#include <ImFusion/Base/RANSAC.h>
This class implements a more generalized form of "Random Sample Consensus" algorithm. More...
This class implements a more generalized form of "Random Sample Consensus" algorithm.
It estimates parameters of a mathematical model from a set of observed data which contains outliers. Differences to regular RANSAC: Agreement of individual data points with the model is a continuous variable instead of boolean, allows assigning different importance to data points. Quality of a parameter set is measured by the sum of the agreement to individual points. This parameter quality measurement can be performed on a subset of the dataset to save computation time. Overall this version is more fuzzy/gray than the regular in-or-out RANSAC. For traditional RANSAC have the model return either 1.0 or 0.0 for the agreement of a data point to the model and set the size of the validation set to the entire dataset.
Dataset | has .size() |
Hypothesis |
Classes | |
struct | Result |
Public Types | |
typedef std::function< bool(const Dataset &dataset, const std::vector< size_t > &indices, Hypothesis &hypothesisOut)> | FitFunction |
Function that fits an hypothesis to a subset of the data. | |
typedef std::function< double(const Dataset &dataset, size_t index, const Hypothesis &hypothesis, double eps)> | ScoreFunction |
A function that scores an hypothesis on a data point. | |
typedef std::function< Hypothesis(const Dataset &dataset, const Result &res, double eps)> | RefineFunction |
A function that refines an hypothesis using the whole dataset. | |
Public Member Functions | |
RANSAC (const Dataset &dataset, const FitFunction &fit, const ScoreFunction &score) | |
bool | compute (int sampleSize, int keepBest=1, const std::vector< double > &weights={}) |
Computes the RANSAC estimation. | |
void | refine (double threshold=0.5) |
Refines the current results over the whole dataset, scores and sort them again. | |
void | refine (const RefineFunction &computeRefinement) |
Refines the current results over the whole dataset, scores and sort them again. | |
std::vector< Hypothesis > | hypotheses () const |
Vector of all the kept hypotheses ordered from best to worse. | |
Hypothesis | best () const |
Best hypothesis computed by the algorithm. | |
std::vector< bool > | inliers (double threshold=0.5, int which=0) |
Computes a vector of booleans where the i-th element is true if the i-th element of the dataset is an inlier. | |
size_t | numInliers (double threshold=0.5, int which=0) |
Returns the number of inliers. | |
std::vector< Result > | results () const |
Vector of results ordered from best to worse. Each result contains an hypothesis, the score for each tested item and the total score. | |
void | setMaxIterations (size_t iterations) |
Sets the maximum number of iterations (default: 100). | |
void | setRequiredMatchingQuality (double q) |
Sets the required quality of the matching between parameters and testing set for an early acceptance (default: number of data points). | |
void | setRandomSeed (uint32_t seed) |
Sets the seed that is used to initialize the random generator (default: 0). | |
void | setEps (double eps) |
Set threshold for inliers (default: 0.1) | |
void | setStopValidationEarly (bool v) |
If set to true, the algorithm will stop accumulating scores for the validation data as soon as it's clear that the total score will not be greater than the result with the worst score so far. | |
bool | stopValidationEarly () const |
void | setValidationSetSize (size_t size) |
Controls how many of the data items are used to evaluate parameter quality (default: all available). | |
Hypothesis | fitEntireDataset () const |
Runs fitFunction on the entire dataset. | |
Protected Member Functions | |
void | computeScores (int first=0, int last=-1) |
Recomputes validation scores on all results that where not validated on the entire dataset. | |
Protected Attributes | |
std::vector< Result > | m_results |
const Dataset * | m_dataset |
FitFunction | m_fit |
ScoreFunction | m_score |
size_t | m_validationSetSize = std::numeric_limits<size_t>::max() |
double | m_eps = 0.1 |
size_t | m_maxIterations = 100 |
uint32_t | m_seed = 42 |
double | m_requiredMatchingQuality = -1 |
bool | m_stopValidationEarly = true |
std::unique_ptr< Multinomial > | m_multinomial |
typedef std::function<bool(const Dataset& dataset, const std::vector<size_t>& indices, Hypothesis& hypothesisOut)> FitFunction |
Function that fits an hypothesis to a subset of the data.
dataset | reference to the entire dataset |
indices | vector containing the indices of the elements that should be used to fit the hypothesis |
hypothesisOut | reference that should be set to the fitted hypothesis |
typedef std::function<double(const Dataset& dataset, size_t index, const Hypothesis& hypothesis, double eps)> ScoreFunction |
A function that scores an hypothesis on a data point.
dataset | reference to the entire dataset |
index | the index of the data point that should be used for scoring |
hypothesis | the hypothesis that is being scored |
eps | the current value of the parameter eps of RANSAC |
|
inline |
dataset | |
fit | A function that fits a hypothesis to a subset of the data |
score | A function that scores a hypothesis on a data point |
bool compute | ( | int | sampleSize, |
int | keepBest = 1, | ||
const std::vector< double > & | weights = {} ) |
Computes the RANSAC estimation.
sampleSize | Number of datapoints to sample at each iteration to fit an hypothesis |
keepBest | Maximum number of best hypotheses that should be kept |
weights | Optional weights for each element in the dataset |
void refine | ( | double | threshold = 0.5 | ) |
Refines the current results over the whole dataset, scores and sort them again.
By default calls FitFunction
with indices of all the inliers
threshold | An element of the dataset is considered an inlier if its score is > threshold |
void refine | ( | const RefineFunction & | computeRefinement | ) |
Refines the current results over the whole dataset, scores and sort them again.
computeRefinement | function that takes the dataset, the result to be refined and eps and returns the refined hypothesis |
std::vector< bool > inliers | ( | double | threshold = 0.5, |
int | which = 0 ) |
Computes a vector of booleans where the i-th element is true if the i-th element of the dataset is an inlier.
threshold | An element of the dataset is considered an inlier if its score is > threshold |
which | Specifies which hypothesis to use (0 = best) |
size_t numInliers | ( | double | threshold = 0.5, |
int | which = 0 ) |
Returns the number of inliers.
threshold | An element of the dataset is considered an inlier if its score is > threshold |
which | Specifies which hypothesis to use (0 = best) |
|
inline |
Sets the required quality of the matching between parameters and testing set for an early acceptance (default: number of data points).
Once this quality is reached, the iteration is aborted.
|
inline |
Sets the seed that is used to initialize the random generator (default: 0).
This can be used to reproduce results.
|
inline |
If set to true, the algorithm will stop accumulating scores for the validation data as soon as it's clear that the total score will not be greater than the result with the worst score so far.
Might speed up the computation. Warning: only works as expected if the score per sample is in [0,1] range. Should be set to false otherwise.
|
inline |
Controls how many of the data items are used to evaluate parameter quality (default: all available).
Can be used to restrict the computational load when comparing large datasets to fitted parameters. Setting the validation set size to 0 will reset it to use all available data.