View Javadoc
1   /*
2    * Copyright (C) 2017 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.geometry.refiners;
17  
18  import com.irurueta.algebra.AlgebraException;
19  import com.irurueta.algebra.Matrix;
20  import com.irurueta.geometry.AffineTransformation2D;
21  import com.irurueta.geometry.Line2D;
22  import com.irurueta.geometry.estimators.LockedException;
23  import com.irurueta.geometry.estimators.NotReadyException;
24  import com.irurueta.numerical.EvaluationException;
25  import com.irurueta.numerical.GradientEstimator;
26  import com.irurueta.numerical.fitting.LevenbergMarquardtMultiDimensionFitter;
27  import com.irurueta.numerical.fitting.LevenbergMarquardtMultiDimensionFunctionEvaluator;
28  import com.irurueta.numerical.robust.InliersData;
29  
30  import java.util.BitSet;
31  import java.util.List;
32  
33  /**
34   * A 2D affine transformation refiner using line correspondences.
35   * This class takes into account an initial estimation, inlier line matches
36   * and their residuals to find a solution that minimizes error of inliers in
37   * LMSE terms.
38   * Typically, a refiner is used by a robust estimator, however it can also be
39   * useful in some other situations.
40   */
41  @SuppressWarnings("DuplicatedCode")
42  public class LineCorrespondenceAffineTransformation2DRefiner extends AffineTransformation2DRefiner<Line2D, Line2D> {
43  
44      /**
45       * Line to be reused when computing residuals.
46       */
47      private final Line2D residualTestLine = new Line2D();
48  
49      /**
50       * Constructor.
51       */
52      public LineCorrespondenceAffineTransformation2DRefiner() {
53      }
54  
55      /**
56       * Constructor.
57       *
58       * @param initialEstimation           initial estimation to be set.
59       * @param keepCovariance              true if covariance of estimation must be kept after
60       *                                    refinement, false otherwise.
61       * @param inliers                     set indicating which of the provided matches are inliers.
62       * @param residuals                   residuals for matched samples.
63       * @param numInliers                  number of inliers on initial estimation.
64       * @param samples1                    1st set of paired samples.
65       * @param samples2                    2nd set of paired samples.
66       * @param refinementStandardDeviation standard deviation used for
67       *                                    Levenberg-Marquardt fitting.
68       */
69      public LineCorrespondenceAffineTransformation2DRefiner(
70              final AffineTransformation2D initialEstimation, final boolean keepCovariance,
71              final BitSet inliers, final double[] residuals, final int numInliers,
72              final List<Line2D> samples1, final List<Line2D> samples2, final double refinementStandardDeviation) {
73          super(initialEstimation, keepCovariance, inliers, residuals, numInliers, samples1, samples2,
74                  refinementStandardDeviation);
75      }
76  
77      /**
78       * Constructor.
79       *
80       * @param initialEstimation           initial estimation to be set.
81       * @param keepCovariance              true if covariance of estimation must be kept after
82       *                                    refinement, false otherwise.
83       * @param inliersData                 inlier data, typically obtained from a robust
84       *                                    estimator.
85       * @param samples1                    1st set of paired samples.
86       * @param samples2                    2nd set of paired samples.
87       * @param refinementStandardDeviation standard deviation used for
88       *                                    Levenberg-Marquardt fitting.
89       */
90      public LineCorrespondenceAffineTransformation2DRefiner(
91              final AffineTransformation2D initialEstimation, final boolean keepCovariance,
92              final InliersData inliersData, final List<Line2D> samples1,
93              final List<Line2D> samples2, final double refinementStandardDeviation) {
94          super(initialEstimation, keepCovariance, inliersData, samples1, samples2, refinementStandardDeviation);
95      }
96  
97      /**
98       * Refines provided initial estimation.
99       * This method always sets a value into provided result instance regardless
100      * of the fact that error has actually improved in LMSE terms or not.
101      *
102      * @param result instance where refined estimation will be stored.
103      * @return true if result improves (error decreases) in LMSE terms respect
104      * to initial estimation, false if no improvement has been achieved.
105      * @throws NotReadyException if not enough input data has been provided.
106      * @throws LockedException   if estimator is locked because refinement is
107      *                           already in progress.
108      * @throws RefinerException  if refinement fails for some reason (e.g. unable
109      *                           to converge to a result).
110      */
111     @Override
112     public boolean refine(final AffineTransformation2D result) throws NotReadyException, LockedException,
113             RefinerException {
114         if (isLocked()) {
115             throw new LockedException();
116         }
117         if (!isReady()) {
118             throw new NotReadyException();
119         }
120 
121         locked = true;
122 
123         if (listener != null) {
124             listener.onRefineStart(this, initialEstimation);
125         }
126 
127         final var initialTotalResidual = totalResidual(initialEstimation);
128 
129         try {
130             final var initParams = new double[AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS
131                     + AffineTransformation2D.NUM_TRANSLATION_COORDS];
132             // copy values for A matrix
133             System.arraycopy(initialEstimation.getA().getBuffer(), 0,
134                     initParams, 0,
135                     AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS);
136             // copy values for translation
137             System.arraycopy(initialEstimation.getTranslation(), 0,
138                     initParams, AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS,
139                     AffineTransformation2D.NUM_TRANSLATION_COORDS);
140 
141             // output values to be fitted/optimized will contain residuals
142             final var y = new double[numInliers];
143             // input values will contain 2 sets of 2D points to compute residuals
144             final var nDims = 2 * Line2D.LINE_NUMBER_PARAMS;
145             final var x = new Matrix(numInliers, nDims);
146             final var nSamples = inliers.length();
147             var pos = 0;
148             for (var i = 0; i < nSamples; i++) {
149                 if (inliers.get(i)) {
150                     // sample is inlier
151                     final var inputLine = samples1.get(i);
152                     final var outputLine = samples2.get(i);
153                     inputLine.normalize();
154                     outputLine.normalize();
155                     x.setElementAt(pos, 0, inputLine.getA());
156                     x.setElementAt(pos, 1, inputLine.getB());
157                     x.setElementAt(pos, 2, inputLine.getC());
158                     x.setElementAt(pos, 3, outputLine.getA());
159                     x.setElementAt(pos, 4, outputLine.getB());
160                     x.setElementAt(pos, 5, outputLine.getC());
161 
162                     y[pos] = residuals[i];
163                     pos++;
164                 }
165             }
166 
167             final var evaluator = new LevenbergMarquardtMultiDimensionFunctionEvaluator() {
168 
169                 private final Line2D inputLine = new Line2D();
170 
171                 private final Line2D outputLine = new Line2D();
172 
173                 private final AffineTransformation2D transformation = new AffineTransformation2D();
174 
175                 private final GradientEstimator gradientEstimator = new GradientEstimator(params -> {
176                     // copy values for A matrix
177                     System.arraycopy(params, 0, transformation.getA().getBuffer(), 0,
178                             AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS);
179                     // copy values for translation
180                     System.arraycopy(params,
181                             AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS,
182                             transformation.getTranslation(), 0, AffineTransformation2D.NUM_TRANSLATION_COORDS);
183 
184                     return residual(transformation, inputLine, outputLine);
185                 });
186 
187                 @Override
188                 public int getNumberOfDimensions() {
189                     return nDims;
190                 }
191 
192                 @Override
193                 public double[] createInitialParametersArray() {
194                     return initParams;
195                 }
196 
197                 @Override
198                 public double evaluate(
199                         final int i, final double[] point, final double[] params, final double[] derivatives)
200                         throws EvaluationException {
201                     inputLine.setParameters(point[0], point[1], point[2]);
202                     outputLine.setParameters(point[3], point[4], point[5]);
203 
204                     // copy values for A matrix
205                     System.arraycopy(params, 0, transformation.getA().getBuffer(), 0,
206                             AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS);
207                     // copy values for translation
208                     System.arraycopy(params,
209                             AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS,
210                             transformation.getTranslation(), 0, AffineTransformation2D.NUM_TRANSLATION_COORDS);
211 
212                     final var y = residual(transformation, inputLine, outputLine);
213                     gradientEstimator.gradient(params, derivatives);
214 
215                     return y;
216                 }
217             };
218 
219             final var fitter = new LevenbergMarquardtMultiDimensionFitter(evaluator, x, y,
220                     getRefinementStandardDeviation());
221 
222             fitter.fit();
223 
224             // obtain estimated params
225             final var params = fitter.getA();
226 
227             // update transformation
228 
229             // copy values for A matrix
230             System.arraycopy(params, 0, result.getA().getBuffer(), 0,
231                     AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS);
232             // copy values for translation
233             System.arraycopy(params, AffineTransformation2D.INHOM_COORDS * AffineTransformation2D.INHOM_COORDS,
234                     result.getTranslation(), 0, AffineTransformation2D.NUM_TRANSLATION_COORDS);
235 
236             if (keepCovariance) {
237                 // keep covariance
238                 covariance = fitter.getCovar();
239             }
240 
241             final var finalTotalResidual = totalResidual(result);
242             final var errorDecreased = finalTotalResidual < initialTotalResidual;
243 
244             if (listener != null) {
245                 listener.onRefineEnd(this, initialEstimation, result, errorDecreased);
246             }
247 
248             return errorDecreased;
249 
250         } catch (final Exception e) {
251             throw new RefinerException(e);
252         } finally {
253             locked = false;
254         }
255     }
256 
257     /**
258      * Computes the residual between the affine transformation and a pair of
259      * matched lines.
260      *
261      * @param transformation a transformation.
262      * @param inputLine      input 2D line.
263      * @param outputLine     output 2D line.
264      * @return residual.
265      */
266     private double residual(final AffineTransformation2D transformation, final Line2D inputLine,
267                             final Line2D outputLine) {
268         try {
269             inputLine.normalize();
270             outputLine.normalize();
271 
272             transformation.transform(inputLine, residualTestLine);
273             return 1.0 - Math.abs(outputLine.dotProduct(residualTestLine));
274         } catch (final AlgebraException e) {
275             return 1.0;
276         }
277     }
278 
279     /**
280      * Computes total residual among all provided inlier samples.
281      *
282      * @param transformation a transformation.
283      * @return total residual.
284      */
285     private double totalResidual(final AffineTransformation2D transformation) {
286         var result = 0.0;
287 
288         final var nSamples = inliers.length();
289         for (var i = 0; i < nSamples; i++) {
290             if (inliers.get(i)) {
291                 // sample is inlier
292                 final var inputLine = samples1.get(i);
293                 final var outputLine = samples2.get(i);
294                 inputLine.normalize();
295                 outputLine.normalize();
296                 result += residual(transformation, inputLine, outputLine);
297             }
298         }
299 
300         return result;
301     }
302 }