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://www.centreleonberard.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);
62 std::cout<<"Error, Only for 2, 3 or 4 Dimensions!!!"<<std::endl ;
67 //-------------------------------------------------------------------
68 // Update with the number of dimensions
69 //-------------------------------------------------------------------
70 template<class args_info_type>
71 template<unsigned int Dimension>
73 AffineTransformGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType, int Components)
75 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<<Components<<" component(s) of "<< PixelType<<"..."<<std::endl;
78 if(PixelType == "short") {
79 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
80 UpdateWithDimAndPixelType<Dimension, signed short>();
82 // else if(PixelType == "unsigned_short"){
83 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
84 // UpdateWithDimAndPixelType<Dimension, unsigned short>();
87 else if (PixelType == "unsigned_char") {
88 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
89 UpdateWithDimAndPixelType<Dimension, unsigned char>();
92 // else if (PixelType == "char"){
93 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
94 // UpdateWithDimAndPixelType<Dimension, signed char>();
97 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
98 UpdateWithDimAndPixelType<Dimension, float>();
102 else if (Components==3) {
103 if (m_Verbose) std::cout << "Launching transform in "<< Dimension <<"D and 3D float (DVF)" << std::endl;
104 UpdateWithDimAndVectorType<Dimension, itk::Vector<float, Dimension> >();
107 else std::cerr<<"Number of components is "<<Components<<", not supported!"<<std::endl;
112 //-------------------------------------------------------------------
113 // Update with the number of dimensions and the pixeltype
114 //-------------------------------------------------------------------
115 template<class args_info_type>
116 template <unsigned int Dimension, class PixelType>
118 AffineTransformGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
122 typedef itk::Image<PixelType, Dimension> InputImageType;
123 typedef itk::Image<PixelType, Dimension> OutputImageType;
126 typedef itk::ImageFileReader<InputImageType> InputReaderType;
127 typename InputReaderType::Pointer reader = InputReaderType::New();
128 reader->SetFileName( m_InputFileName);
130 typename InputImageType::Pointer input= reader->GetOutput();
133 typedef itk::ResampleImageFilter< InputImageType,OutputImageType > ResampleFilterType;
134 typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
137 typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
138 if (m_ArgsInfo.matrix_given) {
139 matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
140 if (m_Verbose) std::cout<<"Reading the matrix..."<<std::endl;
142 matrix.SetIdentity();
144 if (m_Verbose) std::cout<<"Using the following matrix:"<<std::endl;
145 if (m_Verbose) std::cout<<matrix<<std::endl;
146 typename itk::Matrix<double, Dimension, Dimension> rotationMatrix=clitk::GetRotationalPartMatrix(matrix);
147 typename itk::Vector<double,Dimension> translationPart= clitk::GetTranslationPartMatrix(matrix);
150 typedef itk::AffineTransform<double, Dimension> AffineTransformType;
151 typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
152 affineTransform->SetMatrix(rotationMatrix);
153 affineTransform->SetTranslation(translationPart);
156 typedef clitk::GenericInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
157 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
158 genericInterpolator->SetArgsInfo(m_ArgsInfo);
161 if (m_ArgsInfo.like_given) {
162 typename InputReaderType::Pointer likeReader=InputReaderType::New();
163 likeReader->SetFileName(m_ArgsInfo.like_arg);
164 likeReader->Update();
165 resampler->SetOutputParametersFromImage(likeReader->GetOutput());
166 } else if(m_ArgsInfo.transform_grid_flag) {
167 typename itk::Matrix<double, Dimension+1, Dimension+1> invMatrix( matrix.GetInverse() );
168 typename itk::Matrix<double, Dimension, Dimension> invRotMatrix( clitk::GetRotationalPartMatrix(invMatrix) );
169 typename itk::Vector<double,Dimension> invTrans = clitk::GetTranslationPartMatrix(invMatrix);
171 // Size is converted to double, transformed and converted back to size type
172 vnl_vector<double> vnlOutputSize(Dimension);
173 for(unsigned int i=0; i< Dimension; i++) {
174 vnlOutputSize[i] = input->GetLargestPossibleRegion().GetSize()[i];
176 vnlOutputSize = invRotMatrix *
177 input->GetDirection().GetVnlMatrix() *
179 typename OutputImageType::SizeType outputSize;
180 for(unsigned int i=0; i< Dimension; i++) {
181 outputSize[i] = lrint(vnlOutputSize[i]);
183 resampler->SetSize( outputSize );
185 // Spacing can be dictly computed in the same way
186 resampler->SetOutputSpacing ( invRotMatrix *
187 input->GetDirection() *
188 input->GetSpacing() );
189 // Origin is influenced by translation but not by direction
190 resampler->SetOutputOrigin ( invRotMatrix *
195 typename OutputImageType::SizeType outputSize;
196 if (m_ArgsInfo.size_given) {
197 for(unsigned int i=0; i< Dimension; i++)
198 outputSize[i]=m_ArgsInfo.size_arg[i];
199 } else outputSize=input->GetLargestPossibleRegion().GetSize();
202 typename OutputImageType::SpacingType outputSpacing;
203 if (m_ArgsInfo.spacing_given) {
204 for(unsigned int i=0; i< Dimension; i++)
205 outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
206 } else outputSpacing=input->GetSpacing();
209 typename OutputImageType::PointType outputOrigin;
210 if (m_ArgsInfo.origin_given) {
211 for(unsigned int i=0; i< Dimension; i++)
212 outputOrigin[i]=m_ArgsInfo.origin_arg[i];
213 } else outputOrigin=input->GetOrigin();
216 resampler->SetSize( outputSize );
217 resampler->SetOutputSpacing( outputSpacing );
218 resampler->SetOutputOrigin( outputOrigin );
222 if (m_ArgsInfo.verbose_flag) {
223 std::cout << "Setting the output size to " << resampler->GetSize() << "..." << std::endl;
224 std::cout << "Setting the output spacing to " << resampler->GetOutputSpacing() << "..." << std::endl;
225 std::cout << "Setting the output origin to " << resampler->GetOutputOrigin() << "..." << std::endl;
228 resampler->SetInput( input );
229 resampler->SetTransform( affineTransform );
230 resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
231 resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
235 } catch(itk::ExceptionObject) {
236 std::cerr<<"Error resampling the image"<<std::endl;
239 typename OutputImageType::Pointer output = resampler->GetOutput();
242 typedef itk::ImageFileWriter<OutputImageType> WriterType;
243 typename WriterType::Pointer writer = WriterType::New();
244 writer->SetFileName(m_ArgsInfo.output_arg);
245 writer->SetInput(output);
250 //-------------------------------------------------------------------
251 // Update with the number of dimensions and the pixeltype (components)
252 //-------------------------------------------------------------------
253 template<class args_info_type>
254 template<unsigned int Dimension, class PixelType>
255 void AffineTransformGenericFilter<args_info_type>::UpdateWithDimAndVectorType()
258 typedef itk::Image<PixelType, Dimension> InputImageType;
259 typedef itk::Image<PixelType, Dimension> OutputImageType;
262 typedef itk::ImageFileReader<InputImageType> InputReaderType;
263 typename InputReaderType::Pointer reader = InputReaderType::New();
264 reader->SetFileName( m_InputFileName);
266 typename InputImageType::Pointer input= reader->GetOutput();
269 typedef itk::VectorResampleImageFilter< InputImageType,OutputImageType, double > ResampleFilterType;
270 typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
273 typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
274 if (m_ArgsInfo.matrix_given)
275 matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
277 matrix.SetIdentity();
278 if (m_Verbose) std::cout<<"Using the following matrix:"<<std::endl;
279 if (m_Verbose) std::cout<<matrix<<std::endl;
280 typename itk::Matrix<double, Dimension, Dimension> rotationMatrix=clitk::GetRotationalPartMatrix(matrix);
281 typename itk::Vector<double, Dimension> translationPart= clitk::GetTranslationPartMatrix(matrix);
284 typedef itk::AffineTransform<double, Dimension> AffineTransformType;
285 typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
286 affineTransform->SetMatrix(rotationMatrix);
287 affineTransform->SetTranslation(translationPart);
290 typedef clitk::GenericVectorInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
291 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
292 genericInterpolator->SetArgsInfo(m_ArgsInfo);
295 if (m_ArgsInfo.like_given) {
296 typename InputReaderType::Pointer likeReader=InputReaderType::New();
297 likeReader->SetFileName(m_ArgsInfo.like_arg);
298 likeReader->Update();
299 resampler->SetSize( likeReader->GetOutput()->GetLargestPossibleRegion().GetSize() );
300 resampler->SetOutputSpacing( likeReader->GetOutput()->GetSpacing() );
301 resampler->SetOutputOrigin( likeReader->GetOutput()->GetOrigin() );
304 typename OutputImageType::SizeType outputSize;
305 if (m_ArgsInfo.size_given) {
306 for(unsigned int i=0; i< Dimension; i++)
307 outputSize[i]=m_ArgsInfo.size_arg[i];
308 } else outputSize=input->GetLargestPossibleRegion().GetSize();
309 std::cout<<"Setting the size to "<<outputSize<<"..."<<std::endl;
312 typename OutputImageType::SpacingType outputSpacing;
313 if (m_ArgsInfo.spacing_given) {
314 for(unsigned int i=0; i< Dimension; i++)
315 outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
316 } else outputSpacing=input->GetSpacing();
317 std::cout<<"Setting the spacing to "<<outputSpacing<<"..."<<std::endl;
320 typename OutputImageType::PointType outputOrigin;
321 if (m_ArgsInfo.origin_given) {
322 for(unsigned int i=0; i< Dimension; i++)
323 outputOrigin[i]=m_ArgsInfo.origin_arg[i];
324 } else outputOrigin=input->GetOrigin();
325 std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
328 resampler->SetSize( outputSize );
329 resampler->SetOutputSpacing( outputSpacing );
330 resampler->SetOutputOrigin( outputOrigin );
334 resampler->SetInput( input );
335 resampler->SetTransform( affineTransform );
336 resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
337 resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
341 } 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