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.model.landmark;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.pixel.sampling.FLineSampler;
034import org.openimaj.image.pixel.statistics.FStatisticalPixelProfileModel;
035import org.openimaj.image.pixel.statistics.PixelProfileModel;
036import org.openimaj.math.geometry.line.Line2d;
037import org.openimaj.math.geometry.point.Point2d;
038import org.openimaj.math.geometry.point.PointList;
039import org.openimaj.math.geometry.point.PointListConnections;
040import org.openimaj.util.pair.ObjectFloatPair;
041
042/**
043 * An {@link FNormalLandmarkModel} is a landmark represented by the 
044 * surface normal line of a point (which is usually part of a 
045 * {@link PointList} in an {@link FImage} connected by {@link PointListConnections}). 
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 */
049public class FNormalLandmarkModel implements LandmarkModel<FImage> {
050        /**
051         * A factory for producing {@link FNormalLandmarkModel}s
052         * 
053         * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
054         */
055        public static class Factory implements LandmarkModelFactory<FImage> {
056                private PointListConnections connections;
057                private float normalLength;
058                private int numSearchSamples;
059                private FLineSampler sampler;
060                private int numModelSamples;
061
062                /**
063                 * Default constructor.
064                 * @param connections connections between points.
065                 * @param sampler sampler for sampling along normals 
066                 * @param numModelSamples number of samples for the model
067                 * @param numSearchSamples number of samples for search; must be bigger than numModelSamples
068                 * @param normalLength length of the normal in intrinsic scale units
069                 */
070                public Factory(PointListConnections connections, FLineSampler sampler, int numModelSamples, int numSearchSamples, float normalLength) {
071                        this.connections = connections;
072                        this.sampler = sampler;
073                        this.numModelSamples = numModelSamples;
074                        this.normalLength = normalLength;
075                        this.numSearchSamples = numSearchSamples;
076                }
077
078                @Override
079                public FNormalLandmarkModel createLandmarkModel() {
080                        return new FNormalLandmarkModel(connections, sampler, numModelSamples, numSearchSamples, normalLength);
081                }
082                
083                @Override
084                public FNormalLandmarkModel createLandmarkModel(float scaleFactor) {
085                        return new FNormalLandmarkModel(connections, sampler, numModelSamples, numSearchSamples, scaleFactor * normalLength);
086                }
087        }
088
089        private PointListConnections connections;
090        private PixelProfileModel<FImage> model;
091        private float normalLength;
092        private int numModelSamples;
093        private int numSearchSamples;
094
095        /**
096         * Default constructor.
097         * 
098         * @param connections connections between points.
099         * @param sampler sampler for sampling along normals 
100         * @param numModelSamples number of samples for the model
101         * @param numSearchSamples number of samples for search; must be bigger than numModelSamples
102         * @param normalLength length of the normal in intrinsic scale units
103         */
104        public FNormalLandmarkModel(PointListConnections connections, FLineSampler sampler, int numModelSamples, int numSearchSamples, float normalLength) {
105                this.connections = connections;
106                this.model = new FStatisticalPixelProfileModel(numModelSamples, sampler);
107                this.normalLength = normalLength;
108                this.numModelSamples = numModelSamples;
109                this.numSearchSamples = numSearchSamples;
110        }
111
112        @Override
113        public void updateModel(FImage image, Point2d point, PointList pointList) {
114                float lineScale = normalLength * pointList.computeIntrinsicScale();
115                Line2d line = connections.calculateNormalLine(point, pointList, lineScale);
116                
117                model.updateModel(image, line);
118        }
119
120        @Override
121        public float computeCost(FImage image, Point2d point, PointList pointList) {
122                float lineScale = normalLength * pointList.computeIntrinsicScale();
123                Line2d line = connections.calculateNormalLine(point, pointList, lineScale);
124
125                return model.computeCost(image, line);
126        }
127
128        @Override
129        public ObjectFloatPair<Point2d> updatePosition(FImage image, Point2d initial, PointList pointList) {
130                float scale = numSearchSamples * normalLength * pointList.computeIntrinsicScale() / (float) numModelSamples;
131                Line2d line = connections.calculateNormalLine(initial, pointList, scale);
132                
133                Point2d newBest = model.computeNewBest(image, line, numSearchSamples);
134                float distance = model.computeMovementDistance(image, line, numSearchSamples, newBest);
135                
136                return new ObjectFloatPair<Point2d>(newBest, distance);
137        }
138}