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.FImage;
36  import org.openimaj.image.ImageUtilities;
37  import org.openimaj.image.processing.algorithm.FourierTransform;
38  
39  import edu.emory.mathcs.jtransforms.fft.FloatFFT_3D;
40  
41  public class VideoSpectrogram {
42  	public static void main(String[] args) throws IOException {
43  		final FImage[] sequence = new FImage[20];
44  		for (int i = 0; i < sequence.length; i++) {
45  			sequence[i] = ImageUtilities.readF(new File("/Users/jon/pendulum+circle+notexture/frame_" + i + ".png"));
46  		}
47  
48  		final int width = sequence[0].width;
49  		final int height = sequence[0].height;
50  
51  		final int windowSize = 10;
52  		final float[][][][] spectrogram = new float[sequence.length - windowSize][][][];
53  
54  		for (int w = 0; w < spectrogram.length; w++) {
55  			final FloatFFT_3D fft = new FloatFFT_3D(windowSize, height, width);
56  			final float[][][] data = new float[10][][];
57  			for (int i = 0; i < windowSize; i++)
58  				data[i] = FourierTransform.prepareData(sequence[i], height, width, false);
59  			fft.complexForward(data);
60  
61  			spectrogram[w] = data;
62  		}
63  
64  		for (int i = 1; i < spectrogram.length - 2; i++) {
65  			for (int j = 1; j < spectrogram[0].length - 2; j++) {
66  				for (int k = 1; k < spectrogram[0][0].length - 2; k++) {
67  					for (int l = 1; l < (spectrogram[0][0][0].length / 2) - 2; l++) {
68  						final float centre = (float) Math.sqrt(spectrogram[i][j][k][l * 2] * spectrogram[i][j][k][l * 2]
69  								+ spectrogram[i][j][k][l * 2 + 1] * spectrogram[i][j][k][l * 2 + 1]);
70  						boolean max = true;
71  
72  						for (int ii = -1; ii <= 1; ii++) {
73  							for (int jj = -1; jj <= 1; jj++) {
74  								for (int kk = -1; kk <= 1; kk++) {
75  									for (int ll = -1; ll <= 1; ll++) {
76  										if (i != 0 && j != 0 && k != 0 && l != 0) {
77  											final float curr = (float) Math
78  													.sqrt(spectrogram[i + ii][j + jj][k + kk][(l + ll) * 2]
79  															* spectrogram[i + ii][j + jj][k + kk][(l + ll) * 2]
80  															+ spectrogram[i + ii][j + jj][k + kk][(l + ll) * 2 + 1]
81  															* spectrogram[i + ii][j + jj][k + kk][(l + ll) * 2 + 1]);
82  
83  											if (curr > centre) {
84  												max = false;
85  											}
86  										}
87  									}
88  								}
89  							}
90  						}
91  
92  						if (max)
93  							System.out.println("max " + i + " " + j + " " + k + " " + l);
94  					}
95  				}
96  			}
97  		}
98  	}
99  }