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 java.util.ArrayList;
35  import java.util.List;
36  
37  import javax.swing.JFrame;
38  import javax.swing.SwingUtilities;
39  
40  import org.openimaj.demos.video.utils.PolygonDrawingListener;
41  import org.openimaj.demos.video.utils.PolygonExtractionProcessor;
42  import org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d;
43  import org.openimaj.image.FImage;
44  import org.openimaj.image.MBFImage;
45  import org.openimaj.image.colour.RGBColour;
46  import org.openimaj.image.colour.Transforms;
47  import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
48  import org.openimaj.image.feature.local.keypoints.Keypoint;
49  import org.openimaj.image.model.pixel.HistogramPixelModel;
50  import org.openimaj.video.VideoDisplay;
51  import org.openimaj.video.VideoDisplayListener;
52  import org.openimaj.video.capture.VideoCapture;
53  
54  public class VideoPixelHistogram implements KeyListener, VideoDisplayListener<MBFImage> {
55  	VideoCapture capture;
56  	VideoDisplay<MBFImage> videoFrame;
57  	JFrame modelFrame;
58  	JFrame matchFrame;
59  	MBFImage modelImage;
60  
61  	ConsistentLocalFeatureMatcher2d<Keypoint> matcher;
62  	private DoGSIFTEngine engine;
63  	private PolygonDrawingListener polygonListener;
64  	private boolean learnMode = false;
65  	private HistogramPixelModel hmodel;
66  	private boolean viewMode = false;
67  	private List<MBFImage> learningFrames;
68  
69  	public VideoPixelHistogram() throws Exception {
70  		capture = new VideoCapture(640, 480);
71  		polygonListener = new PolygonDrawingListener();
72  		videoFrame = VideoDisplay.createVideoDisplay(capture);
73  		SwingUtilities.getRoot(videoFrame.getScreen()).addKeyListener(this);
74  		SwingUtilities.getRoot(videoFrame.getScreen()).addMouseListener(polygonListener);
75  		videoFrame.addVideoListener(this);
76  		engine = new DoGSIFTEngine();
77  		engine.getOptions().setDoubleInitialImage(false);
78  		hmodel = new HistogramPixelModel(10,10,10);
79  		this.learningFrames = new ArrayList<MBFImage>();
80  	}
81  
82  	@Override
83  	public void afterUpdate(VideoDisplay<MBFImage> display) {
84  		
85  	}
86  
87  	@Override
88  	public void beforeUpdate(MBFImage frame) {
89  		if(learnMode){
90  			System.out.println("Adding frame");
91  			if(this.learningFrames.size()>5)
92  				this.learningFrames.remove(0);
93  			this.learningFrames.add(frame.process(new PolygonExtractionProcessor<Float[],MBFImage>(this.polygonListener.getPolygon(),RGBColour.BLACK)));
94  			
95  		}
96  		if(viewMode){
97  			FImage guess = this.hmodel.classifyImage(frame).normalise();
98  			FImage greyFrame = Transforms.calculateIntensity(frame);
99  			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 }