View Javadoc

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.demos.video.videosift;
31  
32  import java.awt.event.KeyEvent;
33  import java.awt.event.KeyListener;
34  import javax.swing.SwingUtilities;
35  
36  import org.openimaj.image.FImage;
37  import org.openimaj.image.MBFImage;
38  import org.openimaj.image.colour.Transforms;
39  import org.openimaj.video.VideoDisplay;
40  import org.openimaj.video.VideoDisplayListener;
41  import org.openimaj.video.capture.VideoCapture;
42  import org.openimaj.video.tracking.klt.FeatureList;
43  import org.openimaj.video.tracking.klt.FeatureTable;
44  import org.openimaj.video.tracking.klt.KLTTracker;
45  import org.openimaj.video.tracking.klt.TrackingContext;
46  
47  public class VideoKLT implements KeyListener, VideoDisplayListener<MBFImage> {
48  	
49  	private VideoCapture capture;
50  	private VideoDisplay<MBFImage> videoFrame;
51  	private KLTTracker tracker;
52  	private FeatureTable ft;
53  	private FeatureList fl;
54  	
55  	boolean firstFrame = true;
56  	private FImage oldFrame;
57  	private int frameNumber = 0;
58  	private int nFeatures = 150;
59  	private int nOriginalFoundFeatures = -1;
60  	public VideoKLT() throws Exception{
61  		capture = new VideoCapture(640, 480);
62  		videoFrame = VideoDisplay.createVideoDisplay(capture);
63  		videoFrame.addVideoListener(this);
64  		SwingUtilities.getRoot(videoFrame.getScreen()).addKeyListener(this);
65  		// videoFrame.getScreen().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66  		
67  		TrackingContext tc = new TrackingContext();
68  		fl = new FeatureList(nFeatures );
69  		ft = new FeatureTable(nFeatures);
70  		tracker = new KLTTracker(tc, fl);
71  		
72  		tc.setSequentialMode(true);
73  		tc.setWriteInternalImages(false);
74  		tc.setAffineConsistencyCheck(-1);  /* set this to 2 to turn on affine consistency check */
75  	}
76  	
77  	public boolean needsReset(){
78  		return this.firstFrame;
79  	}
80  	
81  	@Override
82  	public void afterUpdate(VideoDisplay<MBFImage> arg0) {
83  		// TODO Auto-generated method stub
84  		
85  	}
86  
87  	@Override
88  	public void beforeUpdate(MBFImage image) {
89  		FImage greyFrame = Transforms.calculateIntensityNTSC(image);
90  		if(needsReset()){
91  
92  			frameNumber = 0;
93  			tracker.selectGoodFeatures(greyFrame);
94  			ft.storeFeatureList(fl, frameNumber);
95  			nOriginalFoundFeatures  = fl.countRemainingFeatures();
96  		}
97  		else{
98  			tracker.trackFeatures(oldFrame, greyFrame);
99  			if(fl.countRemainingFeatures() <= nOriginalFoundFeatures  * 0.5)
100 			{
101 				tracker.replaceLostFeatures(greyFrame);
102 				nOriginalFoundFeatures  = fl.countRemainingFeatures();
103 			}
104 			ft.storeFeatureList(fl, frameNumber);
105 		}
106 		fl.drawFeatures(image);
107 		this.oldFrame = greyFrame;
108 		this.firstFrame = false;
109 		this.frameNumber++;
110 		
111 	}
112 
113 	@Override
114 	public void keyTyped(KeyEvent e) {
115 		
116 	}
117 
118 	@Override
119 	public void keyPressed(KeyEvent e) {
120 		System.out.println(e.getKeyChar());
121 		if(e.getKeyChar() == 'r'){
122 			this.firstFrame = true;
123 		}
124 	}
125 
126 	@Override
127 	public void keyReleased(KeyEvent e) {
128 		// TODO Auto-generated method stub
129 		
130 	}
131 	
132 	public static void main(String args[]) throws Exception{
133 		new VideoKLT();
134 	}
135 
136 }