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(
051                references = {
052                                @Reference(
053                                                type = ReferenceType.Article,
054                                                author = { "David Lowe" },
055                                                title = "Distinctive image features from scale-invariant keypoints",
056                                                year = "2004",
057                                                journal = "IJCV",
058                                                pages = { "91", "110" },
059                                                month = "January",
060                                                number = "2",
061                                                volume = "60"),
062                                @Reference(
063                                                type = ReferenceType.Inproceedings,
064                                                author = { "David Lowe" },
065                                                title = "Object recognition from local scale-invariant features",
066                                                year = "1999",
067                                                booktitle = "Proc. of the International Conference on Computer Vision {ICCV}",
068                                                pages = { "1150", "1157" })
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 the
077         * scores of the top two best matches must differ in order to count the first as
078         * a good match.
079         *
080         * @param threshold
081         *            (as an integer percentage)
082         */
083        public BasicMatcher(int threshold) {
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                matches = new ArrayList<Pair<T>>();
099
100                /*
101                 * Match the keys in list keys1 to their best matches in keys2.
102                 */
103                for (final T k : keys1) {
104                        final T match = checkForMatch(k, modelKeypoints);
105
106                        if (match != null) {
107                                matches.add(new Pair<T>(k, match));
108                        }
109                }
110
111                return true;
112        }
113
114        /**
115         * This searches through the keypoints in klist for the two closest matches to
116         * key. If the closest is less than <code>threshold</code> times distance to
117         * second closest, then return the closest match. Otherwise, return NULL.
118         */
119        protected T checkForMatch(T query, List<T> features) {
120                double distsq1 = Double.MAX_VALUE, distsq2 = Double.MAX_VALUE;
121                T minkey = null;
122
123                // find two closest matches
124                for (final T target : features) {
125                        final double dsq = target.getFeatureVector().asDoubleFV()
126                                        .compare(query.getFeatureVector().asDoubleFV(), DoubleFVComparison.SUM_SQUARE);
127
128                        if (dsq < distsq1) {
129                                distsq2 = distsq1;
130                                distsq1 = dsq;
131                                minkey = target;
132                        } else if (dsq < distsq2) {
133                                distsq2 = dsq;
134                        }
135                }
136
137                // check the distance against the threshold
138                if (10 * 10 * distsq1 < thresh * thresh * distsq2) {
139                        return minkey;
140                } else
141                        return null;
142        }
143
144        @Override
145        public void setModelFeatures(List<T> modelkeys) {
146                modelKeypoints = modelkeys;
147        }
148
149        /**
150         * Set the matching threshold
151         *
152         * @param thresh
153         *            the threshold
154         */
155        public void setThreshold(int thresh) {
156                this.thresh = thresh;
157        }
158}