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.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 			// DisplayUtilities.display(me.totalHeat[i].clone().normalise());
110 		}
111 		// for (int i = 0; i < sequence.length; i++) {
112 		// DisplayUtilities.display(me.E[i].clone().normalise());
113 		// }
114 	}
115 }