/
median.h
1372 lines (1180 loc) · 36.9 KB
/
median.h
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2003-2010, MIST Project, Nagoya University
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the Nagoya University nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
/// @file mist/filter/median.h
//!
//! @brief 各次元の画像に対応したメディアン(中央値)を計算するためのライブラリ
//!
#ifndef __INCLUDE_MIST_MEDIAN_FILTER__
#define __INCLUDE_MIST_MEDIAN_FILTER__
#ifndef __INCLUDE_MIST_H__
#include "../mist.h"
#endif
#ifndef __INCLUDE_MIST_TYPE_TRAIT_H__
#include "../config/type_trait.h"
#endif
// カラー画像の設定を読み込む
#ifndef __INCLUDE_MIST_COLOR_H__
#include "../config/color.h"
#endif
#ifndef __INCLUDE_MIST_THREAD__
#include "../thread.h"
#endif
#include <algorithm>
// mist名前空間の始まり
_MIST_BEGIN
// メディアンフィルタ
namespace __median_filter_with_histogram__
{
// in : 入力画像. 入力画像の画素値は min と max の間とする
// out : 出力画像. 出力画像のメモリはあらかじめ割り当てられているものとする
// fw, fh, fd : フィルタサイズ
// min, max : 濃淡範囲
template < class Array1, class Array2, class Functor >
void median_filter( const Array1 &in, Array2 &out,
typename Array1::size_type fw, typename Array1::size_type fh, typename Array1::size_type fd,
typename Array1::value_type min, typename Array1::value_type max,
typename Array1::size_type thread_idy, typename Array1::size_type thread_numy,
typename Array1::size_type thread_idz, typename Array1::size_type thread_numz, Functor f )
{
typedef typename Array1::size_type size_type;
typedef typename Array1::difference_type difference_type;
typedef typename Array1::value_type value_type;
typedef typename Array2::value_type out_value_type;
typedef difference_type hist_value;
size_type range = static_cast< size_type >( max - min + 1 );
size_type a = 0, i, j, k, x, y, z, ri, leftnum;
difference_type pth, th, lt_med;
difference_type med;
size_type w = in.width( );
size_type h = in.height( );
size_type d = in.depth( );
const bool bprogress1 = thread_idy == 0 && d == 1;
const bool bprogress2 = thread_idz == 0 && d > 1;
size_type bw = fw / 2;
size_type bh = fh / 2;
size_type bd = fd / 2;
difference_type *leftmost = new difference_type[ fh * fd ];
difference_type *sort = new difference_type[ fw * fh * fd + 1 ];
hist_value *hist = new hist_value[ range ];
for( k = thread_idz ; k < d ; k += thread_numz )
{
for( j = thread_idy ; j < h ; j += thread_numy )
{
i = 0;
memset( hist, 0, sizeof( hist_value ) * range );
pth = 0;
leftnum = 0;
for( z = k < bd ? 0 : k - bd ; z <= k + bd && z < d ; z++ )
{
for( y = j < bh ? 0 : j - bh ; y <= j + bh && y < h ; y++ )
{
leftmost[ leftnum++ ] = in( 0, y, z ) - min;
for( x = 0 ; x <= bw ; x++ )
{
sort[ pth++ ] = in( x, y, z ) - min;
hist[ in( x, y, z ) - min ]++;
}
}
}
// 昇順に並べ替える
std::sort( sort, sort + pth );
pth--;
th = pth / 2;
med = sort[ th ];
if( sort[ 0 ] == sort[ th ] )
{
lt_med = 0;
}
else
{
a = th;
while( med <= sort[ --a ] ){}
lt_med = static_cast< signed int >( a + 1 );
}
out( i, j, k ) = static_cast< out_value_type >( med + min );
for( i = 1 ; i < w ; i++ )
{
ri = i + bw;
if( bw < i )
{
for( a = 0 ; a < leftnum ; a++ )
{
hist[ leftmost[ a ] ]--;
pth--;
if( leftmost[ a ] < med )
{
lt_med--;
}
}
}
leftnum = 0;
for( z = k < bd ? 0 : k - bd ; z <= k + bd && z < d ; z++ )
{
for( y = j < bh ? 0 : j - bh ; y <= j + bh && y < h ; y++ )
{
if( ri < w )
{
hist[ in( ri, y, z ) - min ]++;
pth++;
if( in( ri, y, z ) - min < med )
{
lt_med++;
}
}
if( bw <= i )
{
leftmost[ leftnum++ ] = in( i - bw, y, z ) - min;
}
}
}
th = pth / 2;
if( lt_med <= th )
{
while( lt_med + hist[ med ] <= th )
{
lt_med += hist[ med ];
med++;
}
}
else
{
while( th < lt_med )
{
med--;
lt_med -= hist[ med ];
}
}
out( i, j, k ) = static_cast< out_value_type >( med + min );
}
if( bprogress1 )
{
f( static_cast< double >( j + 1 ) / static_cast< double >( h ) * 100.0 );
}
}
if( bprogress2 )
{
f( static_cast< double >( k + 1 ) / static_cast< double >( d ) * 100.0 );
}
}
delete [] hist;
delete [] sort;
delete [] leftmost;
}
}
namespace __median_filter_divide_conquer__
{
// T型の配列の中央値を高速に計算するアルゴリズム
// 配列の要素 数はnum個である。
// 配列内で第c番目に小さい値を検索する
// 入力されるDATA型の配列は中身を入れ替えない代わりに,ワーク配列を利用する
template < class T >
inline T nth_value( const T *a, size_t num, size_t c, T *work1, T *work2, T *work3 )
{
typedef T value_type;
size_t i;
size_t wi1, wi2, wi3;
value_type s;
value_type *w1 = work1;
value_type *w2 = work2;
value_type *src, *www;
wi1 = wi2 = wi3 = 0;
s = a[ c ];
for( i = 0 ; i < num ; i++ )
{
if( a[ i ] < s )
{
w1[ wi1++ ] = a[ i ];
}
else if( a[ i ] > s )
{
w2[ wi2++ ] = a[ i ];
}
else
{
wi3++;
}
}
if( wi1 > c )
{
src = w1;
w1 = work3;
num = wi1;
}
else if( wi1 + wi3 <= c )
{
src = w2;
w2 = work3;
c -= wi1 + wi3;
num = wi2;
}
else
{
return ( s );
}
while( true )
{
wi1 = wi2 = wi3 = 0;
s = src[ c ];
for( i = 0 ; i < num ; i++ )
{
if( src[ i ] < s )
{
w1[ wi1++ ] = src[ i ];
}
else if( src[ i ] > s )
{
w2[ wi2++ ] = src[ i ];
}
else
{
wi3++;
}
}
if( wi1 > c )
{
www = src;
src = w1;
w1 = www;
num = wi1;
}
else if( wi1 + wi3 <= c )
{
www = src;
src = w2;
w2 = www;
c -= wi1 + wi3;
num = wi2;
}
else
{
break;
}
}
return ( s );
}
// in : 入力画像. 入力画像の画素値は min と max の間とする
// out : 出力画像. 出力画像のメモリはあらかじめ割り当てられているものとする
// fw, fh, fd : フィルタサイズ
// min, max : 濃淡範囲
template < class Array1, class Array2, class Functor >
void median_filter( const Array1 &in, Array2 &out,
typename Array1::size_type fw, typename Array1::size_type fh, typename Array1::size_type fd,
typename Array1::size_type thread_idy, typename Array1::size_type thread_numy,
typename Array1::size_type thread_idz, typename Array1::size_type thread_numz, Functor f )
{
typedef typename Array1::size_type size_type;
typedef typename Array1::value_type value_type;
typedef typename Array2::value_type out_value_type;
size_type i, j, k, x, y, z, ri;
size_type pth, c, th, windex;
size_type w = in.width( );
size_type h = in.height( );
size_type d = in.depth( );
const bool bprogress1 = thread_idy == 0 && d == 1;
const bool bprogress2 = thread_idz == 0 && d > 1;
size_type bw = fw / 2;
size_type bh = fh / 2;
size_type bd = fd / 2;
size_type bbh, bbd;
size_type size = fw * fh * fd;
value_type *work = new value_type[ size + 1 ];
value_type *work1 = new value_type[ size + 1 ];
value_type *work2 = new value_type[ size + 1 ];
value_type *work3 = new value_type[ size + 1 ];
value_type **sort = new value_type*[ fw ];
for( k = thread_idz ; k < d ; k += thread_numz )
{
if( k < bd )
{
bbd = k + bd + 1;
}
else if( k + bd >= d )
{
bbd = d - k + bd + 1;
}
else
{
bbd = fd;
}
for( j = thread_idy ; j < h ; j += thread_numy )
{
if( j < bh )
{
bbh = j + bh + 1;
}
else if( j + bh >= h )
{
bbh = h - j + bh + 1;
}
else
{
bbh = fh;
}
for( i = 0 ; i < fw ; i++ )
{
sort[ i ] = work + i * bbh * bbd;
}
i = 0;
windex = 0;
for( x = 0 ; x <= bw ; x++ )
{
windex = windex % fw;
c = 0;
for( z = k < bd ? 0 : k - bd ; z <= k + bd && z < d ; z++ )
{
for( y = j < bh ? 0 : j - bh ; y <= j + bh && y < h ; y++ )
{
sort[ windex ][ c++ ] = in( x, y, z );
}
}
windex++;
}
pth = ( bw + 1 ) * bbh * bbd;
th = ( pth - 1 ) / 2;
out( i, j, k ) = static_cast< out_value_type >( nth_value( work, pth, th, work1, work2, work3 ) );
for( i = 1 ; i < bw ; i++ )
{
ri = i + bw;
pth = ( i + bw + 1 ) * bbh * bbd;
windex = windex % fw;
c = 0;
for( z = k < bd ? 0 : k - bd ; z <= k + bd && z < d ; z++ )
{
for( y = j < bh ? 0 : j - bh ; y <= j + bh && y < h ; y++ )
{
sort[ windex ][ c++ ] = in( i, y, z );
}
}
th = ( pth - 1 ) / 2;
out( i, j, k ) = static_cast< out_value_type >( nth_value( work, pth, th, work1, work2, work3 ) );
windex++;
}
pth = fw * bbh * bbd;
th = ( pth - 1 ) / 2;
for( ; i + bw < w ; i++ )
{
ri = i + bw;
windex = windex % fw;
c = 0;
for( z = k < bd ? 0 : k - bd ; z <= k + bd && z < d ; z++ )
{
for( y = j < bh ? 0 : j - bh ; y <= j + bh && y < h ; y++ )
{
sort[ windex ][ c++ ] = in( ri, y, z );
}
}
out( i, j, k ) = static_cast< out_value_type >( nth_value( work, pth, th, work1, work2, work3 ) );
windex++;
}
for( ; i < w ; i++ )
{
windex = windex % fw;
c = 0;
for( z = k < bd ? 0 : k - bd ; z <= k + bd && z < d ; z++ )
{
for( y = j < bh ? 0 : j - bh ; y <= j + bh && y < h ; y++ )
{
sort[ windex ][ c++ ] = 0;
}
}
out( i, j, k ) = static_cast< out_value_type >( nth_value( work, pth, th, work1, work2, work3 ) );
windex++;
}
if( bprogress1 )
{
f( static_cast< double >( j + 1 ) / static_cast< double >( h ) * 100.0 );
}
}
if( bprogress2 )
{
f( static_cast< double >( k + 1 ) / static_cast< double >( d ) * 100.0 );
}
}
delete [] work;
delete [] work1;
delete [] work2;
delete [] work3;
delete [] sort;
}
}
namespace __median_filter_specialized_version__
{
template < class T >
inline void sort3x3( const T &v0, const T &v1, const T &v2, T v[ 3 ] )
{
if( v0 > v1 )
{
if( v1 > v2 )
{
v[ 0 ] = v0;
v[ 1 ] = v1;
v[ 2 ] = v2;
}
else
{
if( v0 > v2 )
{
v[ 0 ] = v0;
v[ 1 ] = v2;
v[ 2 ] = v1;
}
else
{
v[ 0 ] = v2;
v[ 1 ] = v0;
v[ 2 ] = v1;
}
}
}
else
{
if( v0 > v2 )
{
v[ 0 ] = v1;
v[ 1 ] = v0;
v[ 2 ] = v2;
}
else
{
if( v1 > v2 )
{
v[ 0 ] = v1;
v[ 1 ] = v2;
v[ 2 ] = v0;
}
else
{
v[ 0 ] = v2;
v[ 1 ] = v1;
v[ 2 ] = v0;
}
}
}
}
template < class T >
inline void sort2x2( const T &v0, const T &v1, T v[ 3 ] )
{
if( v0 > v1 )
{
v[ 0 ] = v0;
v[ 1 ] = v1;
}
else
{
v[ 0 ] = v1;
v[ 1 ] = v0;
}
}
template < class T >
inline void sort3x3( const T &v0, const T &v1, const T &v2, T w0[ 3 ], T w1[ 3 ], T w2[ 3 ], T *v[ 3 ] )
{
if( v0 > v1 )
{
if( v1 > v2 )
{
v[ 0 ] = w0;
v[ 1 ] = w1;
v[ 2 ] = w2;
}
else
{
if( v0 > v2 )
{
v[ 0 ] = w0;
v[ 1 ] = w2;
v[ 2 ] = w1;
}
else
{
v[ 0 ] = w2;
v[ 1 ] = w0;
v[ 2 ] = w1;
}
}
}
else
{
if( v0 > v2 )
{
v[ 0 ] = w1;
v[ 1 ] = w0;
v[ 2 ] = w2;
}
else
{
if( v1 > v2 )
{
v[ 0 ] = w1;
v[ 1 ] = w2;
v[ 2 ] = w0;
}
else
{
v[ 0 ] = w2;
v[ 1 ] = w1;
v[ 2 ] = w0;
}
}
}
}
template < class T >
inline void sort2x2( const T &v0, const T &v1, T w0[ 3 ], T w1[ 3 ], T *v[ 3 ] )
{
if( v0 > v1 )
{
v[ 0 ] = w0;
v[ 1 ] = w1;
}
else
{
v[ 0 ] = w1;
v[ 1 ] = w0;
}
}
template < class T >
inline const T &minimum( const T &v0, const T &v1 )
{
return( v0 < v1 ? v0 : v1 );
}
template < class T >
inline const T &minimum( const T &v0, const T &v1, const T &v2 )
{
return( v0 < v1 ? ( v0 < v2 ? v0 : v2 ) : ( v1 < v2 ? v1 : v2 ) );
}
template < class T >
inline const T &maximum( const T &v0, const T &v1, const T &v2 )
{
return( v0 > v1 ? ( v0 > v2 ? v0 : v2 ) : ( v1 > v2 ? v1 : v2 ) );
}
template < class T >
inline const T &maximum( const T &v0, const T &v1 )
{
return( v0 > v1 ? v0 : v1 );
}
template < class T >
inline const T &median( const T &v0, const T &v1, const T &v2 )
{
if( v0 > v1 )
{
if( v1 > v2 )
{
return( v1 );
}
else
{
if( v0 > v2 )
{
return( v2 );
}
else
{
return( v0 );
}
}
}
else
{
if( v0 > v2 )
{
return( v0 );
}
else
{
if( v1 > v2 )
{
return( v2 );
}
else
{
return( v1 );
}
}
}
}
/****************************************************************************************************************************************
**
** 参考文献:
**
** 浜村倫行, 入江文平, ``3×3メディアンフィルタの高速アルゴリズム,'' FIT(情報科学技術フォーラム), LI-9, 2002
** ``A Fast Algorithm for 3x3 Median Filtering''
**
** ※ヒストグラムを利用するもの等と比較し,中央値を求めるのに必要な比較回数が非常に少なく,高速な動作が可能
**
** Coded by ddeguchi.
**
****************************************************************************************************************************************/
template < class T1, class T2, class Allocator1, class Allocator2, class Functor >
void median_filter3x3( const array2< T1, Allocator1 > &in, array2< T2, Allocator2 > &out,
typename array2< T1, Allocator1 >::size_type thread_id, typename array2< T1, Allocator1 >::size_type thread_num, Functor f )
{
typedef typename array2< T1, Allocator1 >::size_type size_type;
typedef typename array2< T1, Allocator1 >::value_type value_type;
typedef typename array2< T1, Allocator1 >::difference_type difference_type;
typedef typename array2< T2, Allocator2 >::value_type out_value_type;
size_type i, j, wi;
size_type w = in.width( );
size_type h = in.height( );
value_type work0[ 3 ];
value_type work1[ 3 ];
value_type work2[ 3 ];
value_type *work[ 3 ] = { work0, work1, work2 };
value_type *sort[ 3 ];
// 一番上の部分
for( j = thread_id ; j < 1 ; j += thread_num )
{
sort2x2( in( 0, 0 ), in( 0, 1 ), work0 );
sort2x2( in( 1, 0 ), in( 1, 1 ), work1 );
sort2x2( work0[ 1 ], work1[ 1 ], work0, work1, sort );
out( 0, 0 ) = static_cast< out_value_type >( minimum( sort[ 0 ][ 1 ], sort[ 1 ][ 0 ] ) );
for( i = 1 ; i < w - 1 ; i++ )
{
wi = ( i + 1 ) % 3;
sort2x2( in( i + 1, 0 ), in( i + 1, 0 + 1 ), work[ wi ] );
sort3x3( work0[ 1 ], work1[ 1 ], work2[ 1 ], work0, work1, work2, sort );
value_type &x = sort[ 1 ][ 1 ];
value_type &y = sort[ 0 ][ 1 ];
value_type &z = sort[ 2 ][ 0 ];
if( x < z )
{
value_type &w = sort[ 1 ][ 0 ];
out( i, 0 ) = static_cast< out_value_type >( minimum( y, z, w ) );
}
else
{
out( i, 0 ) = static_cast< out_value_type >( x );
}
}
sort2x2( work[ ( w - 2 ) % 3 ][ 1 ], work[ ( w - 1 ) % 3 ][ 1 ], work[ ( w - 2 ) % 3 ], work[ ( w - 1 ) % 3 ], sort );
out( w - 1, 0 ) = static_cast< out_value_type >( minimum( sort[ 0 ][ 1 ], sort[ 1 ][ 0 ] ) );
if( thread_id == 0 )
{
f( static_cast< double >( j + 1 ) / static_cast< double >( h ) * 100.0 );
}
}
// 真ん中部分
for( ; j < h - 1 ; j += thread_num )
{
sort3x3( in( 0, j - 1 ), in( 0, j ), in( 0, j + 1 ), work0 );
sort3x3( in( 1, j - 1 ), in( 1, j ), in( 1, j + 1 ), work1 );
sort2x2( work0[ 1 ], work1[ 1 ], work0, work1, sort );
out( 0, j ) = static_cast< out_value_type >( median( sort[ 0 ][ 2 ], sort[ 1 ][ 0 ], sort[ 1 ][ 1 ] ) );
for( i = 1 ; i < w - 1 ; i++ )
{
wi = ( i + 1 ) % 3;
sort3x3( in( i + 1, j - 1 ), in( i + 1, j ), in( i + 1, j + 1 ), work[ wi ] );
sort3x3( work0[ 1 ], work1[ 1 ], work2[ 1 ], work0, work1, work2, sort );
value_type &x = sort[ 1 ][ 1 ];
value_type &y = sort[ 0 ][ 2 ];
value_type &z = sort[ 2 ][ 0 ];
if( x < y && x < z )
{
value_type &w = sort[ 1 ][ 0 ];
out( i, j ) = static_cast< out_value_type >( minimum( y, z, w ) );
}
else if( x > y && x > z )
{
value_type &w = sort[ 1 ][ 2 ];
out( i, j ) = static_cast< out_value_type >( maximum( y, z, w ) );
}
else
{
out( i, j ) = static_cast< out_value_type >( x );
}
}
sort2x2( work[ ( w - 2 ) % 3 ][ 1 ], work[ ( w - 1 ) % 3 ][ 1 ], work[ ( w - 2 ) % 3 ], work[ ( w - 1 ) % 3 ], sort );
out( w - 1, j ) = static_cast< out_value_type >( median( sort[ 0 ][ 2 ], sort[ 1 ][ 0 ], sort[ 1 ][ 1 ] ) );
if( thread_id == 0 )
{
f( static_cast< double >( j + 1 ) / static_cast< double >( h ) * 100.0 );
}
}
// 一番下の部分
for( ; j < h ; j += thread_num )
{
sort2x2( in( 0, h - 2 ), in( 0, h - 1 ), work0 );
sort2x2( in( 1, h - 2 ), in( 1, h - 1 ), work1 );
sort2x2( work0[ 1 ], work1[ 1 ], work0, work1, sort );
out( 0, h - 1 ) = static_cast< out_value_type >( minimum( sort[ 0 ][ 1 ], sort[ 1 ][ 0 ] ) );
for( i = 1 ; i < w - 1 ; i++ )
{
wi = ( i + 1 ) % 3;
sort2x2( in( i + 1, h - 2 ), in( i + 1, h - 1 ), work[ wi ] );
sort3x3( work0[ 1 ], work1[ 1 ], work2[ 1 ], work0, work1, work2, sort );
value_type &x = sort[ 1 ][ 1 ];
value_type &y = sort[ 0 ][ 1 ];
value_type &z = sort[ 2 ][ 0 ];
if( x < z )
{
value_type &w = sort[ 1 ][ 0 ];
out( i, h - 1 ) = static_cast< out_value_type >( minimum( y, z, w ) );
}
else
{
out( i, h - 1 ) = static_cast< out_value_type >( x );
}
}
sort2x2( work[ ( w - 2 ) % 3 ][ 1 ], work[ ( w - 1 ) % 3 ][ 1 ], work[ ( w - 2 ) % 3 ], work[ ( w - 1 ) % 3 ], sort );
out( w - 1, h - 1 ) = static_cast< out_value_type >( minimum( sort[ 0 ][ 1 ], sort[ 1 ][ 0 ] ) );
if( thread_id == 0 )
{
f( static_cast< double >( j + 1 ) / static_cast< double >( h ) * 100.0 );
}
}
}
}
// メディアンフィルタのスレッド実装
namespace __median_filter_controller__
{
template < class T, class Allocator >
void get_min_max( const array< T, Allocator > &in, typename array< T, Allocator >::value_type &min, typename array< T, Allocator >::value_type &max )
{
min = max = in[ 0 ];
for( typename array< T, Allocator >::size_type i = 0 ; i < in.size( ) ; i++ )
{
if( min > in[ i ] )
{
min = in[ i ];
}
else if( max < in[ i ] )
{
max = in[ i ];
}
}
}
template < bool b >
struct __median_filter__
{
template < class T1, class Allocator1, class T2, class Allocator2, class Functor >
static void median_filter( const array< T1, Allocator1 > &in, array< T2, Allocator2 > &out,
typename array< T1, Allocator1 >::size_type fw, typename array< T1, Allocator1 >::size_type fh, typename array< T1, Allocator1 >::size_type fd,
typename array< T1, Allocator1 >::size_type thread_id, typename array< T1, Allocator1 >::size_type thread_num, Functor f )
{
typedef typename array< T1, Allocator1 >::value_type value_type;
value_type min = in[ 0 ];
value_type max = in[ 0 ];
get_min_max( in, min, max );
__median_filter_with_histogram__::median_filter( in, out, fw, fh, fd, min, max, 0, 1, thread_id, thread_num, f );
}
template < class T1, class Allocator1, class T2, class Allocator2, class Functor >
static void median_filter( const array2< T1, Allocator1 > &in, array2< T2, Allocator2 > &out,
typename array2< T1, Allocator1 >::size_type fw, typename array2< T1, Allocator1 >::size_type fh, typename array2< T1, Allocator1 >::size_type fd,
typename array2< T1, Allocator1 >::size_type thread_id, typename array2< T1, Allocator1 >::size_type thread_num, Functor f )
{
typedef typename array2< T1, Allocator1 >::value_type value_type;
if( fw == 3 && fh == 3 )
{
__median_filter_specialized_version__::median_filter3x3( in, out, thread_id, thread_num, f );
}
else
{
value_type min = in[ 0 ];
value_type max = in[ 0 ];
get_min_max( in, min, max );
__median_filter_with_histogram__::median_filter( in, out, fw, fh, fd, min, max, thread_id, thread_num, 0, 1, f );
}
}
template < class T1, class Allocator1, class T2, class Allocator2, class Functor >
static void median_filter( const array3< T1, Allocator1 > &in, array3< T2, Allocator2 > &out,
typename array3< T1, Allocator1 >::size_type fw, typename array3< T1, Allocator1 >::size_type fh, typename array3< T1, Allocator1 >::size_type fd,
typename array3< T1, Allocator1 >::size_type thread_id, typename array3< T1, Allocator1 >::size_type thread_num, Functor f )
{
typedef typename array3< T1, Allocator1 >::value_type value_type;
value_type min = in[ 0 ];
value_type max = in[ 0 ];
get_min_max( in, min, max );
__median_filter_with_histogram__::median_filter( in, out, fw, fh, fd, min, max, 0, 1, thread_id, thread_num, f );
}
};
// ヒストグラムを作成できない場合のメディアンフィルタ
// 浮動小数点など
template < >
struct __median_filter__< false >
{
template < class T1, class Allocator1, class T2, class Allocator2, class Functor >
static void median_filter( const array< T1, Allocator1 > &in, array< T2, Allocator2 > &out,
typename array< T1, Allocator1 >::size_type fw, typename array< T1, Allocator1 >::size_type fh, typename array< T1, Allocator1 >::size_type fd,
typename array< T1, Allocator1 >::size_type thread_id, typename array< T1, Allocator1 >::size_type thread_num, Functor f )
{
__median_filter_divide_conquer__::median_filter( in, out, fw, fh, fd, 0, 1, thread_id, thread_num, f );
}
template < class T1, class Allocator1, class T2, class Allocator2, class Functor >
static void median_filter( const array2< T1, Allocator1 > &in, array2< T2, Allocator2 > &out,
typename array2< T1, Allocator1 >::size_type fw, typename array2< T1, Allocator1 >::size_type fh, typename array2< T1, Allocator1 >::size_type fd,
typename array2< T1, Allocator1 >::size_type thread_id, typename array2< T1, Allocator1 >::size_type thread_num, Functor f )
{
if( fw == 3 && fh == 3 )
{
__median_filter_specialized_version__::median_filter3x3( in, out, thread_id, thread_num, f );
}
else
{
__median_filter_divide_conquer__::median_filter( in, out, fw, fh, fd, thread_id, thread_num, 0, 1, f );
}
}
template < class T1, class Allocator1, class T2, class Allocator2, class Functor >
static void median_filter( const array3< T1, Allocator1 > &in, array3< T2, Allocator2 > &out,
typename array3< T1, Allocator1 >::size_type fw, typename array3< T1, Allocator1 >::size_type fh, typename array3< T1, Allocator1 >::size_type fd,
typename array3< T1, Allocator1 >::size_type thread_id, typename array3< T1, Allocator1 >::size_type thread_num, Functor f )
{
__median_filter_divide_conquer__::median_filter( in, out, fw, fh, fd, 0, 1, thread_id, thread_num, f );
}
};
template < class T1, class T2, class Functor >
class median_thread : public mist::thread< median_thread< T1, T2, Functor > >
{
public:
typedef mist::thread< median_thread< T1, T2, Functor > > base;
typedef typename base::thread_exit_type thread_exit_type;
typedef typename T1::size_type size_type;
typedef typename T1::value_type value_type;
private:
size_t thread_id_;
size_t thread_num_;
// 入出力用の画像へのポインタ
const T1 *in_;
T2 *out_;
size_type fw_;
size_type fh_;
size_type fd_;
Functor f_;
public:
void setup_parameters( const T1 &in, T2 &out, size_t fw, size_type fh, size_type fd, size_type thread_id, size_type thread_num, Functor f )
{
in_ = ∈
out_ = &out;
fw_ = fw;
fh_ = fh;