1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
7 #include <CTBronchi/Functions.h>
8 #include <CTBronchi/Mori.h>
9 #include <CTBronchi/MoriLabelling.h>
10 #include <CTBronchi/RandomWalker.h>
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;
21 // -------------------------------------------------------------------------
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 );
31 // -------------------------------------------------------------------------
33 template< class _TInputPtr, class _TOutputPtr >
34 void Label( _TOutputPtr& output, const _TInputPtr& input, const _TOutputPtr& labels )
36 typedef typename _TInputPtr::ObjectType _TInput;
37 typedef typename _TOutputPtr::ObjectType _TOutput;
38 typedef CTBronchi::MoriLabelling< _TInput, _TOutput > _TLabelling;
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( ) ) )
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( );
58 // -------------------------------------------------------------------------
59 template< class _TRawPtr, class _TLabelPtr >
60 void RandomWalker( _TLabelPtr& output, const _TRawPtr& raw, const _TLabelPtr& labels )
62 typedef typename _TRawPtr::ObjectType _TRaw;
63 typedef typename _TLabelPtr::ObjectType _TLabel;
66 // -------------------------------------------------------------------------
67 bool ParseArgs( int argc, char* argv[] )
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" );
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";
85 for( int i = 1; i < argc; i += 2 )
86 Args[ argv[ i ] + 1 ] = argv[ i + 1 ];
89 for( std::string t: mandatory )
90 complete &= ( Args.find( t ) != Args.end( ) );
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;
117 // -------------------------------------------------------------------------
118 int main( int argc, char* argv[] )
120 std::map< std::string, std::string > args;
123 if( CTBronchi::ParseArgs( args, argc, argv ) )
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, ";" ) );
134 TImage::Pointer input_image;
135 CTBronchi::ReadImage( input_image, args[ "in" ] );
138 TLabelImage::Pointer mori;
139 TInputPixel opt_thr = CTBronchi::Mori( mori, input_image, seed, args );
142 TLabelImage::Pointer labels;
143 CTBronchi::Label( labels, input_image, mori, opt_thr, args );
145 // Final segmentation
146 TScalarImage::Pointer segmentation;
147 CTBronchi::RandomWalker( segmentation, input_image, labels, args );
150 CTBronchi::WriteImage( segmentation, args[ "out" ] );
155 catch( std::exception& err )
158 << "===============================" << std::endl
159 << "Error caught: " << std::endl
161 << "===============================" << std::endl
171 if( ParseArgs( argc, argv ) )
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( ) );
179 TInputImage::Pointer input_image;
180 ReadImage( input_image, Args[ "in" ] );
183 TLabelImage::Pointer mori;
184 Mori( mori, input_image );
187 TLabelImage::Pointer labels;
188 Label( labels, input_image, mori );
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