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.engine.asift;
031
032import java.util.Map;
033
034import org.openimaj.feature.local.list.LocalFeatureList;
035import org.openimaj.feature.local.list.MemoryLocalFeatureList;
036import org.openimaj.image.MBFImage;
037import org.openimaj.image.feature.local.affine.AffineSimulationExtractor;
038import org.openimaj.image.feature.local.affine.AffineSimulationKeypoint;
039import org.openimaj.image.feature.local.affine.ColourASIFT;
040import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
041import org.openimaj.image.feature.local.engine.DoGSIFTEngineOptions;
042import org.openimaj.image.feature.local.engine.Engine;
043import org.openimaj.image.feature.local.keypoints.Keypoint;
044import org.openimaj.image.processing.transform.AffineParams;
045
046/**
047 * An {@link Engine} for Colour ASIFT.
048 *
049 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
050 */
051public class ColourASIFTEngine implements Engine<AffineSimulationKeypoint, MBFImage> {
052        protected AffineSimulationExtractor<LocalFeatureList<Keypoint>, Keypoint, MBFImage, Float[]> asift;
053        protected int nTilts = 5;
054
055        /**
056         * Construct using 5 tilt levels and no initial double-sizing. The default
057         * parameters for the internal {@link DoGSIFTEngine} are used.
058         */
059        public ColourASIFTEngine() {
060                this(false);
061        }
062
063        /**
064         * Construct using 5 tilt levels with optional initial double-sizing. The
065         * default parameters for the internal {@link DoGSIFTEngine} are used.
066         *
067         * @param hires
068         *            should the image should be double sized as a first step
069         */
070        public ColourASIFTEngine(boolean hires) {
071                asift = new ColourASIFT(hires);
072        }
073
074        /**
075         * Construct using given number of tilt levels with optional initial
076         * double-sizing. The default parameters for the internal
077         * {@link DoGSIFTEngine} are used.
078         *
079         * @param hires
080         *            should the image should be double sized as a first step
081         * @param nTilts
082         *            number of tilt levels
083         */
084        public ColourASIFTEngine(boolean hires, int nTilts) {
085                asift = new ColourASIFT(hires);
086                this.nTilts = nTilts;
087        }
088
089        /**
090         * Construct using 5 tilt levels and the given parameters for the internal
091         * {@link DoGSIFTEngine}.
092         *
093         * @param opts
094         *            parameters for the internal {@link DoGSIFTEngine}.
095         */
096        public ColourASIFTEngine(DoGSIFTEngineOptions<MBFImage> opts) {
097                asift = new ColourASIFT(opts);
098        }
099
100        /**
101         * Construct using the given numbe of tilt levels and parameters for the
102         * internal {@link DoGSIFTEngine}.
103         *
104         * @param opts
105         *            parameters for the internal {@link DoGSIFTEngine}.
106         * @param nTilts
107         *            number of tilt levels
108         */
109        public ColourASIFTEngine(DoGSIFTEngineOptions<MBFImage> opts, int nTilts) {
110                asift = new ColourASIFT(opts);
111                this.nTilts = nTilts;
112        }
113
114        /**
115         * Find the features as a list of {@link Keypoint} objects
116         *
117         * @param image
118         *            the image
119         * @return the detected features
120         */
121        public LocalFeatureList<Keypoint> findKeypoints(MBFImage image) {
122                asift.detectFeatures(image, nTilts);
123                return asift.getFeatures();
124        }
125
126        /**
127         * Find the features of a single simulation as a list of {@link Keypoint}
128         * objects
129         *
130         * @param image
131         *            the image
132         * @param params
133         *            the simulation parameters
134         * @return the detected features
135         */
136        public LocalFeatureList<Keypoint> findKeypoints(MBFImage image, AffineParams params) {
137                return asift.detectFeatures(image, params);
138        }
139
140        /**
141         * Find the features and return the resultant features in a per-simulation
142         * format.
143         *
144         * @param image
145         *            the image
146         * @return the features
147         */
148        public Map<AffineParams, LocalFeatureList<Keypoint>> findKeypointsMapped(MBFImage image) {
149                asift.detectFeatures(image, nTilts);
150                return asift.getKeypointsMap();
151        }
152
153        @Override
154        public LocalFeatureList<AffineSimulationKeypoint> findFeatures(MBFImage image) {
155                asift.detectFeatures(image, nTilts);
156                final Map<AffineParams, LocalFeatureList<Keypoint>> keypointMap = asift.getKeypointsMap();
157                final LocalFeatureList<AffineSimulationKeypoint> affineSimulationList = new MemoryLocalFeatureList<AffineSimulationKeypoint>();
158                for (final AffineParams params : asift.simulationOrder) {
159                        for (final Keypoint k : keypointMap.get(params)) {
160                                affineSimulationList.add(new AffineSimulationKeypoint(k, params, asift.simulationOrder.indexOf(params)));
161                        }
162                }
163                return affineSimulationList;
164        }
165}