bundles / skimage 0.26.1rc0.dev0+git20260530.b607368ff / skimage / measure / fit / ransac
function
skimage.measure.fit:ransac
source: /dev/scikit-image/src/skimage/measure/fit.py :1183
Signature
def 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 , rng = None , initial_inliers = None , model_kwargs = None ) Summary
Fit a model to data with the RANSAC (random sample consensus) algorithm.
Extended Summary
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
min_samplesrandom samples from the original data and check whether the set of data is valid (seeis_data_valid).Estimate a model to the random subset (
model_cls.from_estimate(*data[random_subset]) and check whether the estimated model is valid (seeis_model_valid).Classify all data as inliers or outliers by calculating the residuals to the estimated model (
model_cls.residuals(*data)) - all data samples with residuals smaller than theresidual_thresholdare 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.
Parameters
data: list or tuple or array of shape (N,)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 functionsestimate(*data),residuals(*data),is_model_valid(model, *random_data)andis_data_valid(*random_data)must all take each data array as separate arguments.model_class: typeClass with the following methods:
Either:
from_estimateclass method returning transform instance, as intform = model_class.from_estimate(*data, **kwargs); the resultingtformshould be truthy (bool(tform) == True) where estimation succeeded, or falsey (bool(tform) == False) where it failed; OR(deprecated)
estimateinstance method, returning flag to indicate successful estimation, as intform = model_class(); success = tform.estimate(*data).success == Truewhen estimation succeeded,success == Falsewhen it failed.
residuals(*data)
Your model should conform to the
RansacModelProtocol— meaning implement all of the methods / attributes specified by the :class:RansacModelProctocol. An easy check to see whether that is the case is to useisinstance(MyModel, RansacModelProtocol). See https://docs.python.org/3/library/typing.html#typing.Protocol for more details.min_samples: int, in range (0, N)The minimum number of data points to fit a model to.
residual_threshold: float, >0Maximum distance for a data point to be classified as an inlier.
is_data_valid: Callable, optionalThis function is called with the randomly selected data before the model is fitted to it:
is_data_valid(*random_data).is_model_valid: Callable, optionalThis function is called with the estimated model and the randomly selected data:
is_model_valid(model, *random_data), .max_trials: int, optionalMaximum number of iterations for random sample selection.
stop_sample_num: int, optionalStop iteration if at least this number of inliers are found.
stop_residuals_sum: float, optionalStop iteration if sum of residuals is less than or equal to this threshold.
stop_probability: float, optional, in range [0, 1]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.
rng: {`numpy.random.Generator`, int}, optionalPseudo-random number generator. By default, a PCG64 generator is used (see numpy.random.default_rng). If
rngis an int, it is used to seed the generator.initial_inliers: array-like of bool, shape (N,), optionalInitial samples selection for model estimation
model_kwargs: dict of {str: Any}, optionalThe dict of keyword arguments passed to
from_estimateofmodel_class.
Returns
model: objectBest model with largest consensus set.
inliers: ndarray of shape (N,)Boolean mask of inliers classified as
True.
Examples
Generate ellipse data without tilt and add noise: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]) rng = np.random.default_rng(203560) # do not copy this value data += rng.normal(size=data.shape)✓
data[0] = (100, 100) data[1] = (110, 120) data[2] = (120, 130) data[3] = (140, 130)✓
model = EllipseModel.from_estimate(data) np.round(model.center) np.round(model.axis_lengths) np.round(model.theta)⚠
ransac_model, inliers = ransac(data, EllipseModel, 20, 3, max_trials=50) sum(inliers) > 40⚠
from skimage.transform import SimilarityTransform rng = np.random.default_rng() src = 100 * rng.random((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))✓
Aliases
-
skimage.measure.ransac