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 clitkWarpImageGenericFilter_txx
19 #define clitkWarpImageGenericFilter_txx
21 /* =================================================
22 * @file clitkWarpImageGenericFilter.txx
28 ===================================================*/
34 //-------------------------------------------------------------------
35 // Update with the number of dimensions
36 //-------------------------------------------------------------------
37 template<unsigned int Dimension>
39 WarpImageGenericFilter::UpdateWithDim(std::string PixelType)
41 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
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>();
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>();
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>();
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>();
62 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
63 UpdateWithDimAndPixelType<Dimension, float>();
68 //-------------------------------------------------------------------
69 // Update with the number of dimensions and the pixeltype
70 //-------------------------------------------------------------------
71 template <unsigned int Dimension, class PixelType>
73 WarpImageGenericFilter::UpdateWithDimAndPixelType()
77 typedef itk::Image<PixelType, Dimension> InputImageType;
78 typedef itk::Image<PixelType, Dimension> OutputImageType;
79 typedef itk::Vector<float, Dimension> DisplacementType;
80 typedef itk::Image<DisplacementType, Dimension> DeformationFieldType;
84 typedef itk::ImageFileReader<InputImageType> InputReaderType;
85 typename InputReaderType::Pointer reader = InputReaderType::New();
86 reader->SetFileName( m_InputFileName);
88 typename InputImageType::Pointer input= reader->GetOutput();
90 //Read the deformation field
91 typedef itk::ImageFileReader<DeformationFieldType> DeformationFieldReaderType;
92 typename DeformationFieldReaderType::Pointer deformationFieldReader= DeformationFieldReaderType::New();
93 deformationFieldReader->SetFileName(m_ArgsInfo.vf_arg);
94 deformationFieldReader->Update();
95 typename DeformationFieldType::Pointer deformationField =deformationFieldReader->GetOutput();
97 // Intensity interpolator
98 typedef clitk::GenericVectorInterpolator<args_info_clitkWarpImage, DeformationFieldType, double> GenericInterpolatorType;
99 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
100 genericInterpolator->SetArgsInfo(m_ArgsInfo);
103 // -------------------------------------------
105 // -------------------------------------------
106 if (m_ArgsInfo.spacing_arg == 0) {
107 // Calculate the region
108 typename DeformationFieldType::SizeType newSize;
109 for (unsigned int i=0 ; i <Dimension; i++)
110 newSize[i]=(unsigned int) (input->GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i]/deformationField->GetSpacing()[i]);
112 // Get the interpolator
113 typedef clitk::GenericVectorInterpolator<args_info_clitkWarpImage, DeformationFieldType, double> GenericInterpolatorType;
114 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
115 genericInterpolator->SetArgsInfo(m_ArgsInfo);
117 // Resample to match the extent of the input
118 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
119 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
120 resampler->SetInput(deformationField);
121 resampler->SetOutputSpacing(deformationField->GetSpacing());
122 resampler->SetSize(newSize);
123 resampler->SetOutputOrigin(input->GetOrigin());
124 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
127 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
130 } catch( itk::ExceptionObject & excp ) {
131 std::cerr << "Problem resampling the input vector field file" << std::endl;
132 std::cerr << excp << std::endl;
135 deformationField= resampler->GetOutput();
139 // -------------------------------------------
140 // Spacing like input
141 // -------------------------------------------
144 typename DeformationFieldType::SizeType newSize;
145 for (unsigned int i=0 ; i <Dimension; i++)
146 newSize[i]=input->GetLargestPossibleRegion().GetSize()[i];
148 // Resample to match the extent of the input
149 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
150 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
151 resampler->SetInput(deformationField);
152 resampler->SetOutputSpacing(input->GetSpacing());
153 resampler->SetSize(newSize);
154 resampler->SetOutputOrigin(input->GetOrigin());
155 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
158 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
161 } catch( itk::ExceptionObject & excp ) {
162 std::cerr << "Problem resampling the input vector field file" << std::endl;
163 std::cerr << excp << std::endl;
166 deformationField= resampler->GetOutput();
170 // -------------------------------------------
172 // -------------------------------------------
173 typename itk::ImageToImageFilter<InputImageType, InputImageType>::Pointer warpFilter;
174 if (m_ArgsInfo.forward_flag) {
175 //Forward warping: always linear
176 typedef clitk::ForwardWarpImageFilter<InputImageType, InputImageType, DeformationFieldType> ForwardWarpFilterType;
177 typename ForwardWarpFilterType::Pointer forwardWarpFilter= ForwardWarpFilterType::New();
178 forwardWarpFilter->SetDeformationField( deformationField );
179 forwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
180 warpFilter=forwardWarpFilter;
183 // -------------------------------------------
185 // -------------------------------------------
187 // Get the interpolator
188 typedef clitk::GenericInterpolator<args_info_clitkWarpImage, InputImageType, double> GenericInterpolatorType;
189 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
190 genericInterpolator->SetArgsInfo(m_ArgsInfo);
193 typedef itk::WarpImageFilter<InputImageType, InputImageType, DeformationFieldType> BackwardWarpFilterType;
194 typename BackwardWarpFilterType::Pointer backwardWarpFilter= BackwardWarpFilterType::New();
195 #if ITK_VERSION_MAJOR >= 4
196 backwardWarpFilter->SetDisplacementField( deformationField );
198 backwardWarpFilter->SetDeformationField( deformationField );
200 backwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
201 backwardWarpFilter->SetOutputSpacing( deformationField->GetSpacing() );
202 backwardWarpFilter->SetOutputOrigin( input->GetOrigin() );
203 backwardWarpFilter->SetOutputSize( deformationField->GetLargestPossibleRegion().GetSize() );
204 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
205 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
206 backwardWarpFilter->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
207 warpFilter=backwardWarpFilter;
211 // -------------------------------------------
213 // -------------------------------------------
214 warpFilter->SetInput(input);
215 if (m_Verbose) std::cout<< "Warping the input..." <<std::endl;
217 warpFilter->Update();
218 } catch( itk::ExceptionObject & excp ) {
219 std::cerr << "Problem warping the input image" << std::endl;
220 std::cerr << excp << std::endl;
226 typedef itk::ImageFileWriter<OutputImageType> WriterType;
227 typename WriterType::Pointer writer = WriterType::New();
228 writer->SetFileName(m_ArgsInfo.output_arg);
229 writer->SetInput(warpFilter->GetOutput());
237 #endif //#define clitkWarpImageGenericFilter_txx