1 #include "mrilib.h"
2 
3 /*------------------------------------------------------------------------*/
4 
main(int argc,char * argv[])5 int main( int argc , char * argv[] )
6 {
7    float irad=1.5 ;
8    int nrep=1 ;
9    char *prefix = "MedianFilter" ;
10    int iarg , verb=0 , do_mask=0 , niter=1,pit ;
11    THD_3dim_dataset *inset , *outset ;
12    MRI_IMAGE *imout=NULL, *imin=NULL;
13    byte *mask=NULL ;
14 
15    if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
16       printf("Usage: 3dMedianFilter [options] dataset\n"
17              "Computes the median in a spherical nbhd around each point in the\n"
18              "input to produce the output.\n"
19              "\n"
20              "Options:\n"
21              "  -irad x    = Radius in voxels of spherical regions\n"
22              "  -iter n    = Iterate 'n' times [default=1]\n"
23              "  -verb      = Be verbose during run\n"
24              "  -prefix pp = Use 'pp' for prefix of output dataset\n"
25              "  -automask  = Create a mask (a la 3dAutomask)\n"
26              "\n"
27              "Output dataset is always stored in float format.  If the input\n"
28              "dataset has more than 1 sub-brick, only sub-brick #0 is processed.\n"
29              "\n"
30              "-- Feb 2005 - RWCox\n"
31             ) ;
32       PRINT_COMPILE_DATE ; exit(0) ;
33    }
34 
35    mainENTRY("3dMedianFilter main"); machdep(); AFNI_logger("3dMedianFilter",argc,argv);
36    PRINT_VERSION("3dMedianFilter") ;
37 
38    /*-- scan command line --*/
39 
40    iarg = 1 ;
41    while( iarg < argc && argv[iarg][0] == '-' ){
42 
43       if( strcmp(argv[iarg],"-iter") == 0 ){
44         niter = (int)strtol( argv[++iarg], NULL , 10 ) ;
45         if( niter < 1 )
46           ERROR_exit("Illegal value after -iter!") ;
47         iarg++ ; continue ;
48       }
49 
50       if( strncmp(argv[iarg],"-automask",5) == 0 ){
51         do_mask++ ; iarg++ ; continue ;
52       }
53 
54       if( strncmp(argv[iarg],"-verb",5) == 0 ){
55         verb++ ; iarg++ ; continue ;
56       }
57 
58       if( strcmp(argv[iarg],"-irad") == 0 ){
59         irad = strtod( argv[++iarg] , NULL ) ;
60         if( irad < 1.0f )
61           ERROR_exit("Illegal value after -irad!\n");
62         iarg++ ; continue ;
63       }
64 
65       if( strcmp(argv[iarg],"-prefix") == 0 ){
66         prefix = argv[++iarg] ;
67         if( !THD_filename_ok(prefix) )
68           ERROR_exit("Illegal value after -prefix!\n");
69         iarg++ ; continue ;
70       }
71 
72       ERROR_exit("Unknown option: %s\n",argv[iarg]);
73    }
74 
75    if( iarg >= argc )
76      ERROR_exit("No dataset name on command line?\n");
77 
78    /*-- read input --*/
79 
80    inset = THD_open_dataset( argv[iarg] ) ;
81    CHECK_OPEN_ERROR(inset,argv[iarg]) ;
82    DSET_load( inset ) ; CHECK_LOAD_ERROR(inset) ;
83    if( DSET_NVALS(inset) > 1 )
84      WARNING_message("Only processing sub-brick #0\n");
85 
86    if( do_mask ){
87      THD_automask_verbose( verb ) ;
88      THD_automask_extclip( 1 ) ;
89      mask = THD_automask( inset ) ;
90    }
91 
92    imin = mri_to_float( DSET_BRICK(inset,0) ) ;
93    if( imin == NULL ) ERROR_exit("Can't copy input dataset brick") ;
94    for( pit=0 ; pit < niter ; pit++ ){
95      imout = mri_medianfilter( imin , irad , mask , verb ) ;
96      if( pit < niter-1 && mri_equal(imout,imin) ){ /* 08 Aug 2018 */
97        mri_free(imin) ;
98        INFO_message("median filter reaches fixed 'root' image at iteration %d",pit+1) ;
99        break ;
100      }
101      mri_free(imin) ; imin = imout ;
102    }
103 
104    if( mask != NULL ) free((void *)mask) ;
105 
106    outset = EDIT_empty_copy( inset )  ;
107    EDIT_dset_items( outset ,
108                        ADN_prefix , prefix ,
109                        ADN_nvals  , 1 ,
110                        ADN_ntt    , 0 ,
111                     ADN_none ) ;
112    EDIT_substitute_brick( outset , 0 , MRI_float , MRI_FLOAT_PTR(imout) ) ;
113    tross_Copy_History( inset , outset ) ;
114    tross_Make_History( "3dMedianFilter" , argc,argv , outset ) ;
115    DSET_write(outset) ;
116    WROTE_DSET(outset) ;
117    exit(0) ;
118 }
119