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.image.feature.local.interest.experiment;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import javax.swing.JFrame;
036
037import org.openimaj.image.DisplayUtilities;
038import org.openimaj.image.MBFImage;
039import org.openimaj.image.colour.ColourSpace;
040import org.openimaj.image.colour.RGBColour;
041import org.openimaj.image.feature.local.interest.EllipticInterestPointData;
042import org.openimaj.math.geometry.point.Point2d;
043import org.openimaj.math.geometry.shape.Ellipse;
044import org.openimaj.math.geometry.shape.Polygon;
045import org.openimaj.math.geometry.shape.Shape;
046import org.openimaj.math.geometry.transforms.TransformUtilities;
047
048import cern.colt.Arrays;
049
050import Jama.Matrix;
051
052public class RepeatabilityExperiment {
053        private IPDRepeatability<EllipticInterestPointData> rep;
054
055        RepeatabilityExperiment(MBFImage img1, MBFImage img2, List<Ellipse> e1, List<Ellipse> e2, Matrix transform, double d){
056                this.rep = IPDRepeatability.repeatability(img1, img2, e1, e2, transform, d);
057        }
058        
059        public double[] doExperiment(){
060                return new double[]{this.rep.repeatability(0.5)};
061        }
062        
063        public static void main(String args[]){
064                int centerx = 200;
065                int centery = 200;
066                Ellipse e1 = new Ellipse(centerx,centery,30,20,Math.PI/4f);
067                Polygon e1Box = e1.calculateOrientedBoundingBox();
068                
069                MBFImage e1Image = new MBFImage(400,400,ColourSpace.RGB);
070                MBFImage e2Image = new MBFImage(400,400,ColourSpace.RGB);
071                
072                e1Image.drawShape(e1, RGBColour.RED);
073                e1Image.drawShape(e1Box, RGBColour.BLUE);
074                
075                Matrix rot = TransformUtilities.rotationMatrixAboutPoint(Math.PI/2f,centerx,centery);
076                Matrix scale = TransformUtilities.scaleMatrixAboutPoint(2.0, 3.0,centerx,centery);
077                Matrix move = TransformUtilities.translateMatrix(100,100);
078                Matrix transform = Matrix.identity(3,3);
079                transform = transform.times(move);
080                transform = transform.times(scale);
081                transform = transform.times(rot);
082                
083                Point2d e2Pos = e1.transform(transform).calculateCentroid();
084                e2Image.drawPoint(e2Pos, RGBColour.GREEN, 3);
085                Matrix mrot = TransformUtilities.rotationMatrixAboutPoint(0.4, (int)(e2Pos.getX()), (int)(e2Pos.getY()));
086                Matrix mscale = TransformUtilities.scaleMatrixAboutPoint(1.02, 1.01,(int)e2Pos.getX(),(int)e2Pos.getY());
087                Matrix mmove = TransformUtilities.translateMatrix(-2,-2);
088                
089                Matrix pert = Matrix.identity(3, 3);
090                pert = pert.times(mmove);
091                pert = pert.times(mrot);
092                pert = pert.times(mscale);
093                
094                e2Image.drawShape(e1Box.transform(pert.times(transform)), RGBColour.BLUE);
095                e2Image.drawShape(e1.transform(pert.times(transform)), RGBColour.RED);
096                
097                e2Image.drawShape(e1Box.transform(transform), RGBColour.GREEN);
098                e2Image.drawShape(e1.transform(transform), RGBColour.ORANGE);
099                
100                Shape e2Untransform = e1.transform(pert.times(transform)).transform(transform.inverse());
101                e1Image.drawShape(e2Untransform, RGBColour.GREEN);
102                e1Image.drawPoint(e2Untransform.calculateCentroid(), RGBColour.GREEN, 3);
103                
104                ArrayList<Ellipse> e1List = new ArrayList<Ellipse>();
105                e1List.add(e1);
106                
107                ArrayList<Ellipse> e2List = new ArrayList<Ellipse>();
108                e2List.add(e1.transformAffine(pert.times(transform)));
109                
110                RepeatabilityExperiment exp = new RepeatabilityExperiment(e1Image,e2Image,e1List,e2List,transform,1);
111                System.out.println(Arrays.toString(exp.doExperiment()));
112                DisplayUtilities.display(e1Image, "Untransformed image");
113                JFrame w2 = DisplayUtilities.display(e2Image, "Transformed image");
114                w2.setBounds(400, 0, 400, 400);
115                
116        }
117}