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;
034import java.util.HashMap;
035import java.util.Map;
036import java.util.Map.Entry;
037
038import org.openimaj.image.DisplayUtilities;
039import org.openimaj.image.FImage;
040import org.openimaj.image.ImageUtilities;
041import org.openimaj.math.geometry.point.Point2d;
042import org.openimaj.math.geometry.point.Point2dImpl;
043import org.openimaj.video.ArrayBackedVideo;
044import org.openimaj.video.processing.motion.GridMotionEstimator;
045import org.openimaj.video.processing.motion.MotionEstimator;
046import org.openimaj.video.processing.motion.MotionEstimatorAlgorithm;
047
048public class Accel {
049        public static void main(String[] args) throws IOException, InterruptedException {
050                final FImage[] sequence = new FImage[3];
051                for (int i = 0; i < sequence.length; i++) {
052                        sequence[i] = ImageUtilities.readF(new File("/Users/jon/pendulum+circle/frame_" + (i + 10) + ".png"));
053                }
054
055                final MotionEstimatorAlgorithm.TEMPLATE_MATCH alg = new MotionEstimatorAlgorithm.TEMPLATE_MATCH();
056                final MotionEstimator e = new GridMotionEstimator(new ArrayBackedVideo<FImage>(sequence),
057                                alg, 10, 10, true);
058
059                e.getNextFrame();
060                e.getNextFrame();
061                final Map<Point2d, Point2d> mv1 = e.getMotionVectors();
062                e.getNextFrame();
063                final Map<Point2d, Point2d> mv2 = e.getMotionVectors();
064
065                drawVectors(sequence, mv1);
066                drawVectors(sequence, mv2);
067
068                final Map<Point2d, Point2d> accel = new HashMap<Point2d, Point2d>();
069                for (final Entry<Point2d, Point2d> p : mv1.entrySet()) {
070                        final Point2d from = p.getKey();
071                        final Point2d to = p.getValue().copy();
072                        to.translate(from);
073
074                        final Point2d v1 = p.getValue();
075                        Point2d v2 = mv2.get(to);
076                        if (v2 == null)
077                                v2 = new Point2dImpl(0, 0);
078
079                        final Point2d acc = v2.copy().minus(v1);
080                        accel.put(to, acc);
081                }
082
083                drawVectors(sequence, accel);
084        }
085
086        private static void drawVectors(final FImage[] sequence, final Map<Point2d, Point2d> mv1) {
087                final FImage fr = new FImage(sequence[0].width, sequence[0].height);
088                for (final Entry<Point2d, Point2d> p : mv1.entrySet()) {
089                        final Point2d from = p.getKey();
090                        final Point2d to = p.getValue().copy();
091                        to.translate(from);
092
093                        if (!from.equals(to))
094                                fr.drawLine(from, to, 1f);
095                }
096                DisplayUtilities.display(fr);
097        }
098}