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.colour.Transforms;
047import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
048import org.openimaj.image.feature.local.keypoints.Keypoint;
049import org.openimaj.image.model.pixel.HistogramPixelModel;
050import org.openimaj.video.VideoDisplay;
051import org.openimaj.video.VideoDisplayListener;
052import org.openimaj.video.capture.VideoCapture;
053
054public class VideoPixelHistogram implements KeyListener, VideoDisplayListener<MBFImage> {
055        VideoCapture capture;
056        VideoDisplay<MBFImage> videoFrame;
057        JFrame modelFrame;
058        JFrame matchFrame;
059        MBFImage modelImage;
060
061        ConsistentLocalFeatureMatcher2d<Keypoint> matcher;
062        private DoGSIFTEngine engine;
063        private PolygonDrawingListener polygonListener;
064        private boolean learnMode = false;
065        private HistogramPixelModel hmodel;
066        private boolean viewMode = false;
067        private List<MBFImage> learningFrames;
068
069        public VideoPixelHistogram() throws Exception {
070                capture = new VideoCapture(640, 480);
071                polygonListener = new PolygonDrawingListener();
072                videoFrame = VideoDisplay.createVideoDisplay(capture);
073                SwingUtilities.getRoot(videoFrame.getScreen()).addKeyListener(this);
074                SwingUtilities.getRoot(videoFrame.getScreen()).addMouseListener(polygonListener);
075                videoFrame.addVideoListener(this);
076                engine = new DoGSIFTEngine();
077                engine.getOptions().setDoubleInitialImage(false);
078                hmodel = new HistogramPixelModel(10,10,10);
079                this.learningFrames = new ArrayList<MBFImage>();
080        }
081
082        @Override
083        public void afterUpdate(VideoDisplay<MBFImage> display) {
084                
085        }
086
087        @Override
088        public void beforeUpdate(MBFImage frame) {
089                if(learnMode){
090                        System.out.println("Adding frame");
091                        if(this.learningFrames.size()>5)
092                                this.learningFrames.remove(0);
093                        this.learningFrames.add(frame.process(new PolygonExtractionProcessor<Float[],MBFImage>(this.polygonListener.getPolygon(),RGBColour.BLACK)));
094                        
095                }
096                if(viewMode){
097                        FImage guess = this.hmodel.classifyImage(frame).normalise();
098                        FImage greyFrame = Transforms.calculateIntensity(frame);
099                        for(int y = 0; y < guess.height; y++){
100                                for(int x = 0; x < guess.width; x++){
101                                        if(guess.pixels[y][x] < 0.1){
102                                                Float greyP = greyFrame.getPixel(x, y);
103                                                frame.setPixel(x, y, new Float[]{greyP,greyP,greyP});
104                                        }
105                                        
106                                }
107                        }
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 VideoPixelHistogram();
152        }
153}