1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.openimaj.video.processing.shotdetector;
34
35 import org.openimaj.citation.annotation.Reference;
36 import org.openimaj.citation.annotation.ReferenceType;
37 import org.openimaj.feature.DoubleFVComparison;
38 import org.openimaj.image.MBFImage;
39 import org.openimaj.image.colour.RGBColour;
40 import org.openimaj.image.pixel.statistics.HistogramModel;
41 import org.openimaj.math.geometry.shape.Rectangle;
42 import org.openimaj.util.array.ArrayUtils;
43 import org.openimaj.video.Video;
44
45
46
47
48
49
50
51
52 @Reference(
53 type = ReferenceType.Inproceedings,
54 author = { "{S}teiner, {T}homas", "{V}erborgh, {R}uben", "{G}abarr{\'o} {V}all{\'e}s, {J}oaquim", "{T}roncy, {R}apha{\"e}l", "{H}ausenblas, {M}ichael", "{V}an de {W}alle, {R}ik", "{B}rousseau, {A}rnaud" },
55 title = "{E}nabling on-the-fly video shot detection on {Y}ou{T}ube",
56 year = "2012",
57 booktitle = "{WWW} 2012, 21st {I}nternational {W}orld {W}ide {W}eb {C}onference {D}eveloper's {T}rack, {A}pril 16-20, 2012, {L}yon, {F}rance",
58 url = "http://www.eurecom.fr/publication/3676",
59 month = "04",
60 customData = {
61 "address", "{L}yon, {FRANCE}"
62 }
63 )
64 public class LocalHistogramVideoShotDetector
65 extends VideoShotDetector<MBFImage>
66 {
67
68 private final HistogramModel histogramModel = new HistogramModel( 4,4,4 );
69
70
71 private double[][][] lastHistogram = null;
72
73
74 private int nGridElements = 20;
75
76
77 private final double pcMostSimilar = 0.1;
78
79
80 private final double pcMostDissimilar = 0.1;
81
82
83 private final double boostFactor = 1.1;
84
85
86 private final double limitingFactor = 0.9;
87
88
89
90
91
92
93
94
95 public LocalHistogramVideoShotDetector( final int nGridElements )
96 {
97 this.nGridElements = nGridElements;
98 this.lastHistogram = new double[this.nGridElements][this.nGridElements][];
99 this.threshold = 0.2;
100 }
101
102
103
104
105
106
107
108 public LocalHistogramVideoShotDetector( final Video<MBFImage> video,
109 final int nGridElements )
110 {
111 super( video );
112 this.nGridElements = nGridElements;
113 this.lastHistogram = new double[nGridElements][nGridElements][];
114 this.threshold = 0.2;
115 }
116
117
118
119
120
121 @Override
122 protected double getInterframeDistance( final MBFImage frame )
123 {
124
125 final double[][] avgHisto = new double[this.nGridElements][this.nGridElements];
126
127
128 final int gw = frame.getWidth() / this.nGridElements;
129 final int gh = frame.getHeight() / this.nGridElements;
130
131
132 for( int y = 0; y < this.nGridElements; y++ )
133 {
134 for( int x = 0; x < this.nGridElements; x++ )
135 {
136
137 final MBFImage img = frame.extractROI( x*gw, y*gh, gw, gh );
138
139
140 this.histogramModel.estimateModel( img );
141 final double[] histogram = this.histogramModel.
142 histogram.asDoubleVector();
143
144
145
146 if( this.lastHistogram[y][x] != null )
147 {
148 final double dist = DoubleFVComparison.EUCLIDEAN.compare(
149 histogram, this.lastHistogram[y][x] );
150 avgHisto[y][x] = dist;
151 }
152
153
154 this.lastHistogram[y][x] = histogram;
155 }
156
157 }
158
159
160 double[] flattenedAvgHisto = ArrayUtils.reshape( avgHisto );
161
162
163
164 final int[] indices = new int[this.nGridElements*this.nGridElements];
165
166
167
168 ArrayUtils.parallelQuicksortDescending( flattenedAvgHisto,
169 ArrayUtils.fill( indices ) );
170
171
172 final double[][] similarDissimilar =
173 new double[this.nGridElements][this.nGridElements];
174
175
176 for( int index = 0; index < indices.length; index++ )
177 {
178 double factor = 1;
179 if( index < this.nGridElements * this.nGridElements * this.pcMostDissimilar )
180 factor = this.limitingFactor;
181 else
182 if( index >= this.nGridElements * this.nGridElements * (1-this.pcMostSimilar) )
183 factor = this.boostFactor;
184 else factor = 1;
185
186 final int y = indices[index] / this.nGridElements;
187 final int x = indices[index] % this.nGridElements;
188 similarDissimilar[y][x] = factor;
189 }
190
191
192
193
194
195 for( int y = 0; y < this.nGridElements; y++ )
196 for( int x = 0; x < this.nGridElements; x++ )
197 avgHisto[y][x] *= similarDissimilar[y][x];
198
199
200 flattenedAvgHisto = ArrayUtils.reshape( avgHisto );
201 double avgDist = ArrayUtils.sumValues( flattenedAvgHisto );
202 avgDist /= this.nGridElements * this.nGridElements;
203
204
205 ArrayUtils.subtract( flattenedAvgHisto, avgDist );
206 final double stdDev = Math.sqrt( ArrayUtils.sumValuesSquared(
207 flattenedAvgHisto ) / (this.nGridElements*this.nGridElements) );
208
209 return stdDev;
210 }
211
212
213
214
215
216 protected void drawBoxes( final MBFImage img, final double[][] sim )
217 {
218 final int gw = img.getWidth() / this.nGridElements;
219 final int gh = img.getHeight() / this.nGridElements;
220 for( int y = 0; y < this.nGridElements; y++ )
221 {
222 for( int x = 0; x < this.nGridElements; x++ )
223 {
224 Float[] c = new Float[]{0f,0f,0f,0f};
225 if( sim[y][x] == this.boostFactor )
226 c = RGBColour.RED;
227 else
228 if( sim[y][x] == this.limitingFactor )
229 c = RGBColour.BLUE;
230 else c = RGBColour.BLACK;
231 img.drawShape( new Rectangle(x*gw,y*gh,gw,gh), c );
232 }
233 }
234 }
235 }