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.sitestuff;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import org.openimaj.demos.sandbox.image.gif.GifSequenceWriter;
37  import org.openimaj.image.DisplayUtilities;
38  import org.openimaj.image.Image;
39  import org.openimaj.image.MBFImage;
40  import org.openimaj.image.colour.ColourSpace;
41  import org.openimaj.image.processing.resize.ResizeProcessor;
42  import org.openimaj.video.VideoDisplay;
43  import org.openimaj.video.VideoDisplayListener;
44  import org.openimaj.video.capture.VideoCapture;
45  import org.openimaj.video.capture.VideoCaptureException;
46  
47  public class VideoProcessingSiteDemo {
48  	public static void main(String[] args) throws VideoCaptureException {
49  		VideoCapture cap = new VideoCapture(640, 480);
50  		final List<Image<?,?>> frames = new ArrayList<Image<?,?>>();
51  		VideoDisplay.createOffscreenVideoDisplay(cap).addVideoListener(new VideoDisplayListener<MBFImage>() {
52  			MBFImage last;
53  			int nWritten = 0;
54  			@Override
55  			public void beforeUpdate(MBFImage frame) {
56  				if(frame==null) return;
57  				MBFImage combined = new MBFImage(frame.getWidth()*2,frame.getHeight(),ColourSpace.RGB);
58  				combined.drawImage(frame, 0, 0);
59  				if(last != null){
60  					combined.drawImage(
61  						frame.subtract(last).abs(), frame.getWidth(),0
62  					);
63  				}
64  				last = frame.clone();
65  				combined.processInplace(new ResizeProcessor(400, 300));
66  				DisplayUtilities.displayName(combined, "combined");
67  				frames.add(combined);
68  				if(frames.size()%60 == 0){
69  					try {
70  						File gifOut = new File("/Users/ss/Desktop/videoProc/"+ "out_" + nWritten + ".gif");
71  						GifSequenceWriter.writeGif(frames,200, true, gifOut);
72  						frames.clear();
73  						System.out.println("GIF written: " + gifOut);
74  						nWritten++;
75  					} catch (Exception e) {
76  					}
77  				}
78  			}
79  
80  			@Override
81  			public void afterUpdate(VideoDisplay<MBFImage> display) {
82  
83  			}
84  		});
85  	}
86  }