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 { 089 robustFitter = new RANSAC<Point2d, Point2d, AffineTransformModel>(new AffineTransformModel(), 090 new AlgebraicResidual2d<AffineTransformModel>(), threshold, nIterations, stoppingCondition, true, 091 new BucketingSampler2d()); 092 } 093 094 /** 095 * Construct using the {@link LMedS} algorithm with the given expected 096 * outlier percentage 097 * 098 * @param outlierProportion 099 * expected proportion of outliers (between 0 and 1) 100 * @param modelCheck 101 * the predicate to test whether an estimated model is sane 102 */ 103 public RobustAffineTransformEstimator(double outlierProportion, Predicate<AffineTransformModel> modelCheck) { 104 robustFitter = new LMedS<Point2d, Point2d, AffineTransformModel>( 105 new AffineTransformModel(modelCheck), 106 new AlgebraicResidual2d<AffineTransformModel>(), 107 outlierProportion, true, new BucketingSampler2d()); 108 } 109 110 /** 111 * Construct using the {@link RANSAC} algorithm with the given options. 112 * 113 * @param threshold 114 * the threshold on the {@link AlgebraicResidual2d} at which to 115 * consider a point as an inlier 116 * @param nIterations 117 * the maximum number of iterations 118 * @param stoppingCondition 119 * the {@link StoppingCondition} for RANSAC 120 * @param modelCheck 121 * the predicate to test whether an estimated model is sane 122 */ 123 public RobustAffineTransformEstimator(double threshold, int nIterations, StoppingCondition stoppingCondition, 124 Predicate<AffineTransformModel> modelCheck) 125 { 126 robustFitter = new RANSAC<Point2d, Point2d, AffineTransformModel>(new AffineTransformModel(modelCheck), 127 new AlgebraicResidual2d<AffineTransformModel>(), threshold, nIterations, stoppingCondition, true, 128 new BucketingSampler2d()); 129 } 130 131 @Override 132 public boolean fitData(List<? extends IndependentPair<Point2d, Point2d>> data) { 133 // Use a robust fitting technique to find the inliers and estimate a 134 // model using DLT 135 if (!robustFitter.fitData(data)) { 136 // just go with full-on DLT estimate rather than a robust one 137 robustFitter.getModel().estimate(data); 138 139 return false; 140 } 141 142 return true; 143 } 144 145 @Override 146 public int numItemsToEstimate() { 147 return robustFitter.numItemsToEstimate(); 148 } 149 150 @Override 151 public AffineTransformModel getModel() { 152 return robustFitter.getModel(); 153 } 154 155 @Override 156 public List<? extends IndependentPair<Point2d, Point2d>> getInliers() { 157 return robustFitter.getInliers(); 158 } 159 160 @Override 161 public List<? extends IndependentPair<Point2d, Point2d>> getOutliers() { 162 return robustFitter.getOutliers(); 163 } 164}