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 /**
31 *
32 */
33 package org.openimaj.video.processing.shotdetector;
34
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import org.openimaj.image.MBFImage;
39 import org.openimaj.video.Video;
40
41 /**
42 * A shot detector that uses evidence from a number of shot detectors
43 * to determine if a shot has been detected.
44 * <p>
45 * It runs the shot detection for each detector that is registered with the
46 * class and if it determines that a shot boundary occurred the score is increased
47 * by the weighting value for that detector. The final score is divided by
48 * the number of detectors to get a probability value for the frame being
49 * a shot boundary. By default the threshold is set to 0.75 - that is, there must
50 * be a 3/4 correlation of shot boundary for it to be considered a shot boundary.
51 *
52 * @author David Dupplaw (dpd@ecs.soton.ac.uk)
53 * @created 28 Jan 2013
54 * @version $Author$, $Revision$, $Date$
55 */
56 public class CombiShotDetector extends VideoShotDetector<MBFImage>
57 {
58 /** The detectors to use for evidence gathering */
59 private Map<VideoShotDetector<MBFImage>,Double> detectors = null;
60
61 /**
62 * If you use this constructor, your timecodes will be messed up
63 * unless you call {@link #setFPS(double)} before you process
64 * any frames.
65 */
66 public CombiShotDetector()
67 {
68 this.detectors = new HashMap<VideoShotDetector<MBFImage>, Double>();
69 this.threshold = 0.75;
70 }
71
72 /**
73 * Default constructor that takes the video to be processed
74 * @param video The video
75 */
76 public CombiShotDetector( final Video<MBFImage> video )
77 {
78 super( video );
79 this.detectors = new HashMap<VideoShotDetector<MBFImage>, Double>();
80 this.threshold = 0.75;
81 }
82
83 /**
84 * Add a shot detector that will be used in the evidence gathering.
85 * @param detector The detector
86 * @param weight The weight to use for this detector
87 */
88 public void addVideoShotDetector( final VideoShotDetector<MBFImage> detector,
89 final double weight )
90 {
91 if( weight > 1 || weight < 0 )
92 throw new IllegalArgumentException(
93 "Detector weight must be between "+
94 "0 and 1 inclusive" );
95 this.detectors.put( detector, weight );
96 }
97
98 /**
99 * {@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 }