]> Creatis software - FrontAlgorithms.git/blob - appli/CTBronchi/Process.cxx
...
[FrontAlgorithms.git] / appli / CTBronchi / Process.cxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #include <itkImage.h>
7 #include <CTBronchi/Functions.h>
8 #include <CTBronchi/Mori.h>
9 #include <CTBronchi/MoriLabelling.h>
10 #include <CTBronchi/RandomWalker.h>
11
12 // -------------------------------------------------------------------------
13 const unsigned int Dim = 3;
14 typedef short         TInputPixel;
15 typedef unsigned char TLabelPixel;
16 typedef float         TScalar;
17 typedef itk::Image< TInputPixel, Dim > TImage;
18 typedef itk::Image< TLabelPixel, Dim > TLabelImage;
19 typedef itk::Image< TScalar, Dim >     TScalarImage;
20
21 // -------------------------------------------------------------------------
22 /* TODO
23    TMap Args;
24    TInputImage::PointType global_seed;
25    TLabelPixel inside_value = TLabelPixel( 1 );
26    TLabelPixel outside_value = TLabelPixel( 0 );
27    TLabelPixel inside_label = TLabelPixel( 1 );
28    TLabelPixel outside_label = TLabelPixel( 2 );
29 */
30
31 // -------------------------------------------------------------------------
32 /* TODO
33    template< class _TInputPtr, class _TOutputPtr >
34    void Label( _TOutputPtr& output, const _TInputPtr& input, const _TOutputPtr& labels )
35    {
36    typedef typename _TInputPtr::ObjectType  _TInput;
37    typedef typename _TOutputPtr::ObjectType _TOutput;
38    typedef CTBronchi::MoriLabelling< _TInput, _TOutput > _TLabelling;
39
40    typename _TLabelling::Pointer labelling = _TLabelling::New( );
41    labelling->SetInput( input );
42    labelling->SetInputLabels( labels );
43    labelling->SetOutsideValue( outside_label );
44    labelling->SetInsideValue( inside_label );
45    labelling->SetInputInsideValue( inside_value );
46    labelling->SetUpperThreshold(
47    TInputPixel( std::atof( Args[ "labelling_upper_threshold" ].c_str( ) ) )
48    );
49    double t = MeasureTime( labelling );
50    std::cout << "Labelling executed in " << t << " s" << std::endl;
51    output = labelling->GetOutput( );
52    TMap::const_iterator i = Args.find( "out_labels" );
53    if( i != Args.end( ) )
54    WriteImage( output, i->second );
55    output->DisconnectPipeline( );
56    }
57
58    // -------------------------------------------------------------------------
59    template< class _TRawPtr, class _TLabelPtr >
60    void RandomWalker( _TLabelPtr& output, const _TRawPtr& raw, const _TLabelPtr& labels )
61    {
62    typedef typename _TRawPtr::ObjectType  _TRaw;
63    typedef typename _TLabelPtr::ObjectType _TLabel;
64    }
65
66    // -------------------------------------------------------------------------
67    bool ParseArgs( int argc, char* argv[] )
68    {
69    std::set< std::string > mandatory;
70    mandatory.insert( "in" );
71    mandatory.insert( "out_segmentation" );
72    mandatory.insert( "seed_x" );
73    mandatory.insert( "seed_y" );
74    mandatory.insert( "seed_z" );
75
76    Args[ "mori_minimum_threshold" ] = "-850";
77    Args[ "mori_signal_kernel_size" ] = "20";
78    Args[ "mori_signal_threshold" ] = "100";
79    Args[ "mori_signal_influence" ] = "0.5";
80    Args[ "mori_lower_threshold" ] = "-1024";
81    Args[ "mori_upper_threshold" ] = "0";
82    Args[ "mori_delta_threshold" ] = "1";
83    Args[ "labelling_upper_threshold" ] = "-400";
84
85    for( int i = 1; i < argc; i += 2 )
86    Args[ argv[ i ] + 1 ] = argv[ i + 1 ];
87
88    bool complete = true;
89    for( std::string t: mandatory )
90    complete &= ( Args.find( t ) != Args.end( ) );
91
92    if( !complete )
93    {
94    std::cerr
95    << "Usage: " << argv[ 0 ] << std::endl
96    << "\t-in filename" << std::endl
97    << "\t-out_segmentation filename" << std::endl
98    << "\t-seed_x value -seed_y value -seed_z value" << std::endl
99    << "\t[-out_mori filename]" << std::endl
100    << "\t[-out_signal filename]" << std::endl
101    << "\t[-out_labels filename]" << std::endl
102    << "\t[-mori_minimum_threshold value]" << std::endl
103    << "\t[-mori_signal_kernel_size value]" << std::endl
104    << "\t[-mori_signal_threshold value]" << std::endl
105    << "\t[-mori_signal_influence value]" << std::endl
106    << "\t[-mori_lower_threshold value]" << std::endl
107    << "\t[-mori_upper_threshold value]" << std::endl
108    << "\t[-mori_delta_threshold value]" << std::endl
109    << "\t[-labelling_upper_threshold value]" << std::endl;
110    return( false );
111
112    } // fi
113    return( true );
114    }
115 */
116
117 // -------------------------------------------------------------------------
118 int main( int argc, char* argv[] )
119 {
120   std::map< std::string, std::string > args;
121   try
122   {
123     if( CTBronchi::ParseArgs( args, argc, argv ) )
124     {
125       // Parse seed
126       TImage::PointType seed;
127       char* str = new char[ args[ "seed" ].size( ) + 1 ];
128       strcpy( str, args[ "seed" ].c_str( ) );
129       seed[ 0 ] = std::atof( strtok( str, ";" ) );
130       seed[ 1 ] = std::atof( strtok( NULL, ";" ) );
131       seed[ 2 ] = std::atof( strtok( NULL, ";" ) );
132
133       // Read input image
134       TImage::Pointer input_image;
135       CTBronchi::ReadImage( input_image, args[ "in" ] );
136
137       // Mori segmentation
138       TLabelImage::Pointer mori;
139       TInputPixel opt_thr = CTBronchi::Mori( mori, input_image, seed, args );
140
141       // Label image
142       TLabelImage::Pointer labels;
143       CTBronchi::Label( labels, input_image, mori, opt_thr, args );
144
145       // Final segmentation
146       TScalarImage::Pointer segmentation;
147       CTBronchi::RandomWalker( segmentation, input_image, labels, args );
148
149       // Save
150       CTBronchi::WriteImage( segmentation, args[ "out" ] );
151     }
152     else
153       return( 1 );
154   }
155   catch( std::exception& err )
156   {
157     std::cerr
158       << "===============================" << std::endl
159       << "Error caught: " << std::endl
160       << err.what( )
161       << "===============================" << std::endl
162       << std::endl;
163     return( 1 );
164
165   } // yrt
166   return( 0 );
167
168   /* TODO
169      try
170      {
171      if( ParseArgs( argc, argv ) )
172      {
173      // Parse seed
174      global_seed[ 0 ] = std::atof( Args[ "seed_x" ].c_str( ) );
175      global_seed[ 1 ] = std::atof( Args[ "seed_y" ].c_str( ) );
176      global_seed[ 2 ] = std::atof( Args[ "seed_z" ].c_str( ) );
177
178      // Read input image
179      TInputImage::Pointer input_image;
180      ReadImage( input_image, Args[ "in" ] );
181
182      // Mori segmentation
183      TLabelImage::Pointer mori;
184      Mori( mori, input_image );
185
186      // Create labels
187      TLabelImage::Pointer labels;
188      Label( labels, input_image, mori );
189
190      return( 0 );
191
192      } // fi
193      return( 1 );
194      }
195   */
196   /* TODO
197      if [ -z "$label_upper_threshold" ]; then label_upper_threshold="-600"; fi
198      if [ -z "$label_inside" ]; then label_inside="1"; fi
199      if [ -z "$label_outside" ]; then label_outside="2"; fi
200      if [ -z "$random_walker_beta" ]; then random_walker_beta="20"; fi
201   */
202 }
203
204 // eof - $RCSfile$