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.video.processing.tracking; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 import org.openimaj.image.FImage; 36 import org.openimaj.math.geometry.shape.Rectangle; 37 import org.openimaj.video.tracking.klt.FeatureList; 38 import org.openimaj.video.tracking.klt.KLTTracker; 39 import org.openimaj.video.tracking.klt.TrackingContext; 40 41 /** 42 * A tracker that will track one rectangular region using the KLTTracker. 43 * 44 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 45 * 46 * @created 13 Oct 2011 47 */ 48 public class BasicObjectTracker implements ObjectTracker<Rectangle,FImage> 49 { 50 /** The tracking context for the KLTTracker */ 51 private TrackingContext trackingContext = new TrackingContext(); 52 53 /** The feature list used for the tracking */ 54 private FeatureList featureList = null; 55 56 /** The number of features found during the initialisation stage of the tracking */ 57 private int featuresFound = -1; 58 59 /** The tracker used to track the faces */ 60 private KLTTracker tracker = null; 61 62 /** The accuracy to use when tracking, between 0 and 1 */ 63 private double accuracy = 0.5; 64 65 /** The previous frame */ 66 private FImage previousFrame; 67 68 /** 69 * Default constructor that will use 50 features and an accuracy of 0.5. 70 */ 71 public BasicObjectTracker() 72 { 73 this( 50, 0.5 ); 74 } 75 76 /** 77 * Default constructor that takes the number of features to be used. 78 * Will use an accuracy of 0.5. 79 * 80 * @param nFeatures The number of features to use. 81 */ 82 public BasicObjectTracker( int nFeatures ) 83 { 84 this( nFeatures, 0.5 ); 85 } 86 87 /** 88 * Constructor that takes the accuracy to use for tracking. Will use 50 89 * features. 90 * 91 * @param accuracy The accuracy to use. 92 */ 93 public BasicObjectTracker( double accuracy ) 94 { 95 this( 50, accuracy ); 96 } 97 98 /** 99 * 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 }