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 */
030/**
031 *
032 */
033package org.openimaj.vis.video;
034
035import java.util.List;
036
037import org.openimaj.image.MBFImage;
038import org.openimaj.image.colour.RGBColour;
039import org.openimaj.math.geometry.point.Point2d;
040import org.openimaj.math.geometry.point.Point2dImpl;
041import org.openimaj.math.geometry.point.PointList;
042import org.openimaj.time.Timecode;
043import org.openimaj.video.Video;
044
045/**
046 *      A visualisation that is designed to show the position of an object over
047 *      the course of the video. The trackObject method is called during processing
048 *      so that subclasses can track whatever object it is that they wish to track.
049 *      This should return a set of object coordinates that can be used to update
050 *      the visualisation later. The type of position visualisation can be chosen.
051 *
052 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
053 *  @created 8 Feb 2013
054 *      @version $Author$, $Revision$, $Date$
055 */
056public abstract class VideoObjectVisualisation extends VideoBarVisualisation
057{
058        /**
059         *
060         *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
061         *  @created 8 Feb 2013
062         *      @version $Author$, $Revision$, $Date$
063         */
064        public static enum DrawType
065        {
066                /** Draw the point list as a point cloud */
067                DOTS
068                {
069                        @Override
070                        public void draw( final MBFImage vis, final int xoffset,
071                                        final int yoffset, final PointList pl )
072                        {
073                                for( final Point2d p : pl )
074                                {
075                                        final Point2d pp = new Point2dImpl( p );
076                                        p.translate( xoffset, yoffset );
077                                        vis.drawPoint( pp, RGBColour.RED, 2 );
078                                }
079                        }
080                };
081
082                /**
083                 *      Draw the point list into the given visualisation at the given offset.
084                 *      @param vis The visualisation
085                 *      @param xoffset The offset
086                 *      @param yoffset The offset
087                 *      @param pl The pointlist
088                 */
089                public abstract void draw( final MBFImage vis,
090                                final int xoffset, int yoffset, PointList pl );
091        }
092
093        /** We store the positions of all the objects as we go so we can redraw them */
094        private final List<PointList> objectPositions = null;
095
096        /** The height of the video frame */
097        private int frameHeight = 0;
098
099        /** The type of point drawing to use */
100        private final DrawType drawType = DrawType.DOTS;
101
102        /** */
103        private static final long serialVersionUID = 1L;
104
105        /**
106         *
107         *      @param video
108         */
109        protected VideoObjectVisualisation( final Video<MBFImage> video )
110        {
111                super( video );
112
113                this.frameHeight = video.getHeight();
114        }
115
116        /**
117         *      Tracks an object within the frame and returns a set of points
118         *      that somehow delineate the object or give its position. If there is no
119         *      object to track in the given frame return null and this will be dealt
120         *      with correctly by the visualisation. The point positions should be
121         *      in terms of the frame size and they will be resized to fit within
122         *      the visualisation.
123         *
124         *      @param frame The frame
125         *      @return A {@link PointList} representing the object position
126         */
127        public abstract PointList trackObject( final MBFImage frame );
128
129        /**
130         *      {@inheritDoc}
131         *      @see org.openimaj.vis.video.VideoBarVisualisation#processFrame(org.openimaj.image.MBFImage, org.openimaj.time.Timecode)
132         */
133        @Override
134        public void processFrame( final MBFImage frame, final Timecode t )
135        {
136                this.objectPositions.add( this.trackObject( frame ) );
137        }
138
139        /**
140         *      {@inheritDoc}
141         *      @see org.openimaj.vis.video.VideoBarVisualisation#updateVis(org.openimaj.image.MBFImage)
142         */
143        @Override
144        public void updateVis( final MBFImage vis )
145        {
146                final float scalar = vis.getHeight() / this.frameHeight;
147
148                // Redraw each of the positions.
149                int frame = 0;
150                for( final PointList pos : this.objectPositions )
151                {
152                        final PointList pp = new PointList( pos.points, true );
153                        pp.scale( scalar );
154                        this.drawType.draw( vis, (int)this.getTimePosition( frame ), 0, pp );
155                        frame++;
156                }
157        }
158}