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

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

threshold.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 #ifndef __INCLUDE_MIST_THRESHOLD__
34 #define __INCLUDE_MIST_THRESHOLD__
35
36
37 #ifndef __INCLUDE_MIST_H__
38 #include "mist.h"
39 #endif
40
41 #ifndef __INCLUDE_MIST_LIMITS__
42 #include "limits.h"
43 #endif
44
45
46 // mist名前空間の始まり
47 _MIST_BEGIN
48
49
50
57
59
60
62 namespace ptile
63 {
73 template < class Array >
74 typename Array::value_type threshold( const Array &in, double ratio )
75 {
76 typedef typename Array::size_type size_type;
77 typedef typename Array::value_type value_type;
78
79 if( in.empty( ) )
80 {
81 return( value_type( 0 ) );
82 }
83
84 value_type min = in[ 0 ];
85 value_type max = in[ 0 ];
86
87 size_type i;
88
89 for( i = 1 ; i < in.size( ) ; i++ )
90 {
91 if( min > in[ i ] )
92 {
93 min = in[ i ];
94 }
95 else if( max < in[ i ] )
96 {
97 max = in[ i ];
98 }
99 }
100
101 size_type level = static_cast< size_type >( max - min ) + 1;
102 size_type *hist = new size_type[ level ];
103 size_type N = in.size( );
104
105 for( i = 0 ; i < level ; i++ )
106 {
107 hist[ i ] = 0;
108 }
109 for( i = 0 ; i < N ; i++ )
110 {
111 hist[ static_cast< size_type >( in[ i ] - min ) ]++;
112 }
113
114 size_type pix_num = 0;
115 size_type th = 0;
116
117 while( ( pix_num * 100.0 ) / static_cast< double >( N ) < ratio )
118 {
119 pix_num += hist[ th ];
120 th++;
121 }
122
123 delete [] hist;
124
125 return( static_cast< value_type >( th + min ) );
126 }
127
138 template < class Array, class Mask >
139 typename Array::value_type threshold( const Array &in, const Mask &mask, double ratio )
140 {
141 typedef typename Array::size_type size_type;
142 typedef typename Array::value_type value_type;
143
144 if( in.empty( ) )
145 {
146 return( value_type( 0 ) );
147 }
148
149 value_type min = type_limits< value_type >::maximum( );
150 value_type max = type_limits< value_type >::minimum( );
151
152 size_type i, count = 0;
153
154 for( i = 0 ; i < in.size( ) ; i++ )
155 {
156 if( mask[ i ] != 0 )
157 {
158 count++;
159 if( min > in[ i ] )
160 {
161 min = in[ i ];
162 }
163 if( max < in[ i ] )
164 {
165 max = in[ i ];
166 }
167 }
168 }
169
170 if( count == 0 )
171 {
172 return( value_type( 0 ) );
173 }
174
175 size_type level = static_cast< size_type >( max - min ) + 1;
176 size_type *hist = new size_type[ level ];
177
178 for( i = 0 ; i < level ; i++ )
179 {
180 hist[ i ] = 0;
181 }
182 for( i = 0 ; i < in.size( ) ; i++ )
183 {
184 if( mask[ i ] != 0 )
185 {
186 hist[ static_cast< size_type >( in[ i ] - min ) ]++;
187 }
188 }
189
190 size_type pix_num = 0;
191 size_type th = 0;
192
193 while( ( pix_num * 100.0 ) / static_cast< double >( count ) < ratio )
194 {
195 pix_num += hist[ th ];
196 th++;
197 }
198
199 delete [] hist;
200
201 return( static_cast< value_type >( th + min ) );
202 }
203 }
204
205
206
208 namespace discriminant_analysis
209 {
216 template < class T, class Allocator >
217 typename array< T, Allocator >::value_type threshold( const array< T, Allocator > &in )
218 {
219 typedef typename array< T, Allocator >::size_type size_type;
220 typedef typename array< T, Allocator >::value_type value_type;
221
222 if( in.empty( ) )
223 {
224 return( value_type( 0 ) );
225 }
226
227 value_type min = in[ 0 ];
228 value_type max = in[ 0 ];
229
230 size_type i, k;
231
232 for( i = 1 ; i < in.size( ) ; i++ )
233 {
234 if( min > in[ i ] )
235 {
236 min = in[ i ];
237 }
238 else if( max < in[ i ] )
239 {
240 max = in[ i ];
241 }
242 }
243
244 size_type level = static_cast< size_type >( max - min ) + 1;
245 size_type max_k;
246 double *p = new double[ level ];
247 double myu, omg;
248 double myuT, sig, max_sig = 0.0;
249
250 size_type N = in.size( );
251 for( k = 0 ; k < level ; k++ )
252 {
253 p[ k ] = 0.0;
254 }
255 for( k = 0 ; k < N ; k++ )
256 {
257 p[ static_cast< size_type >( in[ k ] - min ) ]++;
258 }
259 for( k = 0 ; k < level ; k++ )
260 {
261 p[ k ] /= static_cast< double >( N );
262 }
263
264 myuT = 0.0;
265 for( k = 0 ; k < level ; k++ )
266 {
267 myuT += k * p[ k ];
268 }
269
270 myu = 0.0;
271 omg = p[ 0 ];
272 max_k = 0;
273 max_sig = ( myuT * omg - myu ) * ( myuT * omg - myu ) / ( omg * ( 1.0 - omg ) );
274 for( k = 1 ; k < level ; k++ )
275 {
276 omg = omg + p[ k ];
277 myu = myu + k * p[ k ];
278 sig = ( myuT * omg - myu ) * ( myuT * omg - myu ) / ( omg * ( 1.0 - omg ) );
279 if( sig > max_sig )
280 {
281 max_sig = sig;
282 max_k = k;
283 }
284 }
285
286 delete [] p;
287
288 return( static_cast< value_type >( max_k + min ) );
289 }
290
298 template < class T1, class Allocator1, class T2, class Allocator2 >
299 typename array< T1, Allocator1 >::value_type threshold( const array< T1, Allocator1 > &in, const array< T2, Allocator2 > &mask )
300 {
301 typedef typename array< T1, Allocator1 >::size_type size_type;
302 typedef typename array< T1, Allocator1 >::value_type value_type;
303
304 if( in.empty( ) || in.size( ) != mask.size( ) )
305 {
306 return( value_type( 0 ) );
307 }
308
309 value_type min = type_limits< value_type >::maximum( );
310 value_type max = type_limits< value_type >::minimum( );
311
312 size_type i, k, count = 0;
313
314 for( i = 0 ; i < in.size( ) ; i++ )
315 {
316 if( mask[ i ] != 0 )
317 {
318 count++;
319 if( min > in[ i ] )
320 {
321 min = in[ i ];
322 }
323 if( max < in[ i ] )
324 {
325 max = in[ i ];
326 }
327 }
328 }
329
330 if( count == 0 )
331 {
332 return( value_type( 0 ) );
333 }
334
335 size_type level = static_cast< size_type >( max - min ) + 1;
336 size_type max_k;
337 double *p = new double[ level ];
338 double myu, omg;
339 double myuT, sig, max_sig = 0.0;
340
341 for( k = 0 ; k < level ; k++ )
342 {
343 p[ k ] = 0.0;
344 }
345 for( k = 0 ; k < in.size( ) ; k++ )
346 {
347 if( mask[ k ] != 0 )
348 {
349 p[ static_cast< size_type >( in[ k ] - min ) ]++;
350 }
351 }
352 for( k = 0 ; k < level ; k++ )
353 {
354 p[ k ] /= static_cast< double >( count );
355 }
356
357 myuT = 0.0;
358 for( k = 0 ; k < level ; k++ )
359 {
360 myuT += k * p[ k ];
361 }
362
363 myu = 0.0;
364 omg = p[ 0 ];
365 max_k = 0;
366 max_sig = ( myuT * omg - myu ) * ( myuT * omg - myu ) / ( omg * ( 1.0 - omg ) );
367 for( k = 1 ; k < level ; k++ )
368 {
369 omg = omg + p[ k ];
370 myu = myu + k * p[ k ];
371 sig = ( myuT * omg - myu ) * ( myuT * omg - myu ) / ( omg * ( 1.0 - omg ) );
372 if( sig > max_sig )
373 {
374 max_sig = sig;
375 max_k = k;
376 }
377 }
378
379 delete [] p;
380
381 return( static_cast< value_type >( max_k + min ) );
382 }
383
396 template < class T, class Allocator >
397 typename array2< T, Allocator >::value_type threshold( const array2< T, Allocator > &in,
398 typename array2< T, Allocator >::size_type x,
399 typename array2< T, Allocator >::size_type y,
400 typename array2< T, Allocator >::size_type w,
401 typename array2< T, Allocator >::size_type h )
402 {
403 typedef typename array2< T, Allocator >::size_type size_type;
404 typedef typename array2< T, Allocator >::value_type value_type;
405
406 if( in.empty( ) )
407 {
408 return( value_type( 0 ) );
409 }
410 else if( in.width( ) <= x || in.height( ) <= y )
411 {
412 return( value_type( 0 ) );
413 }
414
415 if( in.width( ) < x + w )
416 {
417 w = in.width( ) - x;
418 }
419 if( in.height( ) < y + h )
420 {
421 h = in.height( ) - y;
422 }
423
424 value_type min = in( x, y );
425 value_type max = min;
426
427 size_type i, j, k;
428
429 for( j = y ; j < y + h ; j++ )
430 {
431 for( i = x ; i < x + w ; i++ )
432 {
433 if( min > in( i, j ) )
434 {
435 min = in( i, j );
436 }
437 else if( max < in( i, j ) )
438 {
439 max = in( i, j );
440 }
441 }
442 }
443
444 size_type level = static_cast< size_type >( max - min ) + 1;
445 size_type max_k;
446 double *p = new double[ level ];
447 double myu, omg;
448 double myuT, sig, max_sig = 0.0;
449
450 for( k = 0 ; k < level ; k++ )
451 {
452 p[ k ] = 0.0;
453 }
454
455 for( j = y ; j < y + h ; j++ )
456 {
457 for( i = x ; i < x + w ; i++ )
458 {
459 p[ static_cast< size_type >( in( i, j ) - min ) ]++;
460 }
461 }
462
463 size_type N = w * h;
464 for( k = 0 ; k < level ; k++ )
465 {
466 p[ k ] /= static_cast< double >( N );
467 }
468
469 myuT = 0.0;
470 for( k = 0 ; k < level ; k++ )
471 {
472 myuT += k * p[ k ];
473 }
474
475 myu = 0.0;
476 omg = p[ 0 ];
477 max_k = 0;
478 max_sig = ( myuT * omg - myu ) * ( myuT * omg - myu ) / ( omg * ( 1.0 - omg ) );
479 for( k = 1 ; k < level ; k++ )
480 {
481 omg = omg + p[ k ];
482 myu = myu + k * p[ k ];
483 sig = ( myuT * omg - myu ) * ( myuT * omg - myu ) / ( omg * ( 1.0 - omg ) );
484 if( sig > max_sig )
485 {
486 max_sig = sig;
487 max_k = k;
488 }
489 }
490
491 delete [] p;
492
493 return( static_cast< value_type >( max_k + min ) );
494 }
495 }
496
498 // しきい値グループの終わり
499
500
501 // mist名前空間の終わり
502 _MIST_END
503
504
505 #endif // __INCLUDE_MIST_THRESHOLD__
506