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 java.util.ArrayList;
035import java.util.List;
036
037import javax.swing.JFrame;
038import javax.swing.SwingUtilities;
039
040import org.openimaj.demos.video.utils.PolygonDrawingListener;
041import org.openimaj.demos.video.utils.PolygonExtractionProcessor;
042import org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d;
043import org.openimaj.image.FImage;
044import org.openimaj.image.MBFImage;
045import org.openimaj.image.colour.RGBColour;
046import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
047import org.openimaj.image.feature.local.keypoints.Keypoint;
048import org.openimaj.image.model.patch.HistogramPatchModel;
049import org.openimaj.video.VideoDisplay;
050import org.openimaj.video.VideoDisplayListener;
051import org.openimaj.video.capture.VideoCapture;
052
053public class VideoPatchHistogram implements KeyListener, VideoDisplayListener<MBFImage> {
054        VideoCapture capture;
055        VideoDisplay<MBFImage> videoFrame;
056        JFrame modelFrame;
057        JFrame matchFrame;
058        MBFImage modelImage;
059
060        ConsistentLocalFeatureMatcher2d<Keypoint> matcher;
061        private DoGSIFTEngine engine;
062        private PolygonDrawingListener polygonListener;
063        private boolean learnMode = false;
064        private HistogramPatchModel hmodel;
065        private boolean viewMode = false;
066        private List<MBFImage> learningFrames;
067
068        public VideoPatchHistogram() throws Exception {
069                capture = new VideoCapture(640, 480);
070                polygonListener = new PolygonDrawingListener();
071                videoFrame = VideoDisplay.createVideoDisplay(capture);
072                SwingUtilities.getRoot(videoFrame.getScreen()).addKeyListener(this);
073                videoFrame.getScreen().addMouseListener(polygonListener);
074                videoFrame.addVideoListener(this);
075                engine = new DoGSIFTEngine();
076                engine.getOptions().setDoubleInitialImage(false);
077                hmodel = new HistogramPatchModel(10,10,10);
078                this.learningFrames = new ArrayList<MBFImage>();
079        }
080
081        @Override
082        public void afterUpdate(VideoDisplay<MBFImage> display) {
083                
084        }
085
086        @Override
087        public void beforeUpdate(MBFImage frame) {
088                if(learnMode){
089                        System.out.println("Adding frame");
090                        if(this.learningFrames.size()>5)
091                                this.learningFrames.remove(0);
092                        this.learningFrames.add(frame.process(new PolygonExtractionProcessor<Float[],MBFImage>(this.polygonListener.getPolygon(),RGBColour.BLACK)));
093                        
094                }
095                if(viewMode){
096                        FImage guess = this.hmodel.classifyImage(frame).normalise();
097//                      FImage greyFrame = Transforms.calculateIntensity(frame);
098//                      for(int y = 0; y < guess.height; y++){
099//                              for(int x = 0; x < guess.width; x++){
100//                                      if(guess.pixels[y][x] < 0.1){
101//                                              Float greyP = greyFrame.getPixel(x, y);
102//                                              frame.setPixel(x, y, new Float[]{greyP,greyP,greyP});
103//                                      }
104//                                      
105//                              }
106//                      }
107                        frame.internalAssign(new MBFImage(new FImage[]{guess, guess, guess}));
108                }
109                this.polygonListener.drawPoints(frame);
110                
111        }
112
113        @Override
114        public void keyPressed(KeyEvent key) {
115                if(key.getKeyCode() == KeyEvent.VK_SPACE) {
116                        this.videoFrame.togglePause();
117                }
118                if (key.getKeyChar() == 'v' ) {
119                                this.viewMode = !this.viewMode ;
120                }
121                if (key.getKeyChar() == 'c' && this.polygonListener.getPolygon().getVertices().size()>2) {
122                        try {
123                                if(!this.learnMode)
124                                {
125                                        this.learnMode  = true;
126                                }
127                                else{
128                                        this.polygonListener.reset();
129                                        this.hmodel.learnModel(this.learningFrames.toArray(new MBFImage[this.learningFrames.size()]));
130                                        this.learnMode = false;
131                                }
132
133                        } catch (Exception e) {
134                                e.printStackTrace();
135                        } 
136                }
137        }
138
139        @Override
140        public void keyReleased(KeyEvent arg0) {
141                // TODO Auto-generated method stub
142                
143        }
144
145        @Override
146        public void keyTyped(KeyEvent arg0) {
147                // TODO Auto-generated method stub
148                
149        }
150        public static void main(String args[]) throws Exception{
151                new VideoPatchHistogram();
152        }
153}