ransac(data, model_class, min_samples, residual_threshold, is_data_valid=None, is_model_valid=None, max_trials=100, stop_sample_num=inf, stop_residuals_sum=0, stop_probability=1, random_state=None, initial_inliers=None)
RANSAC is an iterative algorithm for the robust estimation of parameters from a subset of inliers from the complete data set. Each iteration performs the following tasks:
Select :None:None:`min_samples`
random samples from the original data and check whether the set of data is valid (see :None:None:`is_data_valid`
).
Estimate a model to the random subset (:None:None:`model_cls.estimate(*data[random_subset]`
) and check whether the estimated model is valid (see :None:None:`is_model_valid`
).
Classify all data as inliers or outliers by calculating the residuals to the estimated model (:None:None:`model_cls.residuals(*data)`
) - all data samples with residuals smaller than the :None:None:`residual_threshold`
are considered as inliers.
Save estimated model as best model if number of inlier samples is maximal. In case the current estimated model has the same number of inliers, it is only considered as the best model if it has less sum of residuals.
These steps are performed either a maximum number of times or until one of the special stop criteria are met. The final model is estimated using all inlier samples of the previously determined best model.
Data set to which the model is fitted, where N is the number of data points and the remaining dimension are depending on model requirements. If the model class requires multiple input data arrays (e.g. source and destination coordinates of skimage.transform.AffineTransform
), they can be optionally passed as tuple or list. Note, that in this case the functions estimate(*data)
, residuals(*data)
, is_model_valid(model, *random_data)
and is_data_valid(*random_data)
must all take each data array as separate arguments.
Object with the following object methods:
success = estimate(*data)
residuals(*data)
where :None:None:`success`
indicates whether the model estimation succeeded (:None:None:`True`
or :None:None:`None`
for success, :None:None:`False`
for failure).
The minimum number of data points to fit a model to.
Maximum distance for a data point to be classified as an inlier.
This function is called with the randomly selected data before the model is fitted to it: :None:None:`is_data_valid(*random_data)`
.
This function is called with the estimated model and the randomly selected data: :None:None:`is_model_valid(model, *random_data)`
, .
Maximum number of iterations for random sample selection.
Stop iteration if at least this number of inliers are found.
Stop iteration if sum of residuals is less than or equal to this threshold.
RANSAC iteration stops if at least one outlier-free set of the training data is sampled with probability >= stop_probability
, depending on the current best model's inlier ratio and the number of trials. This requires to generate at least N samples (trials):
N >= log(1 - probability) / log(1 - e**m)
where the probability (confidence) is typically set to a high value such as 0.99, e is the current fraction of inliers w.r.t. the total number of samples, and m is the min_samples value.
If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by :None:None:`np.random`
.
Initial samples selection for model estimation
Best model with largest consensus set.
Boolean mask of inliers classified as True
.
Fit a model to data with the RANSAC (random sample consensus) algorithm.
Generate ellipse data without tilt and add noise:
This example is valid syntax, but we were not able to check execution>>> t = np.linspace(0, 2 * np.pi, 50)
... xc, yc = 20, 30
... a, b = 5, 10
... x = xc + a * np.cos(t)
... y = yc + b * np.sin(t)
... data = np.column_stack([x, y])
... np.random.seed(seed=1234)
... data += np.random.normal(size=data.shape)
Add some faulty data:
This example is valid syntax, but we were not able to check execution>>> data[0] = (100, 100)
... data[1] = (110, 120)
... data[2] = (120, 130)
... data[3] = (140, 130)
Estimate ellipse model using all available data:
This example is valid syntax, but we were not able to check execution>>> model = EllipseModel()This example is valid syntax, but we were not able to check execution
... model.estimate(data) True
>>> np.round(model.params) # doctest: +SKIP array([ 72., 75., 77., 14., 1.])
Estimate ellipse model using RANSAC:
This example is valid syntax, but we were not able to check execution>>> ransac_model, inliers = ransac(data, EllipseModel, 20, 3, max_trials=50)This example is valid syntax, but we were not able to check execution
... abs(np.round(ransac_model.params)) array([20., 30., 5., 10., 0.])
>>> inliers # doctest: +SKIP array([False, False, False, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True], dtype=bool)This example is valid syntax, but we were not able to check execution
>>> sum(inliers) > 40 True
RANSAC can be used to robustly estimate a geometric transformation. In this section, we also show how to use a proportion of the total samples, rather than an absolute number.
This example is valid syntax, but we were not able to check execution>>> from skimage.transform import SimilarityTransformSee :
... np.random.seed(0)
... src = 100 * np.random.rand(50, 2)
... model0 = SimilarityTransform(scale=0.5, rotation=1, translation=(10, 20))
... dst = model0(src)
... dst[0] = (10000, 10000)
... dst[1] = (-100, 100)
... dst[2] = (50, 50)
... ratio = 0.5 # use half of the samples
... min_samples = int(ratio * len(src))
... model, inliers = ransac((src, dst), SimilarityTransform, min_samples, 10,
... initial_inliers=np.ones(len(src), dtype=bool))
... inliers array([False, False, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True])
The following pages refer to to this document either explicitly or contain code examples using this.
skimage.measure.fit.ransac
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them