1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 }