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.residuals; 031 032import java.util.List; 033 034import org.openimaj.math.geometry.point.Point2d; 035import org.openimaj.math.geometry.transforms.MatrixTransformProvider; 036import org.openimaj.math.model.Model; 037import org.openimaj.math.model.fit.residuals.ResidualCalculator; 038import org.openimaj.util.pair.IndependentPair; 039 040import Jama.Matrix; 041 042/** 043 * Compute the algebraic residuals of points mapped by a 2d homogeneous 044 * transform (i.e. a 3x3 transform matrix). This is equivalent to the residuals 045 * minimised when using the Direct Linear Transform method (i.e. minimising 046 * |Ah|) used for estimating the transform. 047 * 048 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 049 * 050 * @param <M> 051 */ 052public class AlgebraicResidual2d<M extends Model<Point2d, Point2d> & MatrixTransformProvider> 053 implements 054 ResidualCalculator<Point2d, Point2d, M> 055{ 056 private Matrix transform; 057 058 @Override 059 public void setModel(M model) { 060 this.transform = model.getTransform(); 061 062 if (transform.getRowDimension() != 3 || transform.getColumnDimension() != 3) 063 throw new IllegalArgumentException("Transform matrix must be 3x3"); 064 } 065 066 @Override 067 public double computeResidual(IndependentPair<Point2d, Point2d> data) { 068 final Point2d p1 = data.getFirstObject(); 069 final Point2d p2 = data.getSecondObject(); 070 071 final float x = p1.getX(); 072 final float y = p1.getY(); 073 final float X = p2.getX(); 074 final float Y = p2.getY(); 075 076 final double h11 = transform.get(0, 0); 077 final double h12 = transform.get(0, 1); 078 final double h13 = transform.get(0, 2); 079 final double h21 = transform.get(1, 0); 080 final double h22 = transform.get(1, 1); 081 final double h23 = transform.get(1, 2); 082 final double h31 = transform.get(2, 0); 083 final double h32 = transform.get(2, 1); 084 final double h33 = transform.get(2, 2); 085 086 final double s1 = x * h11 + y * h12 + h13 - x * X * h31 - y * X * h32 - X * h33; 087 final double s2 = x * h21 + y * h22 + h23 - x * Y * h31 - y * Y * h32 - Y * h33; 088 089 return s1 * s1 + s2 * s2; 090 } 091 092 @Override 093 public void computeResiduals(List<? extends IndependentPair<Point2d, Point2d>> data, double[] residuals) { 094 for (int i = 0; i < data.size(); i++) { 095 residuals[i] = computeResidual(data.get(i)); 096 } 097 } 098}