]> Creatis software - FrontAlgorithms.git/blob - appli/CTBronchi/Functions.h
...
[FrontAlgorithms.git] / appli / CTBronchi / Functions.h
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __CTBronchi__Functions__h__
6 #define __CTBronchi__Functions__h__
7
8 #include <chrono>
9 #include <map>
10 #include <sstream>
11 #include <itkImageFileReader.h>
12 #include <itkImageFileWriter.h>
13
14 namespace CTBronchi
15 {
16   // -----------------------------------------------------------------------
17   double MeasureTime( itk::ProcessObject* f )
18   {
19     std::chrono::time_point< std::chrono::high_resolution_clock > s, e;
20     std::chrono::duration< double > t;
21     s = std::chrono::high_resolution_clock::now( );
22     f->Update( );
23     e = std::chrono::high_resolution_clock::now( );
24     t = e - s;
25     return( t.count( ) );
26   }
27
28   // -----------------------------------------------------------------------
29   template< class _TImagePtr >
30   void ReadImage( _TImagePtr& image, const std::string& fname )
31   {
32     typedef typename _TImagePtr::ObjectType _TImage;
33     typedef itk::ImageFileReader< _TImage > _TReader;
34     typename _TReader::Pointer reader = _TReader::New( );
35     reader->SetFileName( fname );
36     double t = MeasureTime( reader );
37     std::cout << "Read " << fname << " in " << t << " s" << std::endl;
38     image = reader->GetOutput( );
39     image->DisconnectPipeline( );
40   }
41
42   // -----------------------------------------------------------------------
43   template< class _TImagePtr >
44   void WriteImage( const _TImagePtr& image, const std::string& fname )
45   {
46     typedef typename _TImagePtr::ObjectType _TImage;
47     typedef itk::ImageFileWriter< _TImage > _TWriter;
48     typename _TWriter::Pointer writer = _TWriter::New( );
49     writer->SetFileName( fname );
50     writer->SetInput( image );
51     double t = MeasureTime( writer );
52     std::cout << "Wrote " << fname << " in " << t << " s" << std::endl;
53   }
54
55   // -----------------------------------------------------------------------
56   bool ParseArgs(
57     std::map< std::string, std::string >& args, int argc, char* argv[]
58     )
59   {
60     std::set< std::string > mandatory;
61     mandatory.insert( "in" );
62     mandatory.insert( "out" );
63     mandatory.insert( "seed" );
64
65     args[ "mori_minimum_threshold" ] = "-850";
66     args[ "mori_signal_kernel_size" ] = "20";
67     args[ "mori_signal_threshold" ] = "100";
68     args[ "mori_signal_influence" ] = "0.5";
69     args[ "mori_lower_threshold" ] = "-1024";
70     args[ "mori_upper_threshold" ] = "0";
71     args[ "mori_delta_threshold" ] = "1";
72     args[ "labelling_upper_threshold" ] = "-550";
73     args[ "random_walker_alpha" ] = "1";
74     args[ "random_walker_beta" ] = "100";
75
76     int i = 1;
77     while( i < argc )
78     {
79       std::string cmd = argv[ i ] + 1;
80       if( cmd == "seed" )
81       {
82         std::stringstream seed;
83         seed
84           << argv[ i + 1 ] << ";"
85           << argv[ i + 2 ] << ";"
86           << argv[ i + 3 ];
87         args[ cmd ] = seed.str( );
88         i += 4;
89       }
90       else
91       {
92         args[ cmd ] = argv[ i + 1 ];
93         i += 2;
94
95       } // fi
96
97     } // elihw
98
99     bool complete = true;
100     for( std::string t: mandatory )
101       complete &= ( args.find( t ) != args.end( ) );
102
103     if( !complete )
104     {
105       std::cerr
106         << "Usage: " << argv[ 0 ] << std::endl
107         << "\t-in filename" << std::endl
108         << "\t-out filename" << std::endl
109         << "\t-seed x y z" << std::endl
110         << "\t[-out_mori filename]" << std::endl
111         << "\t[-out_signal filename]" << std::endl
112         << "\t[-out_labels filename]" << std::endl
113         << "\t[-mori_minimum_threshold value]" << std::endl
114         << "\t[-mori_signal_kernel_size value]" << std::endl
115         << "\t[-mori_signal_threshold value]" << std::endl
116         << "\t[-mori_signal_influence value]" << std::endl
117         << "\t[-mori_lower_threshold value]" << std::endl
118         << "\t[-mori_upper_threshold value]" << std::endl
119         << "\t[-mori_delta_threshold value]" << std::endl
120         << "\t[-labelling_upper_threshold value]" << std::endl
121         << "\t[-random_walker_alpha value]" << std::endl
122         << "\t[-random_walker_beta value]" << std::endl;
123       return( false );
124
125     } // fi
126     return( true );
127   }
128
129 } // ecapseman
130
131 #endif // __CTBronchi__Functions__h__
132
133 // eof - $RCSfile$