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.image.feature.local.keypoints.Keypoint;
039import org.openimaj.knn.approximate.ByteNearestNeighboursKDTree;
040import org.openimaj.util.pair.Pair;
041
042/**
043 * Basic keypoint matcher. Matches keypoints by finding closest Two keypoints to
044 * target and checking whether the distance between the two matches is
045 * sufficiently large.
046 * <p>
047 * This is the method for determining matches suggested by Lowe in the original
048 * SIFT papers.
049 * 
050 * @author Jonathon Hare
051 * @param <T>
052 *            The type of keypoint
053 */
054@References(references = {
055                @Reference(
056                                type = ReferenceType.Article,
057                                author = { "David Lowe" },
058                                title = "Distinctive image features from scale-invariant keypoints",
059                                year = "2004",
060                                journal = "IJCV",
061                                pages = { "91", "110" },
062                                month = "January",
063                                number = "2",
064                                volume = "60"),
065                @Reference(
066                                type = ReferenceType.Inproceedings,
067                                author = { "David Lowe" },
068                                title = "Object recognition from local scale-invariant features",
069                                year = "1999",
070                                booktitle = "Proc. of the International Conference on Computer Vision {ICCV}",
071                                pages = { "1150", "1157" }
072                )
073})
074public class FastBasicKeypointMatcher<T extends Keypoint> extends BasicMatcher<T> {
075        protected ByteNearestNeighboursKDTree modelKeypointsKNN;
076
077        /**
078         * Construct with a threshold of 8, corresponding to the 0.8 in Lowe's IJCV
079         * paper
080         */
081        public FastBasicKeypointMatcher()
082        {
083                super(8);
084        }
085
086        /**
087         * 
088         * @param threshold
089         *            threshold for determining matching keypoints
090         */
091        public FastBasicKeypointMatcher(int threshold)
092        {
093                super(threshold);
094        }
095
096        /**
097         * Given a pair of images and their keypoints, pick the first keypoint from
098         * one image and find its closest match in the second set of keypoints. Then
099         * write the result to a file.
100         */
101        @Override
102        public boolean findMatches(List<T> keys1)
103        {
104                matches = new ArrayList<Pair<T>>();
105
106                final byte[][] data = new byte[keys1.size()][];
107                for (int i = 0; i < keys1.size(); i++)
108                        data[i] = keys1.get(i).ivec;
109
110                final int[][] argmins = new int[keys1.size()][2];
111                final float[][] mins = new float[keys1.size()][2];
112                modelKeypointsKNN.searchKNN(data, 2, argmins, mins);
113
114                for (int i = 0; i < keys1.size(); i++) {
115                        final float distsq1 = mins[i][0];
116                        final float distsq2 = mins[i][1];
117
118                        if (10 * 10 * distsq1 < thresh * thresh * distsq2) {
119                                matches.add(new Pair<T>(keys1.get(i), modelKeypoints.get(argmins[i][0])));
120                        }
121                }
122
123                return true;
124        }
125
126        @Override
127        public void setModelFeatures(List<T> modelkeys) {
128                modelKeypoints = modelkeys;
129
130                final byte[][] data = new byte[modelkeys.size()][];
131                for (int i = 0; i < modelkeys.size(); i++)
132                        data[i] = modelkeys.get(i).ivec;
133
134                modelKeypointsKNN = new ByteNearestNeighboursKDTree(data, 1, 100);
135        }
136}