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.demos.sitestuff;
031
032import java.io.File;
033import java.io.IOException;
034
035import org.openimaj.feature.local.list.LocalFeatureList;
036import org.openimaj.feature.local.matcher.FastBasicKeypointMatcher;
037import org.openimaj.feature.local.matcher.MatchingUtilities;
038import org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d;
039import org.openimaj.image.DisplayUtilities;
040import org.openimaj.image.FImage;
041import org.openimaj.image.ImageUtilities;
042import org.openimaj.image.MBFImage;
043import org.openimaj.image.colour.RGBColour;
044import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
045import org.openimaj.image.feature.local.keypoints.Keypoint;
046import org.openimaj.image.processing.resize.ResizeProcessor;
047import org.openimaj.math.geometry.point.Point2d;
048import org.openimaj.math.geometry.shape.Shape;
049import org.openimaj.math.geometry.transforms.HomographyModel;
050import org.openimaj.math.geometry.transforms.residuals.SingleImageTransferResidual2d;
051import org.openimaj.math.model.fit.RANSAC;
052
053import Jama.Matrix;
054
055public class KeypointMatchSiteDemo {
056        static File monaLisaSource = new File("/Users/ss/Desktop/Van-Gogh-Starry-Nights.jpg");
057        static File monaLisaTarget = new File(
058                        "/Users/ss/Desktop/4172349-A_SPECIAL_EXHIBITION_VAN_GOGHS_STARRY_NIGHT_New_Haven.jpg");
059
060        public static void main(String[] args) throws IOException {
061                final MBFImage sourceC = ImageUtilities.readMBF(monaLisaSource);
062                final MBFImage targetC = ImageUtilities.readMBF(monaLisaTarget);
063                final FImage source = sourceC.flatten();
064                final FImage target = targetC.flatten();
065
066                final DoGSIFTEngine eng = new DoGSIFTEngine();
067
068                final LocalFeatureList<Keypoint> sourceFeats = eng.findFeatures(source);
069                final LocalFeatureList<Keypoint> targetFeats = eng.findFeatures(target);
070
071                final HomographyModel model = new HomographyModel();
072                final SingleImageTransferResidual2d<HomographyModel> errorModel = new SingleImageTransferResidual2d<HomographyModel>();
073                final RANSAC<Point2d, Point2d, HomographyModel> ransac = new RANSAC<Point2d, Point2d, HomographyModel>(model,
074                                errorModel, 5f, 1500, new RANSAC.BestFitStoppingCondition(), true);
075                final ConsistentLocalFeatureMatcher2d<Keypoint> matcher = new ConsistentLocalFeatureMatcher2d<Keypoint>(
076                                new FastBasicKeypointMatcher<Keypoint>(8));
077                matcher.setFittingModel(ransac);
078
079                matcher.setModelFeatures(sourceFeats);
080                matcher.findMatches(targetFeats);
081
082                final Matrix boundsToPoly = model.getTransform().inverse();
083
084                final Shape s = source.getBounds().transform(boundsToPoly);
085                targetC.drawShape(s, 10, RGBColour.BLUE);
086                final MBFImage matches = MatchingUtilities.drawMatches(sourceC, targetC, matcher.getMatches(), RGBColour.RED);
087
088                matches.processInplace(new ResizeProcessor(640, 480));
089                DisplayUtilities.display(matches);
090                ImageUtilities.write(matches, new File("/Users/ss/Desktop/keypoint-match-example.png"));
091
092        }
093}