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.video.processing.tracking;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import org.openimaj.image.FImage;
036import org.openimaj.math.geometry.shape.Rectangle;
037import org.openimaj.video.tracking.klt.FeatureList;
038import org.openimaj.video.tracking.klt.KLTTracker;
039import org.openimaj.video.tracking.klt.TrackingContext;
040
041/**
042 *      A tracker that will track one rectangular region using the KLTTracker.
043 * 
044 *  @author David Dupplaw (dpd@ecs.soton.ac.uk)
045 *      
046 *      @created 13 Oct 2011
047 */
048public class BasicObjectTracker implements ObjectTracker<Rectangle,FImage>
049{               
050        /** The tracking context for the KLTTracker */
051        private TrackingContext trackingContext = new TrackingContext();
052        
053        /** The feature list used for the tracking */
054        private FeatureList featureList = null;
055        
056        /** The number of features found during the initialisation stage of the tracking */
057        private int featuresFound = -1;
058        
059        /** The tracker used to track the faces */
060        private KLTTracker tracker = null;
061
062        /** The accuracy to use when tracking, between 0 and 1 */
063        private double accuracy = 0.5;
064
065        /** The previous frame */
066        private FImage previousFrame;
067        
068        /**
069         *      Default constructor that will use 50 features and an accuracy of 0.5.
070         */
071        public BasicObjectTracker()
072    {
073                this( 50, 0.5 );
074    }
075        
076        /**
077         *      Default constructor that takes the number of features to be used.
078         *      Will use an accuracy of 0.5.
079         * 
080         *      @param nFeatures The number of features to use.
081         */
082        public BasicObjectTracker( int nFeatures )
083        {
084                this( nFeatures, 0.5 );
085        }
086        
087        /**
088         *      Constructor that takes the accuracy to use for tracking. Will use 50
089         *      features.
090         * 
091         *      @param accuracy The accuracy to use.
092         */
093        public BasicObjectTracker( double accuracy )
094        {
095                this( 50, accuracy );
096        }
097        
098        /**
099         *      Constructor that takes the number of features to use and the accuracy
100         *      for tracking.
101         * 
102         *      @param nFeatures The number of features to use.
103         *      @param accuracy The accuracy to use
104         */
105        public BasicObjectTracker( int nFeatures, double accuracy )
106        {
107                this.featureList = new FeatureList( nFeatures );
108                this.accuracy  = accuracy;
109                tracker = new KLTTracker( trackingContext, featureList );
110        }
111        
112        /**
113         *      Reset this tracker using the given image
114         *      @return TRUE if the tracking continued ok; FALSE otherwise
115         */
116        @Override
117        public List<Rectangle> trackObject( FImage img )
118        {
119                List<Rectangle> trackedObjects = new ArrayList<Rectangle>();
120                
121                tracker.trackFeatures( previousFrame, img );
122                
123                // If we're losing features left-right and centre then we say
124                // we've lost the object we're tracking
125                if( featureList.countRemainingFeatures() <= featuresFound * accuracy )
126                        return trackedObjects;
127                
128                trackedObjects.add( featureList.getBounds() );
129                
130                previousFrame = img;
131                
132                return trackedObjects;
133        }
134
135        /**
136         *      Initialise this tracker with a particular area on a particular
137         *      image.
138         * 
139         *  @param bounds The area to track
140         *  @param img The image
141         */
142        @Override
143        public List<Rectangle> initialiseTracking( Rectangle bounds, FImage img )
144    {
145                List<Rectangle> initialObjects = new ArrayList<Rectangle>();
146                
147                // Set the tracking area to be the face found
148                trackingContext.setTargetArea( bounds );
149                
150                // Select the good features from the area
151                tracker.selectGoodFeatures( img );
152
153                // Remember how many features we found, so that if we
154                // start to lose them, we can re-initialise the tracking
155                featuresFound = featureList.countRemainingFeatures();
156
157                // Add the initial bounds as the found object
158                initialObjects.add( bounds );
159                
160                previousFrame = img;
161        
162        return initialObjects;
163    }
164        
165        /**
166         *      Returns the list of features that the tracker has been tracking.
167         *      @return the {@link FeatureList}
168         */
169        public FeatureList getFeatureList()
170        {
171                return featureList;
172        }
173}