]> Creatis software - clitk.git/blob - tools/clitkAffineTransformGenericFilter.txx
1853b02659c8e907677b74c049c0829e6c2f3526
[clitk.git] / tools / clitkAffineTransformGenericFilter.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://oncora1.lyon.fnclcc.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 clitkAffineTransformGenericFilter_txx
19 #define clitkAffineTransformGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkAffineTransformGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34   //-----------------------------------------------------------
35   // Constructor
36   //-----------------------------------------------------------
37   template<class args_info_type>
38   AffineTransformGenericFilter<args_info_type>::AffineTransformGenericFilter()
39   {
40     m_Verbose=false;
41     m_InputFileName="";
42   }
43
44
45   //-----------------------------------------------------------
46   // Update
47   //-----------------------------------------------------------
48   template<class args_info_type>
49   void AffineTransformGenericFilter<args_info_type>::Update()
50   {
51     // Read the Dimension and PixelType
52     int Dimension, Components;
53     std::string PixelType;
54     ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType, Components);
55
56     
57     // Call UpdateWithDim
58     if(Dimension==2) UpdateWithDim<2>(PixelType, Components);
59     else if(Dimension==3) UpdateWithDim<3>(PixelType, Components);
60     else if (Dimension==4)UpdateWithDim<4>(PixelType, Components); 
61     else 
62       {
63         std::cout<<"Error, Only for 2, 3 or 4  Dimensions!!!"<<std::endl ;
64         return;
65       }
66   }
67
68   //-------------------------------------------------------------------
69   // Update with the number of dimensions
70   //-------------------------------------------------------------------
71   template<class args_info_type>
72   template<unsigned int Dimension>
73   void 
74   AffineTransformGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType, int Components)
75   {
76     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<<Components<<" component(s) of "<<  PixelType<<"..."<<std::endl;
77
78     if (Components==1)
79       {
80         if(PixelType == "short"){  
81           if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
82           UpdateWithDimAndPixelType<Dimension, signed short>(); 
83         }
84         //    else if(PixelType == "unsigned_short"){  
85         //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
86         //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
87         //     }
88         
89         else if (PixelType == "unsigned_char"){ 
90           if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
91           UpdateWithDimAndPixelType<Dimension, unsigned char>();
92         }
93         
94         //     else if (PixelType == "char"){ 
95         //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
96         //       UpdateWithDimAndPixelType<Dimension, signed char>();
97         //     }
98         else {
99           if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
100           UpdateWithDimAndPixelType<Dimension, float>();
101         }
102       }
103
104     else if (Components==3)
105       {
106         if (m_Verbose) std::cout  << "Launching transform in "<< Dimension <<"D and 3D float (DVF)" << std::endl;
107         UpdateWithDimAndVectorType<Dimension, itk::Vector<float, Dimension> >();
108       }
109
110     else std::cerr<<"Number of components is "<<Components<<", not supported!"<<std::endl;
111
112   }
113
114
115   //-------------------------------------------------------------------
116   // Update with the number of dimensions and the pixeltype
117   //-------------------------------------------------------------------
118   template<class args_info_type>
119   template <unsigned int Dimension, class  PixelType> 
120   void 
121   AffineTransformGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
122   {
123
124     // ImageTypes
125     typedef itk::Image<PixelType, Dimension> InputImageType;
126     typedef itk::Image<PixelType, Dimension> OutputImageType;
127     
128     // Read the input
129     typedef itk::ImageFileReader<InputImageType> InputReaderType;
130     typename InputReaderType::Pointer reader = InputReaderType::New();
131     reader->SetFileName( m_InputFileName);
132     reader->Update();
133     typename InputImageType::Pointer input= reader->GetOutput();
134
135     //Filter
136     typedef  itk::ResampleImageFilter< InputImageType,OutputImageType >  ResampleFilterType;
137     typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
138     
139     // Matrix
140     typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
141     if (m_ArgsInfo.matrix_given)
142       {
143         matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
144         if (m_Verbose) std::cout<<"Reading the matrix..."<<std::endl;
145       }
146     else
147       { 
148         matrix.SetIdentity();
149       }
150     if (m_Verbose) std::cout<<"Using the following matrix:"<<std::endl;
151     if (m_Verbose) std::cout<<matrix<<std::endl;
152     typename itk::Matrix<double, Dimension, Dimension> rotationMatrix=clitk::GetRotationalPartMatrix(matrix);
153     typename itk::Vector<double,Dimension> translationPart= clitk::GetTranslationPartMatrix(matrix);
154     
155     // Transform
156     typedef itk::AffineTransform<double, Dimension> AffineTransformType;
157     typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
158     affineTransform->SetMatrix(rotationMatrix);
159     affineTransform->SetTranslation(translationPart);
160
161     // Interp    
162     typedef clitk::GenericInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
163     typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
164     genericInterpolator->SetArgsInfo(m_ArgsInfo);
165     
166     // Properties
167     if (m_ArgsInfo.like_given)
168       {
169         typename InputReaderType::Pointer likeReader=InputReaderType::New();
170         likeReader->SetFileName(m_ArgsInfo.like_arg);
171         likeReader->Update();
172         resampler->SetOutputParametersFromImage(likeReader->GetOutput());
173       }
174     else
175       {
176         //Size
177         typename OutputImageType::SizeType outputSize;
178         if (m_ArgsInfo.size_given) 
179           {
180             for(unsigned int i=0; i< Dimension; i++)
181               outputSize[i]=m_ArgsInfo.size_arg[i];
182           }
183         else outputSize=input->GetLargestPossibleRegion().GetSize();
184         std::cout<<"Setting the size to "<<outputSize<<"..."<<std::endl;
185         
186         //Spacing
187         typename OutputImageType::SpacingType outputSpacing;
188         if (m_ArgsInfo.spacing_given) 
189           {
190             for(unsigned int i=0; i< Dimension; i++)
191               outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
192           }
193         else outputSpacing=input->GetSpacing();
194         std::cout<<"Setting the spacing to "<<outputSpacing<<"..."<<std::endl;
195     
196         //Origin
197         typename OutputImageType::PointType outputOrigin;
198         if (m_ArgsInfo.origin_given) 
199           {
200             for(unsigned int i=0; i< Dimension; i++)
201               outputOrigin[i]=m_ArgsInfo.origin_arg[i];
202           }
203         else outputOrigin=input->GetOrigin();
204         std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
205     
206         // Set
207         resampler->SetSize( outputSize );
208         resampler->SetOutputSpacing( outputSpacing );
209         resampler->SetOutputOrigin(  outputOrigin );
210
211       }
212
213     resampler->SetInput( input );
214     resampler->SetTransform( affineTransform );
215     resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
216     resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
217
218     try
219       {
220         resampler->Update();
221       }
222     catch(itk::ExceptionObject)
223       {
224         std::cerr<<"Error resampling the image"<<std::endl;
225       }
226
227     typename OutputImageType::Pointer output = resampler->GetOutput();
228
229     // Output
230     typedef itk::ImageFileWriter<OutputImageType> WriterType;
231     typename WriterType::Pointer writer = WriterType::New();
232     writer->SetFileName(m_ArgsInfo.output_arg);
233     writer->SetInput(output);
234     writer->Update();
235
236   }
237
238   //-------------------------------------------------------------------
239   // Update with the number of dimensions and the pixeltype (components)
240   //-------------------------------------------------------------------
241   template<class args_info_type>
242   template<unsigned int Dimension, class PixelType>
243   void AffineTransformGenericFilter<args_info_type>::UpdateWithDimAndVectorType()
244   {
245     // ImageTypes
246     typedef itk::Image<PixelType, Dimension> InputImageType;
247     typedef itk::Image<PixelType, Dimension> OutputImageType;
248      
249     // Read the input
250     typedef itk::ImageFileReader<InputImageType> InputReaderType;
251     typename InputReaderType::Pointer reader = InputReaderType::New();
252     reader->SetFileName( m_InputFileName);
253     reader->Update();
254     typename InputImageType::Pointer input= reader->GetOutput();
255
256     //Filter
257     typedef  itk::VectorResampleImageFilter< InputImageType,OutputImageType, double >  ResampleFilterType;
258     typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
259     
260     // Matrix
261     typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
262     if (m_ArgsInfo.matrix_given)
263       matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
264     else 
265       matrix.SetIdentity();
266     if (m_Verbose) std::cout<<"Using the following matrix:"<<std::endl;
267     if (m_Verbose) std::cout<<matrix<<std::endl;
268     typename itk::Matrix<double, Dimension, Dimension> rotationMatrix=clitk::GetRotationalPartMatrix(matrix);
269     typename itk::Vector<double, Dimension> translationPart= clitk::GetTranslationPartMatrix(matrix);
270     
271     // Transform
272     typedef itk::AffineTransform<double, Dimension> AffineTransformType;
273     typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
274     affineTransform->SetMatrix(rotationMatrix);
275     affineTransform->SetTranslation(translationPart);
276
277     // Interp    
278     typedef clitk::GenericVectorInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
279     typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
280     genericInterpolator->SetArgsInfo(m_ArgsInfo);
281     
282     // Properties
283     if (m_ArgsInfo.like_given)
284       {
285         typename InputReaderType::Pointer likeReader=InputReaderType::New();
286         likeReader->SetFileName(m_ArgsInfo.like_arg);
287         likeReader->Update();
288         resampler->SetSize( likeReader->GetOutput()->GetLargestPossibleRegion().GetSize() );
289         resampler->SetOutputSpacing( likeReader->GetOutput()->GetSpacing() );
290         resampler->SetOutputOrigin(  likeReader->GetOutput()->GetOrigin() );
291       }
292     else
293       {
294         //Size
295         typename OutputImageType::SizeType outputSize;
296         if (m_ArgsInfo.size_given) 
297           {
298             for(unsigned int i=0; i< Dimension; i++)
299               outputSize[i]=m_ArgsInfo.size_arg[i];
300           }
301         else outputSize=input->GetLargestPossibleRegion().GetSize();
302         std::cout<<"Setting the size to "<<outputSize<<"..."<<std::endl;
303         
304         //Spacing
305         typename OutputImageType::SpacingType outputSpacing;
306         if (m_ArgsInfo.spacing_given) 
307           {
308             for(unsigned int i=0; i< Dimension; i++)
309               outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
310           }
311         else outputSpacing=input->GetSpacing();
312         std::cout<<"Setting the spacing to "<<outputSpacing<<"..."<<std::endl;
313     
314         //Origin
315         typename OutputImageType::PointType outputOrigin;
316         if (m_ArgsInfo.origin_given) 
317           {
318             for(unsigned int i=0; i< Dimension; i++)
319               outputOrigin[i]=m_ArgsInfo.origin_arg[i];
320           }
321         else outputOrigin=input->GetOrigin();
322         std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
323     
324         // Set
325         resampler->SetSize( outputSize );
326         resampler->SetOutputSpacing( outputSpacing );
327         resampler->SetOutputOrigin(  outputOrigin );
328
329       }
330
331     resampler->SetInput( input );
332     resampler->SetTransform( affineTransform );
333     resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
334     resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
335
336     try
337       {
338         resampler->Update();
339       }
340     catch(itk::ExceptionObject)
341       {
342         std::cerr<<"Error resampling the image"<<std::endl;
343       }
344
345     typename OutputImageType::Pointer output = resampler->GetOutput();
346
347     // Output
348     typedef itk::ImageFileWriter<OutputImageType> WriterType;
349     typename WriterType::Pointer writer = WriterType::New();
350     writer->SetFileName(m_ArgsInfo.output_arg);
351     writer->SetInput(output);
352     writer->Update();
353
354   }
355
356
357 } //end clitk
358  
359 #endif //#define clitkAffineTransformGenericFilter_txx