1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 /* =================================================
22 * @file clitkAffineTransformGenericFilter.txx
28 ===================================================*/
34 //-----------------------------------------------------------
36 //-----------------------------------------------------------
37 template<class args_info_type>
38 AffineTransformGenericFilter<args_info_type>::AffineTransformGenericFilter()
45 //-----------------------------------------------------------
47 //-----------------------------------------------------------
48 template<class args_info_type>
49 void AffineTransformGenericFilter<args_info_type>::Update()
51 // Read the Dimension and PixelType
52 int Dimension, Components;
53 std::string PixelType;
54 ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType, Components);
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);
63 std::cout<<"Error, Only for 2, 3 or 4 Dimensions!!!"<<std::endl ;
68 //-------------------------------------------------------------------
69 // Update with the number of dimensions
70 //-------------------------------------------------------------------
71 template<class args_info_type>
72 template<unsigned int Dimension>
74 AffineTransformGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType, int Components)
76 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<<Components<<" component(s) of "<< PixelType<<"..."<<std::endl;
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>();
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>();
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>();
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>();
99 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
100 UpdateWithDimAndPixelType<Dimension, float>();
104 else if (Components==3)
106 if (m_Verbose) std::cout << "Launching transform in "<< Dimension <<"D and 3D float (DVF)" << std::endl;
107 UpdateWithDimAndVectorType<Dimension, itk::Vector<float, Dimension> >();
110 else std::cerr<<"Number of components is "<<Components<<", not supported!"<<std::endl;
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>
121 AffineTransformGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
125 typedef itk::Image<PixelType, Dimension> InputImageType;
126 typedef itk::Image<PixelType, Dimension> OutputImageType;
129 typedef itk::ImageFileReader<InputImageType> InputReaderType;
130 typename InputReaderType::Pointer reader = InputReaderType::New();
131 reader->SetFileName( m_InputFileName);
133 typename InputImageType::Pointer input= reader->GetOutput();
136 typedef itk::ResampleImageFilter< InputImageType,OutputImageType > ResampleFilterType;
137 typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
140 typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
141 if (m_ArgsInfo.matrix_given)
143 matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
144 if (m_Verbose) std::cout<<"Reading the matrix..."<<std::endl;
148 matrix.SetIdentity();
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);
156 typedef itk::AffineTransform<double, Dimension> AffineTransformType;
157 typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
158 affineTransform->SetMatrix(rotationMatrix);
159 affineTransform->SetTranslation(translationPart);
162 typedef clitk::GenericInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
163 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
164 genericInterpolator->SetArgsInfo(m_ArgsInfo);
167 if (m_ArgsInfo.like_given)
169 typename InputReaderType::Pointer likeReader=InputReaderType::New();
170 likeReader->SetFileName(m_ArgsInfo.like_arg);
171 likeReader->Update();
172 resampler->SetOutputParametersFromImage(likeReader->GetOutput());
177 typename OutputImageType::SizeType outputSize;
178 if (m_ArgsInfo.size_given)
180 for(unsigned int i=0; i< Dimension; i++)
181 outputSize[i]=m_ArgsInfo.size_arg[i];
183 else outputSize=input->GetLargestPossibleRegion().GetSize();
184 std::cout<<"Setting the size to "<<outputSize<<"..."<<std::endl;
187 typename OutputImageType::SpacingType outputSpacing;
188 if (m_ArgsInfo.spacing_given)
190 for(unsigned int i=0; i< Dimension; i++)
191 outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
193 else outputSpacing=input->GetSpacing();
194 std::cout<<"Setting the spacing to "<<outputSpacing<<"..."<<std::endl;
197 typename OutputImageType::PointType outputOrigin;
198 if (m_ArgsInfo.origin_given)
200 for(unsigned int i=0; i< Dimension; i++)
201 outputOrigin[i]=m_ArgsInfo.origin_arg[i];
203 else outputOrigin=input->GetOrigin();
204 std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
207 resampler->SetSize( outputSize );
208 resampler->SetOutputSpacing( outputSpacing );
209 resampler->SetOutputOrigin( outputOrigin );
213 resampler->SetInput( input );
214 resampler->SetTransform( affineTransform );
215 resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
216 resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
222 catch(itk::ExceptionObject)
224 std::cerr<<"Error resampling the image"<<std::endl;
227 typename OutputImageType::Pointer output = resampler->GetOutput();
230 typedef itk::ImageFileWriter<OutputImageType> WriterType;
231 typename WriterType::Pointer writer = WriterType::New();
232 writer->SetFileName(m_ArgsInfo.output_arg);
233 writer->SetInput(output);
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()
246 typedef itk::Image<PixelType, Dimension> InputImageType;
247 typedef itk::Image<PixelType, Dimension> OutputImageType;
250 typedef itk::ImageFileReader<InputImageType> InputReaderType;
251 typename InputReaderType::Pointer reader = InputReaderType::New();
252 reader->SetFileName( m_InputFileName);
254 typename InputImageType::Pointer input= reader->GetOutput();
257 typedef itk::VectorResampleImageFilter< InputImageType,OutputImageType, double > ResampleFilterType;
258 typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
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);
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);
272 typedef itk::AffineTransform<double, Dimension> AffineTransformType;
273 typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
274 affineTransform->SetMatrix(rotationMatrix);
275 affineTransform->SetTranslation(translationPart);
278 typedef clitk::GenericVectorInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
279 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
280 genericInterpolator->SetArgsInfo(m_ArgsInfo);
283 if (m_ArgsInfo.like_given)
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() );
295 typename OutputImageType::SizeType outputSize;
296 if (m_ArgsInfo.size_given)
298 for(unsigned int i=0; i< Dimension; i++)
299 outputSize[i]=m_ArgsInfo.size_arg[i];
301 else outputSize=input->GetLargestPossibleRegion().GetSize();
302 std::cout<<"Setting the size to "<<outputSize<<"..."<<std::endl;
305 typename OutputImageType::SpacingType outputSpacing;
306 if (m_ArgsInfo.spacing_given)
308 for(unsigned int i=0; i< Dimension; i++)
309 outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
311 else outputSpacing=input->GetSpacing();
312 std::cout<<"Setting the spacing to "<<outputSpacing<<"..."<<std::endl;
315 typename OutputImageType::PointType outputOrigin;
316 if (m_ArgsInfo.origin_given)
318 for(unsigned int i=0; i< Dimension; i++)
319 outputOrigin[i]=m_ArgsInfo.origin_arg[i];
321 else outputOrigin=input->GetOrigin();
322 std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
325 resampler->SetSize( outputSize );
326 resampler->SetOutputSpacing( outputSpacing );
327 resampler->SetOutputOrigin( outputOrigin );
331 resampler->SetInput( input );
332 resampler->SetTransform( affineTransform );
333 resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
334 resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
340 catch(itk::ExceptionObject)
342 std::cerr<<"Error resampling the image"<<std::endl;
345 typename OutputImageType::Pointer output = resampler->GetOutput();
348 typedef itk::ImageFileWriter<OutputImageType> WriterType;
349 typename WriterType::Pointer writer = WriterType::New();
350 writer->SetFileName(m_ArgsInfo.output_arg);
351 writer->SetInput(output);
359 #endif //#define clitkAffineTransformGenericFilter_txx