001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.math.geometry.transforms.estimation; 031 032import java.util.List; 033 034import org.openimaj.math.geometry.point.Point2d; 035import org.openimaj.math.geometry.transforms.AffineTransformModel; 036import org.openimaj.math.geometry.transforms.TransformUtilities; 037import org.openimaj.math.geometry.transforms.estimation.sampling.BucketingSampler2d; 038import org.openimaj.math.geometry.transforms.residuals.AlgebraicResidual2d; 039import org.openimaj.math.model.fit.LMedS; 040import org.openimaj.math.model.fit.RANSAC; 041import org.openimaj.math.model.fit.RANSAC.StoppingCondition; 042import org.openimaj.math.model.fit.RobustModelFitting; 043import org.openimaj.util.function.Predicate; 044import org.openimaj.util.pair.IndependentPair; 045 046/** 047 * Helper class to simplify robust estimation of 2D affine transforms without 048 * having to deal with the nuts and bolts of the underlying robust model 049 * fitters, etc. The overall robust estimation process uses the normalised DLT 050 * algorithm (see {@link TransformUtilities#affineMatrix(List)}) coupled with 051 * either {@link RANSAC} or {@link LMedS} with a {@link BucketingSampler2d} 052 * sampling strategy for selecting subsets. 053 * <p> 054 * Non-linear optimisation is unncessary as the algebraic and geometric 055 * distances are equal in the affine case. 056 * 057 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 058 */ 059public class RobustAffineTransformEstimator implements RobustModelFitting<Point2d, Point2d, AffineTransformModel> { 060 private RobustModelFitting<Point2d, Point2d, AffineTransformModel> robustFitter; 061 062 /** 063 * Construct using the {@link LMedS} algorithm with the given expected 064 * outlier percentage 065 * 066 * @param outlierProportion 067 * expected proportion of outliers (between 0 and 1) 068 */ 069 public RobustAffineTransformEstimator(double outlierProportion) { 070 robustFitter = new LMedS<Point2d, Point2d, AffineTransformModel>( 071 new AffineTransformModel(), 072 new AlgebraicResidual2d<AffineTransformModel>(), 073 outlierProportion, true, new BucketingSampler2d()); 074 } 075 076 /** 077 * Construct using the {@link RANSAC} algorithm with the given options. 078 * 079 * @param threshold 080 * the threshold on the {@link AlgebraicResidual2d} at which to 081 * consider a point as an inlier 082 * @param nIterations 083 * the maximum number of iterations 084 * @param stoppingCondition 085 * the {@link StoppingCondition} for RANSAC 086 */ 087 public RobustAffineTransformEstimator(double threshold, int nIterations, StoppingCondition stoppingCondition) { 088 robustFitter = new RANSAC<Point2d, Point2d, AffineTransformModel>(new AffineTransformModel(), 089 new AlgebraicResidual2d<AffineTransformModel>(), threshold, nIterations, stoppingCondition, true, 090 new BucketingSampler2d()); 091 } 092 093 /** 094 * Construct using the {@link LMedS} algorithm with the given expected 095 * outlier percentage 096 * 097 * @param outlierProportion 098 * expected proportion of outliers (between 0 and 1) 099 * @param modelCheck 100 * the predicate to test whether an estimated model is sane 101 */ 102 public RobustAffineTransformEstimator(double outlierProportion, Predicate<AffineTransformModel> modelCheck) { 103 robustFitter = new LMedS<Point2d, Point2d, AffineTransformModel>( 104 new AffineTransformModel(modelCheck), 105 new AlgebraicResidual2d<AffineTransformModel>(), 106 outlierProportion, true, new BucketingSampler2d()); 107 } 108 109 /** 110 * Construct using the {@link RANSAC} algorithm with the given options. 111 * 112 * @param threshold 113 * the threshold on the {@link AlgebraicResidual2d} at which to 114 * consider a point as an inlier 115 * @param nIterations 116 * the maximum number of iterations 117 * @param stoppingCondition 118 * the {@link StoppingCondition} for RANSAC 119 * @param modelCheck 120 * the predicate to test whether an estimated model is sane 121 */ 122 public RobustAffineTransformEstimator(double threshold, int nIterations, StoppingCondition stoppingCondition, 123 Predicate<AffineTransformModel> modelCheck) 124 { 125 robustFitter = new RANSAC<Point2d, Point2d, AffineTransformModel>(new AffineTransformModel(modelCheck), 126 new AlgebraicResidual2d<AffineTransformModel>(), threshold, nIterations, stoppingCondition, true, 127 new BucketingSampler2d()); 128 } 129 130 @Override 131 public boolean fitData(List<? extends IndependentPair<Point2d, Point2d>> data) { 132 // Use a robust fitting technique to find the inliers and estimate a 133 // model using DLT 134 if (!robustFitter.fitData(data)) { 135 // just go with full-on DLT estimate rather than a robust one 136 robustFitter.getModel().estimate(data); 137 138 return false; 139 } 140 141 return true; 142 } 143 144 @Override 145 public int numItemsToEstimate() { 146 return robustFitter.numItemsToEstimate(); 147 } 148 149 @Override 150 public AffineTransformModel getModel() { 151 return robustFitter.getModel(); 152 } 153 154 @Override 155 public List<? extends IndependentPair<Point2d, Point2d>> getInliers() { 156 return robustFitter.getInliers(); 157 } 158 159 @Override 160 public List<? extends IndependentPair<Point2d, Point2d>> getOutliers() { 161 return robustFitter.getOutliers(); 162 } 163}