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.feature.local.matcher;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import org.openimaj.citation.annotation.Reference;
036import org.openimaj.citation.annotation.ReferenceType;
037import org.openimaj.citation.annotation.References;
038import org.openimaj.feature.DoubleFVComparison;
039import org.openimaj.feature.local.LocalFeature;
040import org.openimaj.util.pair.Pair;
041
042/**
043 * Basic local feature matcher. Matches interest points by finding closest two
044 * interest points to target and checking whether the distance between the two
045 * matches is sufficiently large.
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 * @param <T>
049 */
050@References(references = {
051                @Reference(
052                                type = ReferenceType.Article,
053                                author = { "David Lowe" },
054                                title = "Distinctive image features from scale-invariant keypoints",
055                                year = "2004",
056                                journal = "IJCV",
057                                pages = { "91", "110" },
058                                month = "January",
059                                number = "2",
060                                volume = "60"),
061                @Reference(
062                                type = ReferenceType.Inproceedings,
063                                author = { "David Lowe" },
064                                title = "Object recognition from local scale-invariant features",
065                                year = "1999",
066                                booktitle = "Proc. of the International Conference on Computer Vision {ICCV}",
067                                pages = { "1150", "1157" }
068                )
069})
070public class BasicMatcher<T extends LocalFeature<?, ?>> implements LocalFeatureMatcher<T> {
071        protected List<T> modelKeypoints;
072        protected List<Pair<T>> matches;
073        protected int thresh = 8;
074
075        /**
076         * Initialise the matcher setting the threshold which the difference between
077         * the scores of the top two best matches must differ in order to count the
078         * first as a good match.
079         * 
080         * @param threshold
081         */
082        public BasicMatcher(int threshold)
083        {
084                matches = new ArrayList<Pair<T>>();
085                thresh = threshold;
086        }
087
088        /**
089         * @return List of pairs of matching keypoints
090         */
091        @Override
092        public List<Pair<T>> getMatches() {
093                return matches;
094        }
095
096        @Override
097        public boolean findMatches(List<T> keys1)
098        {
099                matches = new ArrayList<Pair<T>>();
100
101                /*
102                 * Match the keys in list keys1 to their best matches in keys2.
103                 */
104                for (final T k : keys1) {
105                        final T match = checkForMatch(k, modelKeypoints);
106
107                        if (match != null) {
108                                matches.add(new Pair<T>(k, match));
109                        }
110                }
111
112                return true;
113        }
114
115        /**
116         * This searches through the keypoints in klist for the two closest matches
117         * to key. If the closest is less than <code>threshold</code> times distance
118         * to second closest, then return the closest match. Otherwise, return NULL.
119         */
120        protected T checkForMatch(T query, List<T> features)
121        {
122                double distsq1 = Double.MAX_VALUE, distsq2 = Double.MAX_VALUE;
123                T minkey = null;
124
125                // find two closest matches
126                for (final T target : features) {
127                        final double dsq = target.getFeatureVector().asDoubleFV()
128                                        .compare(query.getFeatureVector().asDoubleFV(), DoubleFVComparison.EUCLIDEAN);
129
130                        if (dsq < distsq1) {
131                                distsq2 = distsq1;
132                                distsq1 = dsq;
133                                minkey = target;
134                        } else if (dsq < distsq2) {
135                                distsq2 = dsq;
136                        }
137                }
138
139                // check the distance against the threshold
140                if (10 * 10 * distsq1 < thresh * thresh * distsq2) {
141                        return minkey;
142                }
143                else
144                        return null;
145        }
146
147        @Override
148        public void setModelFeatures(List<T> modelkeys) {
149                modelKeypoints = modelkeys;
150        }
151
152        /**
153         * Set the matching threshold
154         * 
155         * @param thresh
156         *            the threshold
157         */
158        public void setThreshold(int thresh) {
159                this.thresh = thresh;
160        }
161}