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.sitestuff;
031
032import java.io.File;
033import java.util.ArrayList;
034import java.util.List;
035
036import org.openimaj.demos.sandbox.image.gif.GifSequenceWriter;
037import org.openimaj.image.DisplayUtilities;
038import org.openimaj.image.Image;
039import org.openimaj.image.MBFImage;
040import org.openimaj.image.colour.ColourSpace;
041import org.openimaj.image.processing.resize.ResizeProcessor;
042import org.openimaj.video.VideoDisplay;
043import org.openimaj.video.VideoDisplayListener;
044import org.openimaj.video.capture.VideoCapture;
045import org.openimaj.video.capture.VideoCaptureException;
046
047public class VideoProcessingSiteDemo {
048        public static void main(String[] args) throws VideoCaptureException {
049                VideoCapture cap = new VideoCapture(640, 480);
050                final List<Image<?,?>> frames = new ArrayList<Image<?,?>>();
051                VideoDisplay.createOffscreenVideoDisplay(cap).addVideoListener(new VideoDisplayListener<MBFImage>() {
052                        MBFImage last;
053                        int nWritten = 0;
054                        @Override
055                        public void beforeUpdate(MBFImage frame) {
056                                if(frame==null) return;
057                                MBFImage combined = new MBFImage(frame.getWidth()*2,frame.getHeight(),ColourSpace.RGB);
058                                combined.drawImage(frame, 0, 0);
059                                if(last != null){
060                                        combined.drawImage(
061                                                frame.subtract(last).abs(), frame.getWidth(),0
062                                        );
063                                }
064                                last = frame.clone();
065                                combined.processInplace(new ResizeProcessor(400, 300));
066                                DisplayUtilities.displayName(combined, "combined");
067                                frames.add(combined);
068                                if(frames.size()%60 == 0){
069                                        try {
070                                                File gifOut = new File("/Users/ss/Desktop/videoProc/"+ "out_" + nWritten + ".gif");
071                                                GifSequenceWriter.writeGif(frames,200, true, gifOut);
072                                                frames.clear();
073                                                System.out.println("GIF written: " + gifOut);
074                                                nWritten++;
075                                        } catch (Exception e) {
076                                        }
077                                }
078                        }
079
080                        @Override
081                        public void afterUpdate(VideoDisplay<MBFImage> display) {
082
083                        }
084                });
085        }
086}