View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.demos.sitestuff;
31  
32  import java.io.File;
33  import java.io.IOException;
34  
35  import org.openimaj.feature.local.list.LocalFeatureList;
36  import org.openimaj.feature.local.matcher.FastBasicKeypointMatcher;
37  import org.openimaj.feature.local.matcher.MatchingUtilities;
38  import org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d;
39  import org.openimaj.image.DisplayUtilities;
40  import org.openimaj.image.FImage;
41  import org.openimaj.image.ImageUtilities;
42  import org.openimaj.image.MBFImage;
43  import org.openimaj.image.colour.RGBColour;
44  import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
45  import org.openimaj.image.feature.local.keypoints.Keypoint;
46  import org.openimaj.image.processing.resize.ResizeProcessor;
47  import org.openimaj.math.geometry.point.Point2d;
48  import org.openimaj.math.geometry.shape.Shape;
49  import org.openimaj.math.geometry.transforms.HomographyModel;
50  import org.openimaj.math.geometry.transforms.residuals.SingleImageTransferResidual2d;
51  import org.openimaj.math.model.fit.RANSAC;
52  
53  import Jama.Matrix;
54  
55  public class KeypointMatchSiteDemo {
56  	static File monaLisaSource = new File("/Users/ss/Desktop/Van-Gogh-Starry-Nights.jpg");
57  	static File monaLisaTarget = new File(
58  			"/Users/ss/Desktop/4172349-A_SPECIAL_EXHIBITION_VAN_GOGHS_STARRY_NIGHT_New_Haven.jpg");
59  
60  	public static void main(String[] args) throws IOException {
61  		final MBFImage sourceC = ImageUtilities.readMBF(monaLisaSource);
62  		final MBFImage targetC = ImageUtilities.readMBF(monaLisaTarget);
63  		final FImage source = sourceC.flatten();
64  		final FImage target = targetC.flatten();
65  
66  		final DoGSIFTEngine eng = new DoGSIFTEngine();
67  
68  		final LocalFeatureList<Keypoint> sourceFeats = eng.findFeatures(source);
69  		final LocalFeatureList<Keypoint> targetFeats = eng.findFeatures(target);
70  
71  		final HomographyModel model = new HomographyModel();
72  		final SingleImageTransferResidual2d<HomographyModel> errorModel = new SingleImageTransferResidual2d<HomographyModel>();
73  		final RANSAC<Point2d, Point2d, HomographyModel> ransac = new RANSAC<Point2d, Point2d, HomographyModel>(model,
74  				errorModel, 5f, 1500, new RANSAC.BestFitStoppingCondition(), true);
75  		final ConsistentLocalFeatureMatcher2d<Keypoint> matcher = new ConsistentLocalFeatureMatcher2d<Keypoint>(
76  				new FastBasicKeypointMatcher<Keypoint>(8));
77  		matcher.setFittingModel(ransac);
78  
79  		matcher.setModelFeatures(sourceFeats);
80  		matcher.findMatches(targetFeats);
81  
82  		final Matrix boundsToPoly = model.getTransform().inverse();
83  
84  		final Shape s = source.getBounds().transform(boundsToPoly);
85  		targetC.drawShape(s, 10, RGBColour.BLUE);
86  		final MBFImage matches = MatchingUtilities.drawMatches(sourceC, targetC, matcher.getMatches(), RGBColour.RED);
87  
88  		matches.processInplace(new ResizeProcessor(640, 480));
89  		DisplayUtilities.display(matches);
90  		ImageUtilities.write(matches, new File("/Users/ss/Desktop/keypoint-match-example.png"));
91  
92  	}
93  }