- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

media.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
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 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. 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 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
33
34 #ifndef __INCLUDE_MEDIA_CUT_H__
35 #define __INCLUDE_MEDIA_CUT_H__
36
37
38 #ifndef __INCLUDE_MIST_CONF_H__
39 #include "config/mist_conf.h"
40 #endif
41
42 #ifndef __INCLUDE_MIST_H__
43 #include "mist.h"
44 #endif
45
46 #ifndef __INCLUDE_MIST_COLOR_H__
47 #include "config/color.h"
48 #endif
49
50 #ifndef __INCLUDE_MIST_COLOR_H__
51 #include "config/color.h"
52 #endif
53
54 #ifndef __INCLUDE_IO_VIDEO_H__
55 #include "io/video.h"
56 #endif
57
58 #include <vector>
59
60
61 // mist名前空間の始まり
62 _MIST_BEGIN
63
64 namespace __media_utility__
65 {
66 template < class Array >
67 void __compute_histogram__( const Array &img, array3< double > &hist )
68 {
69 typedef typename Array::size_type size_type;
70
71 hist.fill( );
72
73 size_type binR = 256 / hist.size1( );
74 size_type binG = 256 / hist.size2( );
75 size_type binB = 256 / hist.size3( );
76
77 for( size_type i = 0 ; i < img.size( ) ; i++ )
78 {
79 hist( img[ i ].r / binR, img[ i ].g / binG, img[ i ].b / binB ) += 1.0;
80 }
81
82 for( size_type i = 0 ; i < hist.size( ) ; i++ )
83 {
84 hist[ i ] /= static_cast< double >( img.size( ) );
85 }
86 }
87
88 template < class Histogram >
89 double __compute_similarity__( const Histogram &hist1, const Histogram &hist2 )
90 {
91 double ave1 = 1.0 / hist1.size( ), ave2 = 1.0 / hist2.size( );
92 double r = 0.0, s1 = 0.0, s2 = 0.0;
93
94 for( size_t i = 0 ; i < hist1.size( ) ; i++ )
95 {
96 double h1 = hist1[ i ];
97 double h2 = hist2[ i ];
98 r += ( h1 - ave1 ) * ( h2 - ave2 );
99 s1 += ( h1 - ave1 ) * ( h1 - ave1 );
100 s2 += ( h2 - ave2 ) * ( h2 - ave2 );
101 }
102
103 return ( r / std::sqrt( s1 * s2 ) );
104 }
105 }
106
109
112
113
182 bool cut_detection( video::decoder &iv, std::vector< int > &frame_indeces, size_t red_bin = 8, size_t green_bin = 8, size_t blue_bin = 8, double threshold_of_similarity = 0.95, ptrdiff_t number_of_between_frames = 30 )
183 {
184 typedef video::decoder::size_type size_type;
185 typedef video::decoder::difference_type difference_type;
186 typedef rgb< unsigned char > pixel_type;
187 typedef array2< pixel_type > image_type;
188 typedef array3< double > histogram_type;
189
190 difference_type frameNo = 0;
191 image_type img;
192
193 // 先頭フレームを読み込む
194 if( !iv.read( img, frameNo ) )
195 {
196 return( false );
197 }
198
199 const difference_type num_playback = 5;
200 histogram_type h[ 2 ], *hh = new histogram_type[ num_playback ];
201 double osim = 1.0;
202
203 h[ 0 ].resize( red_bin, green_bin, blue_bin );
204 h[ 1 ].resize( red_bin, green_bin, blue_bin );
205
206 for( difference_type i = 0 ; i < num_playback ; i++ )
207 {
208 hh[ i ].resize( red_bin, green_bin, blue_bin );
209 }
210
211 frame_indeces.push_back( static_cast< int >( frameNo ) );
212
213 __media_utility__::__compute_histogram__( img, h[ 0 ] );
214 hh[ 0 ] = h[ 0 ];
215
216 for( difference_type i = 0 ; !iv.is_eof( ) ; i++ )
217 {
218 if( !iv.read( img, frameNo ) )
219 {
220 break;
221 }
222
223 histogram_type &ch = h[ ( i + 1 ) % 2 ];
224 __media_utility__::__compute_histogram__( img, ch );
225 double sim1 = __media_utility__::__compute_similarity__( h[ 0 ], h[ 1 ] );
226 double sim2 = 1.0, sim = sim1;
227
228 if( i >= num_playback - 1 )
229 {
230 sim2 = __media_utility__::__compute_similarity__( ch, hh[ ( i + 1 ) % num_playback ] );
231 sim = ( sim1 + sim2 ) * 0.5;
232 }
233
234 hh[ ( i + 1 ) % num_playback ] = ch;
235
236 if( sim1 < threshold_of_similarity || sim2 < threshold_of_similarity )
237 {
238 difference_type oindx = frame_indeces.back( );
239 difference_type nindx = frameNo;
240 if( nindx < oindx + number_of_between_frames )
241 {
242 if( sim < osim )
243 {
244 frame_indeces.back( ) = static_cast< int >( nindx );
245 osim = sim;
246 }
247 }
248 else
249 {
250 frame_indeces.push_back( static_cast< int >( nindx ) );
251 osim = sim;
252 }
253 }
254 }
255
256 delete [] hh;
257
258 frame_indeces.push_back( static_cast< int >( iv.frame_id( ) ) );
259
260 return( true );
261 }
262
264 // メディア処理に関するグループの終わり
265
267 // ビデオファイルの入出力を行うライブラリの終わり
268
269
270 // mist名前空間の終わり
271 _MIST_END
272
273
274 #endif // __INCLUDE_MEDIA_CUT_H__
275