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.demos.video.videosift;
031
032import java.awt.event.KeyEvent;
033import java.awt.event.KeyListener;
034import javax.swing.SwingUtilities;
035
036import org.openimaj.image.FImage;
037import org.openimaj.image.MBFImage;
038import org.openimaj.image.colour.Transforms;
039import org.openimaj.video.VideoDisplay;
040import org.openimaj.video.VideoDisplayListener;
041import org.openimaj.video.capture.VideoCapture;
042import org.openimaj.video.tracking.klt.FeatureList;
043import org.openimaj.video.tracking.klt.FeatureTable;
044import org.openimaj.video.tracking.klt.KLTTracker;
045import org.openimaj.video.tracking.klt.TrackingContext;
046
047public class VideoKLT implements KeyListener, VideoDisplayListener<MBFImage> {
048        
049        private VideoCapture capture;
050        private VideoDisplay<MBFImage> videoFrame;
051        private KLTTracker tracker;
052        private FeatureTable ft;
053        private FeatureList fl;
054        
055        boolean firstFrame = true;
056        private FImage oldFrame;
057        private int frameNumber = 0;
058        private int nFeatures = 150;
059        private int nOriginalFoundFeatures = -1;
060        public VideoKLT() throws Exception{
061                capture = new VideoCapture(640, 480);
062                videoFrame = VideoDisplay.createVideoDisplay(capture);
063                videoFrame.addVideoListener(this);
064                SwingUtilities.getRoot(videoFrame.getScreen()).addKeyListener(this);
065                // videoFrame.getScreen().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
066                
067                TrackingContext tc = new TrackingContext();
068                fl = new FeatureList(nFeatures );
069                ft = new FeatureTable(nFeatures);
070                tracker = new KLTTracker(tc, fl);
071                
072                tc.setSequentialMode(true);
073                tc.setWriteInternalImages(false);
074                tc.setAffineConsistencyCheck(-1);  /* set this to 2 to turn on affine consistency check */
075        }
076        
077        public boolean needsReset(){
078                return this.firstFrame;
079        }
080        
081        @Override
082        public void afterUpdate(VideoDisplay<MBFImage> arg0) {
083                // TODO Auto-generated method stub
084                
085        }
086
087        @Override
088        public void beforeUpdate(MBFImage image) {
089                FImage greyFrame = Transforms.calculateIntensityNTSC(image);
090                if(needsReset()){
091
092                        frameNumber = 0;
093                        tracker.selectGoodFeatures(greyFrame);
094                        ft.storeFeatureList(fl, frameNumber);
095                        nOriginalFoundFeatures  = fl.countRemainingFeatures();
096                }
097                else{
098                        tracker.trackFeatures(oldFrame, greyFrame);
099                        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}