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.workinprogress;
31
32 import java.io.File;
33
34 import org.openimaj.feature.local.list.LocalFeatureList;
35 import org.openimaj.feature.local.list.MemoryLocalFeatureList;
36 import org.openimaj.feature.local.matcher.FastBasicKeypointMatcher;
37 import org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d;
38 import org.openimaj.image.DisplayUtilities;
39 import org.openimaj.image.FImage;
40 import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
41 import org.openimaj.image.feature.local.keypoints.Keypoint;
42 import org.openimaj.image.processing.resize.ResizeProcessor;
43 import org.openimaj.math.geometry.point.Point2d;
44 import org.openimaj.math.geometry.point.Point2dImpl;
45 import org.openimaj.math.geometry.transforms.estimation.RobustAffineTransformEstimator;
46 import org.openimaj.video.capture.VideoCapture;
47
48 public class Snap {
49 MemoryLocalFeatureList<Keypoint> mapData = new MemoryLocalFeatureList<Keypoint>();
50 File[] mapKeypointFiles = {
51 new File("/Users/jon/Consulting/MapSnapper/map_data/MAP_DATA_2K/NY42.key"),
52 new File("/Users/jon/Consulting/MapSnapper/map_data/MAP_DATA_2K/NY44.key"),
53 new File("/Users/jon/Consulting/MapSnapper/map_data/MAP_DATA_2K/NY46.key")
54 };
55 int[] baseNorthing = { 200, 400, 600 };
56 int[] baseEasting = { 400, 400, 400 };
57
58
59 int scaleFactor = 100;
60
61
62
63 int mapDimension = 2000;
64 private DoGSIFTEngine engine;
65 private ConsistentLocalFeatureMatcher2d<Keypoint> matcher;
66
67 public Snap() {
68 loadMapData();
69
70 engine = new DoGSIFTEngine();
71
72
73 final FastBasicKeypointMatcher<Keypoint> innerMatcher = new FastBasicKeypointMatcher<Keypoint>(8);
74 matcher = new ConsistentLocalFeatureMatcher2d<Keypoint>(innerMatcher);
75 final RobustAffineTransformEstimator estimator = new RobustAffineTransformEstimator(0.5);
76 matcher.setFittingModel(estimator);
77 matcher.setModelFeatures(mapData);
78
79 }
80
81 protected void loadMapData() {
82 for (int i = 0; i < mapKeypointFiles.length; i++) {
83 try {
84
85 final MemoryLocalFeatureList<Keypoint> map = MemoryLocalFeatureList.read(mapKeypointFiles[i],
86 Keypoint.class);
87
88
89
90
91 for (final Keypoint k : map) {
92 final int east = baseEasting[i] + Math.round(10.0f * (k.getX() / scaleFactor));
93 final int north = baseNorthing[i]
94 + Math.round(10.0f * ((mapDimension - k.getY()) / scaleFactor));
95
96
97 k.setX(east);
98 k.setY(north);
99 }
100
101 mapData.addAll(map);
102 } catch (final Exception ex) {
103 System.out.println(ex);
104 System.exit(1);
105 }
106 }
107 }
108
109
110
111
112
113
114
115
116
117 public String getGridRef(FImage image) throws Exception {
118 final LocalFeatureList<Keypoint> keys = engine.findFeatures(image);
119 if (matcher.findMatches(keys)) {
120 System.out.println("Done! -- Found Match");
121
122 final Point2d coords = matcher.getModel().predict(
123 new Point2dImpl(image.width / 2.0f, image.height / 2.0f));
124
125 final int east = Math.round(coords.getX());
126 final int north = Math.round(coords.getY());
127
128 final Object[] gr = { "NY", east, north };
129 return String.format("%2s%3d%3d\n", gr);
130 } else {
131 return "Match Not Found";
132 }
133 }
134
135 public static void main(String[] args) throws Exception {
136 final Snap snap = new Snap();
137 final VideoCapture vc = new VideoCapture(640, 480);
138
139 while (true) {
140 final FImage img = vc.getNextFrame().flatten();
141 DisplayUtilities.displayName(img, "Live Video");
142 final FImage patch = ResizeProcessor.resample(img, 160, 120);
143
144 final String res = snap.getGridRef(patch);
145 if (!res.contains("Not"))
146 System.out.println(res);
147 }
148 }
149 }