|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.hipparchus.fitting.ransac; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.Random; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import org.hipparchus.exception.LocalizedCoreFormats; |
| 26 | +import org.hipparchus.exception.MathIllegalArgumentException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class implementing Random sample consensus (RANSAC) algorithm. |
| 30 | + * <p> |
| 31 | + * RANSAC is a robust method for estimating the parameters of a |
| 32 | + * mathematical model from a set of observed data. |
| 33 | + * It works iteratively selecting random subsets of the input data, |
| 34 | + * fitting a model to these subsets, and then determining how many |
| 35 | + * data points from the entire set are consistent with the estimated |
| 36 | + * model parameters. |
| 37 | + * The model can yields the largest number of inliers (i.e., point |
| 38 | + * that fit well) is considered the best estimate. |
| 39 | + * </p> |
| 40 | + * <p> |
| 41 | + * This implementation is designed to be generic and can be used with |
| 42 | + * different types of models, such as {@link PolynomialModelFitter |
| 43 | + * polynomial models}. |
| 44 | + * </p> |
| 45 | + * @param <M> mathematical model representing the parameters to estimate |
| 46 | + * @since 4.1 |
| 47 | + */ |
| 48 | +public class RansacFitter<M> { |
| 49 | + |
| 50 | + /** Mathematical model fitter. */ |
| 51 | + private final IModelFitter<M> fitter; |
| 52 | + |
| 53 | + /** The minimum number of data points to estimate the model parameters. */ |
| 54 | + private final int sampleSize; |
| 55 | + |
| 56 | + /** The maximum number of iterations allowed to fit the model. */ |
| 57 | + private final int maxIterations; |
| 58 | + |
| 59 | + /** Threshold to assert that a data point fits the model. */ |
| 60 | + private final double threshold; |
| 61 | + |
| 62 | + /** The minimum number of close data points required to assert that the model fits the input data. */ |
| 63 | + private final int minInliers; |
| 64 | + |
| 65 | + /** Random generator. */ |
| 66 | + private final Random random; |
| 67 | + |
| 68 | + /** |
| 69 | + * Constructor. |
| 70 | + * @param fitter mathematical model fitter |
| 71 | + * @param sampleSize minimum number of data points to estimate the model parameters |
| 72 | + * @param maxIterations maximum number of iterations allowed to fit the model |
| 73 | + * @param threshold threshold to assert that a data point fits the model |
| 74 | + * @param minInliers minimum number of close data points required to assert that the model fits the input data |
| 75 | + * @param seed seed for the random generator |
| 76 | + */ |
| 77 | + public RansacFitter(final IModelFitter<M> fitter, final int sampleSize, |
| 78 | + final int maxIterations, final double threshold, |
| 79 | + final int minInliers, final int seed) { |
| 80 | + this.fitter = fitter; |
| 81 | + this.sampleSize = sampleSize; |
| 82 | + this.maxIterations = maxIterations; |
| 83 | + this.threshold = threshold; |
| 84 | + this.minInliers = minInliers; |
| 85 | + this.random = new Random(seed); |
| 86 | + checkInputs(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Fits the set of observed data to determine the model parameters. |
| 91 | + * @param points set of observed data |
| 92 | + * @return a java class containing the best estimate of the model parameters |
| 93 | + */ |
| 94 | + public RansacFitterOutputs<M> fit(final List<double[]> points) { |
| 95 | + |
| 96 | + // Initialize the best model data |
| 97 | + final List<double[]> data = new ArrayList<>(points); |
| 98 | + Optional<M> bestModel = Optional.empty(); |
| 99 | + List<double[]> bestInliers = new ArrayList<>(); |
| 100 | + |
| 101 | + // Iterative loop to determine the best model |
| 102 | + for (int iteration = 0; iteration < maxIterations; iteration++) { |
| 103 | + |
| 104 | + // Random permute the set of observed data and determine the inliers |
| 105 | + Collections.shuffle(data, random); |
| 106 | + final List<double[]> inliers = determineCurrentInliersFromRandomlyPermutedPoints(data); |
| 107 | + |
| 108 | + // Verifies if the current inliers are fit better the model than the previous ones |
| 109 | + if (isCurrentInliersSetBetterThanPreviousOne(inliers, bestInliers)) { |
| 110 | + bestModel = Optional.of(fitter.fitModel(inliers)); |
| 111 | + bestInliers = inliers; |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + // Returns the best model data |
| 117 | + return new RansacFitterOutputs<>(bestModel, bestInliers); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Determines the current inliers (i.e., points that fit well the model) from the input randomly permuted data. |
| 122 | + * @param permutedPoints randomly permuted data |
| 123 | + * @return the list of inliers |
| 124 | + */ |
| 125 | + private List<double[]> determineCurrentInliersFromRandomlyPermutedPoints(final List<double[]> permutedPoints) { |
| 126 | + M model = fitter.fitModel(permutedPoints.subList(0, sampleSize)); |
| 127 | + return permutedPoints.stream().filter(point -> fitter.computeModelError(model, point) < threshold).collect(Collectors.toList()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Verifies is the current inliers are better than the previous ones. |
| 132 | + * @param current current inliers |
| 133 | + * @param previous previous inliers |
| 134 | + * @return true is the current inlier are better than the previous ones |
| 135 | + */ |
| 136 | + private boolean isCurrentInliersSetBetterThanPreviousOne(final List<double[]> current, final List<double[]> previous) { |
| 137 | + return current.size() > previous.size() && current.size() >= minInliers; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Checks that the fitter inputs are correct. |
| 142 | + */ |
| 143 | + private void checkInputs() { |
| 144 | + if (maxIterations < 0) { |
| 145 | + throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, maxIterations, 0); |
| 146 | + } |
| 147 | + if (sampleSize < 0) { |
| 148 | + throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, sampleSize, 0); |
| 149 | + } |
| 150 | + if (threshold < 0.) { |
| 151 | + throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, threshold, 0); |
| 152 | + } |
| 153 | + if (minInliers < 0) { |
| 154 | + throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, minInliers, 0); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments