001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.workinprogress.accel; 031 032import java.io.File; 033import java.io.IOException; 034 035import org.openimaj.image.DisplayUtilities; 036import org.openimaj.image.FImage; 037import org.openimaj.image.ImageUtilities; 038import org.openimaj.image.processing.convolution.FSobelMagnitude; 039import org.openimaj.image.processor.Processor; 040 041public class MovingEdges { 042 FImage[] E; 043 FImage[] totalHeat; 044 FImage[] heatOut; 045 046 public MovingEdges(FImage[] sequence, Processor<FImage> proc, int niters, float kappa) { 047 E = new FImage[sequence.length]; 048 heatOut = new FImage[sequence.length - 2]; 049 totalHeat = new FImage[sequence.length - 2]; 050 051 for (int i = 0; i < sequence.length; i++) { 052 E[i] = sequence[i].process(proc); 053 } 054 055 for (int i = 0; i < sequence.length - 2; i++) { 056 totalHeat[i] = new FImage(E[0].width, E[0].height); 057 heatOut[i] = new FImage(E[0].width, E[0].height); 058 } 059 060 evolve(sequence, niters, kappa); 061 } 062 063 private void evolve(FImage[] sequence, int niters, float kappa) { 064 final int width = E[0].width; 065 final int height = E[0].height; 066 067 for (int i = 0; i < niters; i++) { 068 FImage last = E[0].clone(); 069 070 for (int f = 1; f < sequence.length - 1; f++) { 071 final FImage delta = delta(last, E[f], E[f + 1]); 072 last = E[f].clone(); 073 074 for (int y = 0; y < height; y++) { 075 for (int x = 0; x < width; x++) { 076 final float d = delta.pixels[y][x]; 077 E[f].pixels[y][x] += kappa * d; 078 079 totalHeat[f - 1].pixels[y][x] += Math.abs(kappa * d); 080 if (d < 0) 081 heatOut[f - 1].pixels[y][x] += Math.abs(kappa * d); 082 } 083 } 084 } 085 } 086 } 087 088 private FImage delta(FImage enm, FImage en, FImage enp) { 089 final FImage delta = new FImage(en.width, en.height); 090 for (int y = 0; y < delta.height; y++) { 091 for (int x = 0; x < delta.width; x++) { 092 delta.pixels[y][x] = enp.pixels[y][x] + enm.pixels[y][x] - 2 * en.pixels[y][x]; 093 } 094 } 095 096 return delta; 097 } 098 099 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}