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
35 import org.openimaj.image.DisplayUtilities;
36 import org.openimaj.image.FImage;
37 import org.openimaj.image.ImageUtilities;
38 import org.openimaj.image.processing.convolution.FSobelMagnitude;
39 import org.openimaj.image.processor.Processor;
40
41 public class MovingEdges {
42 FImage[] E;
43 FImage[] totalHeat;
44 FImage[] heatOut;
45
46 public MovingEdges(FImage[] sequence, Processor<FImage> proc, int niters, float kappa) {
47 E = new FImage[sequence.length];
48 heatOut = new FImage[sequence.length - 2];
49 totalHeat = new FImage[sequence.length - 2];
50
51 for (int i = 0; i < sequence.length; i++) {
52 E[i] = sequence[i].process(proc);
53 }
54
55 for (int i = 0; i < sequence.length - 2; i++) {
56 totalHeat[i] = new FImage(E[0].width, E[0].height);
57 heatOut[i] = new FImage(E[0].width, E[0].height);
58 }
59
60 evolve(sequence, niters, kappa);
61 }
62
63 private void evolve(FImage[] sequence, int niters, float kappa) {
64 final int width = E[0].width;
65 final int height = E[0].height;
66
67 for (int i = 0; i < niters; i++) {
68 FImage last = E[0].clone();
69
70 for (int f = 1; f < sequence.length - 1; f++) {
71 final FImage delta = delta(last, E[f], E[f + 1]);
72 last = E[f].clone();
73
74 for (int y = 0; y < height; y++) {
75 for (int x = 0; x < width; x++) {
76 final float d = delta.pixels[y][x];
77 E[f].pixels[y][x] += kappa * d;
78
79 totalHeat[f - 1].pixels[y][x] += Math.abs(kappa * d);
80 if (d < 0)
81 heatOut[f - 1].pixels[y][x] += Math.abs(kappa * d);
82 }
83 }
84 }
85 }
86 }
87
88 private FImage delta(FImage enm, FImage en, FImage enp) {
89 final FImage delta = new FImage(en.width, en.height);
90 for (int y = 0; y < delta.height; y++) {
91 for (int x = 0; x < delta.width; x++) {
92 delta.pixels[y][x] = enp.pixels[y][x] + enm.pixels[y][x] - 2 * en.pixels[y][x];
93 }
94 }
95
96 return delta;
97 }
98
99 public static void main(String[] args) throws IOException, InterruptedException {
100 final FImage[] sequence = new FImage[10];
101 for (int i = 0; i < sequence.length; i++) {
102 sequence[i] = ImageUtilities.readF(new File("/Users/jon/pendulum+circle+notexture/frame_" + i + ".png"));
103 }
104
105 final MovingEdges me = new MovingEdges(sequence, new FSobelMagnitude(), 10, 0.45f);
106
107 for (int i = 0; i < sequence.length - 2; i++) {
108 DisplayUtilities.display(me.heatOut[i].clone().normalise());
109
110 }
111
112
113
114 }
115 }