]> Creatis software - clitk.git/blob - tools/clitkFilterGenericFilter.txx
changes in license header
[clitk.git] / tools / clitkFilterGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef clitkFilterGenericFilter_txx
19 #define clitkFilterGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkFilterGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34   //-------------------------------------------------------------------
35   // Update with the number of dimensions
36   //-------------------------------------------------------------------
37   template<unsigned int Dimension>
38   void 
39   FilterGenericFilter::UpdateWithDim(std::string PixelType)
40   {
41     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
42
43 //     if(PixelType == "short"){  
44 //       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
45 //       UpdateWithDimAndPixelType<Dimension, signed short>(); 
46 //     }
47     //    else if(PixelType == "unsigned_short"){  
48     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
49     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
50     //     }
51     
52 //     else if (PixelType == "unsigned_char"){ 
53 //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
54 //       UpdateWithDimAndPixelType<Dimension, unsigned char>();
55 //     }
56     
57     //     else if (PixelType == "char"){ 
58     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
59     //       UpdateWithDimAndPixelType<Dimension, signed char>();
60     //     }
61  //    else {
62       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
63       UpdateWithDimAndPixelType<Dimension, float>();
64 //     }
65   }
66
67
68   //-------------------------------------------------------------------
69   // Update with the number of dimensions and the pixeltype
70   //-------------------------------------------------------------------
71   template <unsigned int Dimension, class  InternalPixelType> 
72   void 
73   FilterGenericFilter::UpdateWithDimAndPixelType()
74   {
75
76     // ImageTypes
77     typedef itk::Image<InternalPixelType, Dimension> InputImageType;
78     typedef itk::Image<InternalPixelType, Dimension> InternalImageType;
79     typedef itk::Image<InternalPixelType, Dimension> OutputImageType;
80     
81     // Read the input
82     typedef itk::ImageFileReader<InputImageType> InputReaderType;
83     typename InputReaderType::Pointer reader = InputReaderType::New();
84     reader->SetFileName( m_InputFileName);
85     reader->Update();
86     typename InputImageType::Pointer input= reader->GetOutput();
87
88
89     // Filter
90     typedef itk::ImageToImageFilter< InternalImageType, InternalImageType > ImageToImageFilterType;
91     typename  ImageToImageFilterType::Pointer filter;
92  
93     switch ( m_ArgsInfo.type_arg ){
94   
95     case 0:{
96       typename itk::DerivativeImageFilter< InternalImageType,InternalImageType >::Pointer df = itk::DerivativeImageFilter<InternalImageType,InternalImageType >::New();
97    
98       //Set parameters
99       df->SetDirection(m_ArgsInfo.direction_arg);
100       df->SetOrder(m_ArgsInfo.order_arg);
101       filter=df;
102       if (m_Verbose) std::cout<<"Using the derivative image filter with order "<<m_ArgsInfo.order_arg <<" on direction "<< m_ArgsInfo.direction_arg << "..." << std::endl;
103       break;
104     }
105     case 1:{
106       typename itk::GradientMagnitudeImageFilter< InternalImageType,InternalImageType >::Pointer gf = itk::GradientMagnitudeImageFilter<InternalImageType,InternalImageType >::New();
107    
108       //Set parameters
109       gf->SetUseImageSpacingOn();
110
111       filter=gf;
112       if (m_Verbose) std::cout<<"Using the gradient magnitude image filter... "<< std::endl;
113       break;
114     }
115     case 2:{
116
117       switch ( m_ArgsInfo.projection_arg ){
118       
119       case 0:{
120         //Sum
121         typename itk::SumProjectionImageFilter< InternalImageType,InternalImageType >::Pointer pf = itk::SumProjectionImageFilter<InternalImageType,InternalImageType>::New();
122         //Set parameters
123         pf->SetProjectionDimension(m_ArgsInfo.direction_arg);
124       
125         filter=pf;
126         if (m_Verbose) std::cout<<"Using the Sum projection image filter on the dimension "<<  m_ArgsInfo.direction_arg << "..."<<std::endl;
127         break;
128       }
129       case 1:{
130         //Max
131         typename itk::MaximumProjectionImageFilter< InternalImageType,InternalImageType >::Pointer pf = itk::MaximumProjectionImageFilter<InternalImageType,InternalImageType>::New();
132         //Set parameters
133         pf->SetProjectionDimension(m_ArgsInfo.direction_arg);
134       
135         filter=pf;
136         if (m_Verbose) std::cout<<"Using the maximum intensity projection image filter on the dimension "<<  m_ArgsInfo.direction_arg << "..."<<std::endl;
137         break;
138       }
139       case 2:{
140         //Sum
141         typename itk::MinimumProjectionImageFilter< InternalImageType,InternalImageType >::Pointer pf = itk::MinimumProjectionImageFilter<InternalImageType,InternalImageType>::New();
142         //Set parameters
143         pf->SetProjectionDimension(m_ArgsInfo.direction_arg);
144       
145         filter=pf;
146         if (m_Verbose) std::cout<<"Using the minimum intensity projection image filter on the dimension "<<  m_ArgsInfo.direction_arg << "..."<<std::endl;
147         break;
148       }
149       }
150       break;
151     }//end case projection filter 
152
153     case 3: {
154       typename itk::IntensityWindowingImageFilter< InternalImageType,InternalImageType >::Pointer pf 
155         = itk::IntensityWindowingImageFilter<InternalImageType,InternalImageType>::New();
156       //Set parameters
157       pf->SetWindowMinimum((InternalPixelType) m_ArgsInfo.inputMin_arg);
158       pf->SetWindowMaximum((InternalPixelType) m_ArgsInfo.inputMax_arg);
159       pf->SetOutputMinimum((InternalPixelType) m_ArgsInfo.outputMin_arg);
160       pf->SetOutputMaximum((InternalPixelType) m_ArgsInfo.outputMax_arg);
161
162       filter=pf;
163       if (m_Verbose) std::cout<<"Using the window intensity image filter with the window min and max being  "
164                               <<  m_ArgsInfo.inputMin_arg << " and "<< m_ArgsInfo.inputMax_arg
165                               <<" and the output min and max being "<<m_ArgsInfo.outputMin_arg<<" and "
166                               << m_ArgsInfo.outputMax_arg << std::endl;
167       break;
168     }
169     case 4: {
170
171       //Downsample using bsplines: four types of resamplers
172       typedef itk::BSplineResampleImageFilterBase<InternalImageType, InternalImageType> ResamplerType;
173       typedef itk::BSplineCenteredResampleImageFilterBase<InternalImageType, InternalImageType> CenteredResamplerType;
174       typedef itk::BSplineL2ResampleImageFilterBase<InternalImageType, InternalImageType> L2ResamplerType;
175       typedef itk::BSplineCenteredL2ResampleImageFilterBase<InternalImageType, InternalImageType> CenteredL2ResamplerType;
176     
177       //ResamplerType
178       if(m_ArgsInfo.resamplerType_arg==0)
179         {
180           typename itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType, ResamplerType >::Pointer df 
181             = itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType, ResamplerType >::New();
182           df->SetSplineOrder(m_ArgsInfo.splineOrder_arg);
183           filter=df;
184           if (m_Verbose) std::cout<<"Using the BSpline downsample image filter with Standard Resampler"<<std::endl;
185         }
186       //CenteredResamplerType
187       else if(m_ArgsInfo.resamplerType_arg==1)
188         {
189           typename itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType, CenteredResamplerType >::Pointer df 
190             = itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType, CenteredResamplerType >::New();
191           df->SetSplineOrder(m_ArgsInfo.splineOrder_arg);
192           filter=df;
193           if (m_Verbose) std::cout<<"Using the BSpline downsample image filter with  Centered Resampler"<<std::endl;
194         }
195       //L2ResamplerType
196       else if(m_ArgsInfo.resamplerType_arg==2)
197         {
198           typename itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType, L2ResamplerType >::Pointer df 
199             = itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType,L2ResamplerType >::New();
200           df->SetSplineOrder(m_ArgsInfo.splineOrder_arg);       
201           filter=df;
202           if (m_Verbose) std::cout<<"Using the BSpline downsample image filter with L2 Resampler"<<std::endl;
203         }
204       else if(m_ArgsInfo.resamplerType_arg==3)
205         {
206           typename itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType, CenteredL2ResamplerType >::Pointer df 
207             = itk::BSplineDownsampleImageFilter< InternalImageType,InternalImageType,CenteredL2ResamplerType >::New();
208           df->SetSplineOrder(m_ArgsInfo.splineOrder_arg);
209           filter=df;
210           if (m_Verbose) std::cout<<"Using the BSpline downsample image filter with L2 Centered Resampler"<<std::endl;
211         }
212       break;
213     }
214     case 5: {
215       typedef itk::NormalizeImageFilter<InternalImageType, InternalImageType> NormalizeFilterType;
216       typename  NormalizeFilterType::Pointer  df = NormalizeFilterType::New(); 
217       filter=df;
218       if (m_Verbose) std::cout <<  "Normalizing image intensities to a zero mean and 1 SD (float!)..." << std::endl;
219       break;
220     }
221 //     case 6: {
222 //       typedef itk::AnisotropicDiffusionImageFilter<InternalImageType, InternalImageType> FilterType;
223 //       typename FilterType::Pointer  df = FilterType::New(); 
224 //       df->SetConductanceParameter(m_ArgsInfo.cond_arg);
225 //       df->SetNumberOfIterations(m_ArgsInfo.iter_arg);
226 //       df->SetTimeStep(m_ArgsInfo.time_arg);
227 //       df->SetUseImageSpacing(true);
228       
229 //       filter=df;
230 //       if (m_Verbose) std::cout <<  "Using the anisotropic diffusion image filter..." << std::endl;
231 //       break;
232 //     }
233
234     case 7: {
235       typedef itk::GradientAnisotropicDiffusionImageFilter<InternalImageType, InternalImageType> FilterType;
236       typename FilterType::Pointer  df = FilterType::New(); 
237       df->SetConductanceParameter(m_ArgsInfo.cond_arg);
238       df->SetNumberOfIterations(m_ArgsInfo.iter_arg);
239       df->SetTimeStep(m_ArgsInfo.time_arg);
240       df->SetUseImageSpacing(true);
241       
242       filter=df;
243       if (m_Verbose) std::cout <<  "Using the gradient anisotropic diffusion image filter..." << std::endl;
244       break;
245     }
246
247     case 8: {
248       typedef itk::CurvatureAnisotropicDiffusionImageFilter<InternalImageType, InternalImageType> FilterType;
249       typename  FilterType::Pointer  df = FilterType::New(); 
250       df->SetConductanceParameter(m_ArgsInfo.cond_arg);
251       df->SetNumberOfIterations(m_ArgsInfo.iter_arg);
252       df->SetTimeStep(m_ArgsInfo.time_arg);
253       df->SetUseImageSpacing(true);
254       
255       filter=df;
256       if (m_Verbose) std::cout <<  "Using the Curvature anisotropic diffusion image filter..." << std::endl;
257       break;
258     }
259
260     case 9: {
261       typedef itk::CurvatureFlowImageFilter<InternalImageType, InternalImageType> FilterType;
262       typename  FilterType::Pointer  df = FilterType::New(); 
263       df->SetTimeStep(m_ArgsInfo.time_arg);
264       df->SetUseImageSpacing(true);
265       df->SetNumberOfIterations(m_ArgsInfo.iter_arg);
266       filter=df;
267       if (m_Verbose) std::cout <<  "Using the curvature flow image filter......" << std::endl;
268       break;
269     }
270
271       
272     }//end switch filterType
273   
274
275     //==================================================================
276     //execute the filter
277     filter->SetInput(input);
278   
279   
280     if (m_Verbose)std::cout<<"Starting filter..."<<std::endl;
281
282     try {
283       filter->Update();
284     }
285     catch( itk::ExceptionObject & err ) 
286       { 
287         std::cerr << "ExceptionObject caught! Filter failed!" << std::endl; 
288         std::cerr << err << std::endl; 
289         return;
290       } 
291     
292     // Output
293     typedef itk::ImageFileWriter<OutputImageType> WriterType;
294     typename WriterType::Pointer writer = WriterType::New();
295     writer->SetFileName(m_ArgsInfo.output_arg);
296     writer->SetInput(filter->GetOutput());
297     writer->Update();
298
299   }
300
301
302 }//end clitk
303  
304 #endif //#define clitkFilterGenericFilter_txx