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;
31  
32  import java.awt.event.KeyEvent;
33  import java.awt.event.KeyListener;
34  
35  import javax.swing.SwingUtilities;
36  
37  import org.openimaj.demos.video.utils.PolygonDrawingListener;
38  import org.openimaj.demos.video.utils.PolygonExtractionProcessor;
39  import org.openimaj.feature.local.list.LocalFeatureList;
40  import org.openimaj.feature.local.matcher.FastBasicKeypointMatcher;
41  import org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d;
42  import org.openimaj.image.FImage;
43  import org.openimaj.image.MBFImage;
44  import org.openimaj.image.colour.RGBColour;
45  import org.openimaj.image.colour.Transforms;
46  import org.openimaj.image.feature.local.engine.DoGSIFTEngine;
47  import org.openimaj.image.feature.local.keypoints.Keypoint;
48  import org.openimaj.math.geometry.shape.Polygon;
49  import org.openimaj.math.geometry.transforms.HomographyRefinement;
50  import org.openimaj.math.geometry.transforms.MatrixTransformProvider;
51  import org.openimaj.math.geometry.transforms.estimation.RobustHomographyEstimator;
52  import org.openimaj.math.model.fit.RANSAC;
53  import org.openimaj.video.VideoDisplay;
54  import org.openimaj.video.VideoDisplayListener;
55  
56  public class CaptureVideoSIFT implements KeyListener, VideoDisplayListener<MBFImage> {
57  
58  	private VideoWithinVideo vwv;
59  	private PolygonDrawingListener polygonListener;
60  	private DoGSIFTEngine engine;
61  	private VideoDisplay<MBFImage> videoFrame;
62  	private MBFImage modelImage;
63  	private ConsistentLocalFeatureMatcher2d<Keypoint> matcher;
64  	private boolean ransacReader = false;
65  
66  	public CaptureVideoSIFT(VideoWithinVideo videoWithinVideo) {
67  		this.vwv = videoWithinVideo;
68  		polygonListener = new PolygonDrawingListener();
69  		this.vwv.display.getScreen().addMouseListener(polygonListener);
70  		SwingUtilities.getRoot(this.vwv.display.getScreen()).addKeyListener(this);
71  		engine = new DoGSIFTEngine();
72  		engine.getOptions().setDoubleInitialImage(false);
73  
74  		this.videoFrame = VideoDisplay.createOffscreenVideoDisplay(vwv.capture);
75  		this.videoFrame.addVideoListener(this);
76  	}
77  
78  	@Override
79  	public void keyPressed(KeyEvent key) {
80  		if (key.getKeyCode() == KeyEvent.VK_SPACE) {
81  			this.videoFrame.togglePause();
82  		}
83  		else if (key.getKeyChar() == 'r') {
84  			vwv.display.seek(0);
85  		}
86  		else if (key.getKeyChar() == 'c' && this.polygonListener.getPolygon().getVertices().size() > 2) {
87  			try {
88  				ransacReader = false;
89  				final Polygon p = this.polygonListener.getPolygon().clone();
90  				this.polygonListener.reset();
91  				modelImage = this.vwv.capture.getCurrentFrame().process(
92  						new PolygonExtractionProcessor<Float[], MBFImage>(p, RGBColour.BLACK));
93  
94  				// configure the matcher
95  				matcher = new ConsistentLocalFeatureMatcher2d<Keypoint>(new FastBasicKeypointMatcher<Keypoint>(8));
96  				matcher.setFittingModel(new RobustHomographyEstimator(3.0, 1500,
97  						new RANSAC.PercentageInliersStoppingCondition(0.01), HomographyRefinement.NONE));
98  
99  				final DoGSIFTEngine engine = new DoGSIFTEngine();
100 				engine.getOptions().setDoubleInitialImage(false);
101 
102 				final FImage modelF = Transforms.calculateIntensityNTSC(modelImage);
103 				matcher.setModelFeatures(engine.findFeatures(modelF));
104 				vwv.display.seek(0);
105 				ransacReader = true;
106 
107 			} catch (final Exception e) {
108 				e.printStackTrace();
109 			}
110 		}
111 	}
112 
113 	@Override
114 	public void keyReleased(KeyEvent arg0) {
115 	}
116 
117 	@Override
118 	public void keyTyped(KeyEvent arg0) {
119 	}
120 
121 	@Override
122 	public void afterUpdate(VideoDisplay<MBFImage> display) {
123 		if (ransacReader && matcher != null && !videoFrame.isPaused()) {
124 			final MBFImage capImg = videoFrame.getVideo().getCurrentFrame();
125 			final LocalFeatureList<Keypoint> kpl = engine.findFeatures(Transforms.calculateIntensityNTSC(capImg));
126 			if (matcher.findMatches(kpl)) {
127 				try {
128 					final Polygon poly = modelImage.getBounds()
129 							.transform(((MatrixTransformProvider) matcher.getModel()).getTransform().inverse())
130 							.asPolygon();
131 
132 					this.vwv.targetArea = poly;
133 				} catch (final RuntimeException e) {
134 				}
135 
136 			} else {
137 				this.vwv.targetArea = null;
138 			}
139 		}
140 	}
141 
142 	@Override
143 	public void beforeUpdate(MBFImage frame) {
144 		final MBFImage frameWrite = frame;
145 		this.polygonListener.drawPoints(frameWrite);
146 		this.vwv.copyToCaptureFrame(frameWrite);
147 
148 	}
149 
150 }