View Javadoc
1   /*
2    * Copyright (C) 2015 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.estimators;
17  
18  import com.irurueta.algebra.AlgebraException;
19  import com.irurueta.geometry.CoincidentLinesException;
20  import com.irurueta.geometry.Line2D;
21  import com.irurueta.geometry.ProjectiveTransformation2D;
22  import com.irurueta.numerical.robust.PROSACRobustEstimator;
23  import com.irurueta.numerical.robust.PROSACRobustEstimatorListener;
24  import com.irurueta.numerical.robust.RobustEstimator;
25  import com.irurueta.numerical.robust.RobustEstimatorException;
26  import com.irurueta.numerical.robust.RobustEstimatorMethod;
27  
28  import java.util.List;
29  
30  /**
31   * Finds the best projective 2D transformation for provided collections of
32   * matched 2D lines using PROSAC algorithm.
33   */
34  @SuppressWarnings("DuplicatedCode")
35  public class PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator
36          extends LineCorrespondenceProjectiveTransformation2DRobustEstimator {
37  
38      /**
39       * Constant defining default threshold to determine whether lines are
40       * inliers or not.
41       * Residuals to determine whether lines are inliers or not are computed by
42       * comparing two lines algebraically (e.g. doing the dot product of their
43       * parameters).
44       * A residual of 0 indicates that dot product was 1 or -1 and lines were
45       * equal.
46       * A residual of 1 indicates that dot product was 0 and lines were
47       * orthogonal.
48       * If dot product between lines is -1, then although their director vectors
49       * are opposed, lines are considered equal, since sign changes are not taken
50       * into account and their residuals will be 0.
51       */
52      public static final double DEFAULT_THRESHOLD = 1e-6;
53  
54      /**
55       * Minimum value that can be set as threshold.
56       * Threshold must be strictly greater than 0.0.
57       */
58      public static final double MIN_THRESHOLD = 0.0;
59  
60      /**
61       * Indicates that by default inliers will only be computed but not kept.
62       */
63      public static final boolean DEFAULT_COMPUTE_AND_KEEP_INLIERS = false;
64  
65      /**
66       * Indicates that by default residuals will only be computed but not kept.
67       */
68      public static final boolean DEFAULT_COMPUTE_AND_KEEP_RESIDUALS = false;
69  
70      /**
71       * Threshold to determine whether lines are inliers or not when testing
72       * possible estimation solutions.
73       * The threshold refers to the amount of error (i.e. distance and director
74       * vector angle difference) a possible solution has on a matched pair of
75       * lines.
76       */
77      private double threshold;
78  
79      /**
80       * Quality scores corresponding to each pair of matched lines.
81       * The larger the score value the better the quality of the matching.
82       */
83      private double[] qualityScores;
84  
85      /**
86       * Indicates whether inliers must be computed and kept.
87       */
88      private boolean computeAndKeepInliers;
89  
90      /**
91       * Indicates whether residuals must be computed and kept.
92       */
93      private boolean computeAndKeepResiduals;
94  
95      /**
96       * Constructor.
97       */
98      public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator() {
99          super();
100         threshold = DEFAULT_THRESHOLD;
101         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
102         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
103     }
104 
105     /**
106      * Constructor with lists of lines to be used to estimate a projective 2D
107      * transformation.
108      * Lines in the list located at the same position are considered to be
109      * matched. Hence, both lists must have the same size, and their size must
110      * be greater or equal than MINIMUM_SIZE.
111      *
112      * @param inputLines  list of input lines to be used to estimate a projective
113      *                    2D transformation.
114      * @param outputLines list of output lines to be used to estimate a
115      *                    projective 2D transformation.
116      * @throws IllegalArgumentException if provided lists of lines don't have
117      *                                  the same size or their size is smaller than MINIMUM_SIZE.
118      */
119     public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator(
120             final List<Line2D> inputLines, final List<Line2D> outputLines) {
121         super(inputLines, outputLines);
122         threshold = DEFAULT_THRESHOLD;
123         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
124         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
125     }
126 
127     /**
128      * Constructor.
129      *
130      * @param listener listener to be notified of events such as when estimation
131      *                 starts, ends or its progress significantly changes.
132      */
133     public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator(
134             final ProjectiveTransformation2DRobustEstimatorListener listener) {
135         super(listener);
136         threshold = DEFAULT_THRESHOLD;
137         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
138         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
139     }
140 
141     /**
142      * Constructor with listener and lists of lines to be used to estimate a
143      * projective 2D transformation.
144      * Lines in the list located at the same position are considered to be
145      * matched. Hence, both lists must have the same size, and their size must
146      * be greater or equal than MINIMUM_SIZE.
147      *
148      * @param listener    listener to be notified of events such as when estimation
149      *                    starts, ends or its progress significantly changes.
150      * @param inputLines  list of input lines to be used to estimate a projective
151      *                    2D transformation.
152      * @param outputLines list of output lines to be used to estimate a
153      *                    projective 2D transformation.
154      * @throws IllegalArgumentException if provided lists of lines don't have
155      *                                  the same size or their size is smaller than MINIMUM_SIZE.
156      */
157     public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator(
158             final ProjectiveTransformation2DRobustEstimatorListener listener,
159             final List<Line2D> inputLines, final List<Line2D> outputLines) {
160         super(listener, inputLines, outputLines);
161         threshold = DEFAULT_THRESHOLD;
162         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
163         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
164     }
165 
166     /**
167      * Constructor.
168      *
169      * @param qualityScores quality scores corresponding to each pair of matched
170      *                      points.
171      * @throws IllegalArgumentException if provided quality scores length is
172      *                                  smaller than MINIMUM_SIZE (i.e. 3 samples).
173      */
174     public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator(final double[] qualityScores) {
175         super();
176         threshold = DEFAULT_THRESHOLD;
177         internalSetQualityScores(qualityScores);
178         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
179         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
180     }
181 
182     /**
183      * Constructor with lists of lines to be used to estimate a projective 2D
184      * transformation.
185      * Lines in the list located at the same position are considered to be
186      * matched. Hence, both lists must have the same size, and their size must
187      * be greater or equal than MINIMUM_SIZE.
188      *
189      * @param inputLines    list of input lines to be used to estimate a projective
190      *                      2D transformation.
191      * @param outputLines   list of output lines to be used to estimate a
192      *                      projective 2D transformation.
193      * @param qualityScores quality scores corresponding to each pair of matched
194      *                      lines.
195      * @throws IllegalArgumentException if provided lists of lines and array
196      *                                  of quality scores don't have the same size or their size is smaller than
197      *                                  MINIMUM_SIZE.
198      */
199     public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator(
200             final List<Line2D> inputLines, final List<Line2D> outputLines, final double[] qualityScores) {
201         super(inputLines, outputLines);
202 
203         if (qualityScores.length != inputLines.size()) {
204             throw new IllegalArgumentException();
205         }
206 
207         threshold = DEFAULT_THRESHOLD;
208         internalSetQualityScores(qualityScores);
209         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
210         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
211     }
212 
213     /**
214      * Constructor.
215      *
216      * @param listener      listener to be notified of events such as when estimation
217      *                      starts, ends or its progress significantly changes.
218      * @param qualityScores quality scores corresponding to each pair of matched
219      *                      lines.
220      * @throws IllegalArgumentException if provided quality scores length is
221      *                                  smaller than MINIMUM_SIZE (i.e. 3 samples).
222      */
223     public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator(
224             final ProjectiveTransformation2DRobustEstimatorListener listener, final double[] qualityScores) {
225         super(listener);
226         threshold = DEFAULT_THRESHOLD;
227         internalSetQualityScores(qualityScores);
228         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
229         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
230     }
231 
232     /**
233      * Constructor with listener and lists of lines to be used to estimate a
234      * projective 2D transformation.
235      * Lines in the list located at the same position are considered to be
236      * matched. Hence, both lists must have the same size, and their size must
237      * be greater or equal than MINIMUM_SIZE.
238      *
239      * @param listener      listener to be notified of events such as when estimation
240      *                      starts, ends or its progress significantly changes.
241      * @param inputLines    list of input lines to be used to estimate a projective
242      *                      2D transformation.
243      * @param outputLines   list of output lines to be used to estimate a
244      *                      projective 2D transformation.
245      * @param qualityScores quality scores corresponding to each pair of matched
246      *                      lines.
247      * @throws IllegalArgumentException if provided lists of lines don't have
248      *                                  the same size or their size is smaller than MINIMUM_SIZE.
249      */
250     public PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator(
251             final ProjectiveTransformation2DRobustEstimatorListener listener,
252             final List<Line2D> inputLines, final List<Line2D> outputLines, final double[] qualityScores) {
253         super(listener, inputLines, outputLines);
254 
255         if (qualityScores.length != inputLines.size()) {
256             throw new IllegalArgumentException();
257         }
258 
259         threshold = DEFAULT_THRESHOLD;
260         internalSetQualityScores(qualityScores);
261         computeAndKeepInliers = DEFAULT_COMPUTE_AND_KEEP_INLIERS;
262         computeAndKeepResiduals = DEFAULT_COMPUTE_AND_KEEP_RESIDUALS;
263     }
264 
265     /**
266      * Returns threshold to determine whether lines are inliers or not when
267      * testing possible estimation solutions.
268      * Residuals to determine whether lines are inliers or not are computed by
269      * comparing two lines algebraically (e.g. doing the dot product of their
270      * parameters).
271      * A residual of 0 indicates that dot product was 1 or -1 and lines were
272      * equal.
273      * A residual of 1 indicates that dot product was 0 and lines were
274      * orthogonal.
275      * If dot product between lines is -1, then although their director vectors
276      * are opposed, lines are considered equal, since sign changes are not taken
277      * into account and their residuals will be 0.
278      *
279      * @return threshold to determine whether matched lines are inliers or not.
280      */
281     public double getThreshold() {
282         return threshold;
283     }
284 
285     /**
286      * Sets threshold to determine whether lines are inliers or not when
287      * testing possible estimation solutions.
288      * Residuals to determine whether lines are inliers or not are computed by
289      * comparing two lines algebraically (e.g. doing the dot product of their
290      * parameters).
291      * A residual of 0 indicates that dot product was 1 or -1 and lines were
292      * equal.
293      * A residual of 1 indicates that dot product was 0 and lines were
294      * orthogonal.
295      * If dot product between lines is -1, then although their director vectors
296      * are opposed, lines are considered equal, since sign changes are not taken
297      * into account and their residuals will be 0.
298      *
299      * @param threshold threshold to determine whether matched lines are inliers
300      *                  or not.
301      * @throws IllegalArgumentException if provided value is equal or less than
302      *                                  zero.
303      * @throws LockedException          if robust estimator is locked because an
304      *                                  estimation is already in progress.
305      */
306     public void setThreshold(final double threshold) throws LockedException {
307         if (isLocked()) {
308             throw new LockedException();
309         }
310         if (threshold <= MIN_THRESHOLD) {
311             throw new IllegalArgumentException();
312         }
313         this.threshold = threshold;
314     }
315 
316     /**
317      * Returns quality scores corresponding to each pair of matched lines.
318      * The larger the score value the better the quality of the matching.
319      *
320      * @return quality scores corresponding to each pair of matched lines.
321      */
322     @Override
323     public double[] getQualityScores() {
324         return qualityScores;
325     }
326 
327     /**
328      * Sets quality scores corresponding to each pair of matched lines.
329      * The larger the score value the better the quality of the matching.
330      *
331      * @param qualityScores quality scores corresponding to each pair of matched
332      *                      lines.
333      * @throws LockedException          if robust estimator is locked because an
334      *                                  estimation is already in progress.
335      * @throws IllegalArgumentException if provided quality scores length is
336      *                                  smaller than MINIMUM_SIZE (i.e. 3 samples).
337      */
338     @Override
339     public void setQualityScores(final double[] qualityScores) throws LockedException {
340         if (isLocked()) {
341             throw new LockedException();
342         }
343         internalSetQualityScores(qualityScores);
344     }
345 
346     /**
347      * Indicates if estimator is ready to start the projective 2D transformation
348      * estimation.
349      * This is true when input data (i.e. lists of matched lines and quality
350      * scores) are provided and a minimum of MINIMUM_SIZE lines are available.
351      *
352      * @return true if estimator is ready, false otherwise.
353      */
354     @Override
355     public boolean isReady() {
356         return super.isReady() && qualityScores != null && qualityScores.length == inputLines.size();
357     }
358 
359     /**
360      * Indicates whether inliers must be computed and kept.
361      *
362      * @return true if inliers must be computed and kept, false if inliers
363      * only need to be computed but not kept.
364      */
365     public boolean isComputeAndKeepInliersEnabled() {
366         return computeAndKeepInliers;
367     }
368 
369     /**
370      * Specifies whether inliers must be computed and kept.
371      *
372      * @param computeAndKeepInliers true if inliers must be computed and kept,
373      *                              false if inliers only need to be computed but not kept.
374      * @throws LockedException if estimator is locked.
375      */
376     public void setComputeAndKeepInliersEnabled(final boolean computeAndKeepInliers) throws LockedException {
377         if (isLocked()) {
378             throw new LockedException();
379         }
380         this.computeAndKeepInliers = computeAndKeepInliers;
381     }
382 
383     /**
384      * Indicates whether residuals must be computed and kept.
385      *
386      * @return true if residuals must be computed and kept, false if residuals
387      * only need to be computed but not kept.
388      */
389     public boolean isComputeAndKeepResidualsEnabled() {
390         return computeAndKeepResiduals;
391     }
392 
393     /**
394      * Specifies whether residuals must be computed and kept.
395      *
396      * @param computeAndKeepResiduals true if residuals must be computed and
397      *                                kept, false if residuals only need to be computed but not kept.
398      * @throws LockedException if estimator is locked.
399      */
400     public void setComputeAndKeepResidualsEnabled(final boolean computeAndKeepResiduals) throws LockedException {
401         if (isLocked()) {
402             throw new LockedException();
403         }
404         this.computeAndKeepResiduals = computeAndKeepResiduals;
405     }
406 
407     /**
408      * Estimates a projective 2D transformation using a robust estimator and
409      * the best set of matched 2D lines correspondences found using the robust
410      * estimator.
411      *
412      * @return a projective 2D transformation.
413      * @throws LockedException          if robust estimator is locked because an
414      *                                  estimation is already in progress.
415      * @throws NotReadyException        if provided input data is not enough to start
416      *                                  the estimation.
417      * @throws RobustEstimatorException if estimation fails for any reason
418      *                                  (i.e. numerical instability, no solution available, etc).
419      */
420     @Override
421     public ProjectiveTransformation2D estimate() throws LockedException, NotReadyException, RobustEstimatorException {
422         if (isLocked()) {
423             throw new LockedException();
424         }
425         if (!isReady()) {
426             throw new NotReadyException();
427         }
428 
429         final var innerEstimator = new PROSACRobustEstimator<>(
430                 new PROSACRobustEstimatorListener<ProjectiveTransformation2D>() {
431 
432                     // line to be reused when computing residuals
433                     private final Line2D testLine = new Line2D();
434 
435                     @Override
436                     public double getThreshold() {
437                         return threshold;
438                     }
439 
440                     @Override
441                     public int getTotalSamples() {
442                         return inputLines.size();
443                     }
444 
445                     @Override
446                     public int getSubsetSize() {
447                         return ProjectiveTransformation2DRobustEstimator.MINIMUM_SIZE;
448                     }
449 
450                     @Override
451                     public void estimatePreliminarSolutions(
452                             final int[] samplesIndices, final List<ProjectiveTransformation2D> solutions) {
453                         final var inputLine1 = inputLines.get(samplesIndices[0]);
454                         final var inputLine2 = inputLines.get(samplesIndices[1]);
455                         final var inputLine3 = inputLines.get(samplesIndices[2]);
456                         final var inputLine4 = inputLines.get(samplesIndices[3]);
457 
458                         final var outputLine1 = outputLines.get(samplesIndices[0]);
459                         final var outputLine2 = outputLines.get(samplesIndices[1]);
460                         final var outputLine3 = outputLines.get(samplesIndices[2]);
461                         final var outputLine4 = outputLines.get(samplesIndices[3]);
462 
463                         try {
464                             final var transformation = new ProjectiveTransformation2D(inputLine1, inputLine2,
465                                     inputLine3, inputLine4, outputLine1, outputLine2, outputLine3, outputLine4);
466                             solutions.add(transformation);
467                         } catch (final CoincidentLinesException e) {
468                             // if lines are coincident, no solution is added
469                         }
470                     }
471 
472                     @Override
473                     public double computeResidual(final ProjectiveTransformation2D currentEstimation, final int i) {
474                         final var inputLine = inputLines.get(i);
475                         final var outputLine = outputLines.get(i);
476 
477                         // transform input line and store result in mTestLine
478                         try {
479                             currentEstimation.transform(inputLine, testLine);
480 
481                             return getResidual(outputLine, testLine);
482                         } catch (final AlgebraException e) {
483                             // this happens when internal matrix of affine transformation
484                             // cannot be reverse (i.e. transformation is not well-defined,
485                             // numerical instabilities, etc.)
486                             return Double.MAX_VALUE;
487                         }
488                     }
489 
490                     @Override
491                     public boolean isReady() {
492                         return PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator.this.isReady();
493                     }
494 
495                     @Override
496                     public void onEstimateStart(final RobustEstimator<ProjectiveTransformation2D> estimator) {
497                         if (listener != null) {
498                             listener.onEstimateStart(
499                                     PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator.this);
500                         }
501                     }
502 
503                     @Override
504                     public void onEstimateEnd(final RobustEstimator<ProjectiveTransformation2D> estimator) {
505                         if (listener != null) {
506                             listener.onEstimateEnd(
507                                     PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator.this);
508                         }
509                     }
510 
511                     @Override
512                     public void onEstimateNextIteration(
513                             final RobustEstimator<ProjectiveTransformation2D> estimator, final int iteration) {
514                         if (listener != null) {
515                             listener.onEstimateNextIteration(
516                                     PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator.this,
517                                     iteration);
518                         }
519                     }
520 
521                     @Override
522                     public void onEstimateProgressChange(
523                             final RobustEstimator<ProjectiveTransformation2D> estimator, final float progress) {
524                         if (listener != null) {
525                             listener.onEstimateProgressChange(
526                                     PROSACLineCorrespondenceProjectiveTransformation2DRobustEstimator.this,
527                                     progress);
528                         }
529                     }
530 
531                     @Override
532                     public double[] getQualityScores() {
533                         return qualityScores;
534                     }
535                 });
536 
537         try {
538             locked = true;
539             inliersData = null;
540             innerEstimator.setComputeAndKeepInliersEnabled(computeAndKeepInliers || refineResult);
541             innerEstimator.setComputeAndKeepResidualsEnabled(computeAndKeepResiduals || refineResult);
542             innerEstimator.setConfidence(confidence);
543             innerEstimator.setMaxIterations(maxIterations);
544             innerEstimator.setProgressDelta(progressDelta);
545             final var transformation = innerEstimator.estimate();
546             inliersData = innerEstimator.getInliersData();
547             return attemptRefine(transformation);
548         } catch (final com.irurueta.numerical.LockedException e) {
549             throw new LockedException(e);
550         } catch (final com.irurueta.numerical.NotReadyException e) {
551             throw new NotReadyException(e);
552         } finally {
553             locked = false;
554         }
555     }
556 
557     /**
558      * Returns method being used for robust estimation.
559      *
560      * @return method being used for robust estimation.
561      */
562     @Override
563     public RobustEstimatorMethod getMethod() {
564         return RobustEstimatorMethod.PROSAC;
565     }
566 
567     /**
568      * Gets standard deviation used for Levenberg-Marquardt fitting during
569      * refinement.
570      * Returned value gives an indication of how much variance each residual
571      * has.
572      * Typically, this value is related to the threshold used on each robust
573      * estimation, since residuals of found inliers are within the range of
574      * such threshold.
575      *
576      * @return standard deviation used for refinement.
577      */
578     @Override
579     protected double getRefinementStandardDeviation() {
580         return threshold;
581     }
582 
583     /**
584      * Sets quality scores corresponding to each pair of matched lines.
585      * This method is used internally and does not check whether instance is
586      * locked or not.
587      *
588      * @param qualityScores quality scores to be set.
589      * @throws IllegalArgumentException if provided quality scores length is
590      *                                  smaller than MINIMUM_SIZE.
591      */
592     private void internalSetQualityScores(final double[] qualityScores) {
593         if (qualityScores.length < MINIMUM_SIZE) {
594             throw new IllegalArgumentException();
595         }
596 
597         this.qualityScores = qualityScores;
598     }
599 }