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.accel;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Map.Entry;
37
38 import org.openimaj.image.DisplayUtilities;
39 import org.openimaj.image.FImage;
40 import org.openimaj.image.ImageUtilities;
41 import org.openimaj.math.geometry.point.Point2d;
42 import org.openimaj.math.geometry.point.Point2dImpl;
43 import org.openimaj.video.ArrayBackedVideo;
44 import org.openimaj.video.processing.motion.GridMotionEstimator;
45 import org.openimaj.video.processing.motion.MotionEstimator;
46 import org.openimaj.video.processing.motion.MotionEstimatorAlgorithm;
47
48 public class Accel {
49 public static void main(String[] args) throws IOException, InterruptedException {
50 final FImage[] sequence = new FImage[3];
51 for (int i = 0; i < sequence.length; i++) {
52 sequence[i] = ImageUtilities.readF(new File("/Users/jon/pendulum+circle/frame_" + (i + 10) + ".png"));
53 }
54
55 final MotionEstimatorAlgorithm.TEMPLATE_MATCH alg = new MotionEstimatorAlgorithm.TEMPLATE_MATCH();
56 final MotionEstimator e = new GridMotionEstimator(new ArrayBackedVideo<FImage>(sequence),
57 alg, 10, 10, true);
58
59 e.getNextFrame();
60 e.getNextFrame();
61 final Map<Point2d, Point2d> mv1 = e.getMotionVectors();
62 e.getNextFrame();
63 final Map<Point2d, Point2d> mv2 = e.getMotionVectors();
64
65 drawVectors(sequence, mv1);
66 drawVectors(sequence, mv2);
67
68 final Map<Point2d, Point2d> accel = new HashMap<Point2d, Point2d>();
69 for (final Entry<Point2d, Point2d> p : mv1.entrySet()) {
70 final Point2d from = p.getKey();
71 final Point2d to = p.getValue().copy();
72 to.translate(from);
73
74 final Point2d v1 = p.getValue();
75 Point2d v2 = mv2.get(to);
76 if (v2 == null)
77 v2 = new Point2dImpl(0, 0);
78
79 final Point2d acc = v2.copy().minus(v1);
80 accel.put(to, acc);
81 }
82
83 drawVectors(sequence, accel);
84 }
85
86 private static void drawVectors(final FImage[] sequence, final Map<Point2d, Point2d> mv1) {
87 final FImage fr = new FImage(sequence[0].width, sequence[0].height);
88 for (final Entry<Point2d, Point2d> p : mv1.entrySet()) {
89 final Point2d from = p.getKey();
90 final Point2d to = p.getValue().copy();
91 to.translate(from);
92
93 if (!from.equals(to))
94 fr.drawLine(from, to, 1f);
95 }
96 DisplayUtilities.display(fr);
97 }
98 }