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;
034import java.util.PriorityQueue;
035import java.util.Queue;
036
037import org.openimaj.image.feature.local.keypoints.Keypoint;
038import org.openimaj.knn.approximate.ByteNearestNeighboursKDTree;
039import org.openimaj.util.pair.Pair;
040
041
042/**
043 * Basic keypoint matcher. Matches keypoints by finding closest
044 * Two keypoints to target and checking whether the distance
045 * between the two matches is sufficiently large. Allows for a match limit to be specified
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
049 *
050 * @param <T>
051 */
052public class FastLimitedEuclideanKeypointMatcher<T extends Keypoint> implements LocalFeatureMatcher<T> {
053        private ByteNearestNeighboursKDTree modelKeypointsKNN;
054        private int limit;
055        private List<Pair<T>> matches;
056        private List<T> modelKeypoints;
057        
058        /**
059         * Number of matches allowed
060         * @param limit
061         */
062        public FastLimitedEuclideanKeypointMatcher(int limit) {
063                this.limit = limit;
064        }
065
066        @Override
067        public void setModelFeatures(List<T> modelkeys) {
068                modelKeypoints = modelkeys;
069                
070                byte [][] data = new byte[modelkeys.size()][];
071                for (int i=0; i<modelkeys.size(); i++)
072                        data[i] = modelkeys.get(i).ivec;
073                
074                modelKeypointsKNN = new ByteNearestNeighboursKDTree(data, 8, 768);
075        }
076
077        class WPair extends Pair<T> implements Comparable<WPair> {
078                float weight;
079
080                public WPair(T obj1, T obj2, float weight) {
081                        super(obj1, obj2);
082                        this.weight = weight;
083                }
084
085                @Override
086                public int compareTo(WPair o) {
087                        if (weight == o.weight) return 0;
088                        if (weight < o.weight) return -1;
089                        return 1;
090                }
091        }
092        
093        @Override
094        public boolean findMatches(List<T> keys1) {
095                Queue<WPair> mq = new PriorityQueue<WPair>();
096                
097                byte [][] data = new byte[keys1.size()][];
098                for (int i=0; i<keys1.size(); i++)
099                        data[i] = keys1.get(i).ivec;
100                
101                int [] argmins = new int[keys1.size()];
102                float [] mins = new float[keys1.size()];
103                modelKeypointsKNN.searchNN(data, argmins, mins);
104                
105                for (int i=0; i<keys1.size(); i++) {
106                        float distsq = mins[i];
107                        
108                        mq.add(new WPair(keys1.get(i), modelKeypoints.get(argmins[i]), distsq));
109                }
110                
111                matches = new ArrayList<Pair<T>>(limit);
112                for (int i=0; i<limit; i++) matches.add(mq.poll());
113                
114            return true;
115        }
116
117        @Override
118        public List<Pair<T>> getMatches() {
119                return matches;
120        }
121
122}