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 */
030/**
031 *
032 */
033package org.openimaj.video.processing.shotdetector;
034
035import java.util.HashMap;
036import java.util.Map;
037
038import org.openimaj.image.MBFImage;
039import org.openimaj.video.Video;
040
041/**
042 *      A shot detector that uses evidence from a number of shot detectors
043 *      to determine if a shot has been detected.
044 *      <p>
045 *      It runs the shot detection for each detector that is registered with the
046 *      class and if it determines that a shot boundary occurred the score is increased
047 *      by the weighting value for that detector.  The final score is divided by
048 *      the number of detectors to get a probability value for the frame being
049 *      a shot boundary. By default the threshold is set to 0.75 - that is, there must
050 *      be a 3/4 correlation of shot boundary for it to be considered a shot boundary.
051 *
052 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
053 *  @created 28 Jan 2013
054 *      @version $Author$, $Revision$, $Date$
055 */
056public class CombiShotDetector extends VideoShotDetector<MBFImage>
057{
058        /** The detectors to use for evidence gathering */
059        private Map<VideoShotDetector<MBFImage>,Double> detectors = null;
060
061        /**
062         *      If you use this constructor, your timecodes will be messed up
063         *      unless you call {@link #setFPS(double)} before you process
064         *      any frames.
065         */
066        public CombiShotDetector()
067        {
068                this.detectors = new HashMap<VideoShotDetector<MBFImage>, Double>();
069                this.threshold = 0.75;
070        }
071
072        /**
073         *      Default constructor that takes the video to be processed
074         *      @param video The video
075         */
076        public CombiShotDetector( final Video<MBFImage> video )
077        {
078                super( video );
079                this.detectors = new HashMap<VideoShotDetector<MBFImage>, Double>();
080                this.threshold = 0.75;
081        }
082
083        /**
084         *      Add a shot detector that will be used in the evidence gathering.
085         *      @param detector The detector
086         *      @param weight The weight to use for this detector
087         */
088        public void addVideoShotDetector( final VideoShotDetector<MBFImage> detector,
089                        final double weight )
090        {
091                if( weight > 1 || weight < 0 )
092                        throw new IllegalArgumentException(
093                                        "Detector weight must be between "+
094                                        "0 and 1 inclusive" );
095                this.detectors.put( detector, weight );
096        }
097
098        /**
099         *      {@inheritDoc}
100         *      @see org.openimaj.video.processing.shotdetector.VideoShotDetector#getInterframeDistance(org.openimaj.image.Image)
101         */
102        @Override
103        protected double getInterframeDistance( final MBFImage thisFrame )
104        {
105                double score = 0;
106                for( final VideoShotDetector<MBFImage> detector : this.detectors.keySet() )
107                {
108                        detector.processFrame( thisFrame );
109                        if( detector.wasLastFrameBoundary() )
110                                score += this.detectors.get(detector);
111                }
112
113                return score/this.detectors.size();
114        }
115}