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