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 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)
108 // Calculate the region
109 typename DeformationFieldType::SizeType newSize;
110 for (unsigned int i=0 ; i <Dimension; i++)
111 newSize[i]=(unsigned int) (input->GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i]/deformationField->GetSpacing()[i]);
113 // Get the interpolator
114 typedef clitk::GenericVectorInterpolator<args_info_clitkWarpImage, DeformationFieldType, double> GenericInterpolatorType;
115 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
116 genericInterpolator->SetArgsInfo(m_ArgsInfo);
118 // Resample to match the extent of the input
119 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
120 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
121 resampler->SetInput(deformationField);
122 resampler->SetOutputSpacing(deformationField->GetSpacing());
123 resampler->SetSize(newSize);
124 resampler->SetOutputOrigin(input->GetOrigin());
125 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
128 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
132 catch( itk::ExceptionObject & excp ) {
133 std::cerr << "Problem resampling the input vector field file" << std::endl;
134 std::cerr << excp << std::endl;
137 deformationField= resampler->GetOutput();
141 // -------------------------------------------
142 // Spacing like input
143 // -------------------------------------------
147 typename DeformationFieldType::SizeType newSize;
148 for (unsigned int i=0 ; i <Dimension; i++)
149 newSize[i]=input->GetLargestPossibleRegion().GetSize()[i];
151 // Resample to match the extent of the input
152 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
153 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
154 resampler->SetInput(deformationField);
155 resampler->SetOutputSpacing(input->GetSpacing());
156 resampler->SetSize(newSize);
157 resampler->SetOutputOrigin(input->GetOrigin());
158 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
161 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
165 catch( itk::ExceptionObject & excp ) {
166 std::cerr << "Problem resampling the input vector field file" << std::endl;
167 std::cerr << excp << std::endl;
170 deformationField= resampler->GetOutput();
174 // -------------------------------------------
176 // -------------------------------------------
177 typename itk::ImageToImageFilter<InputImageType, InputImageType>::Pointer warpFilter;
178 if (m_ArgsInfo.forward_flag)
180 //Forward warping: always linear
181 typedef clitk::ForwardWarpImageFilter<InputImageType, InputImageType, DeformationFieldType> ForwardWarpFilterType;
182 typename ForwardWarpFilterType::Pointer forwardWarpFilter= ForwardWarpFilterType::New();
183 forwardWarpFilter->SetDeformationField( deformationField );
184 forwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
185 warpFilter=forwardWarpFilter;
188 // -------------------------------------------
190 // -------------------------------------------
193 // Get the interpolator
194 typedef clitk::GenericInterpolator<args_info_clitkWarpImage, InputImageType, double> GenericInterpolatorType;
195 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
196 genericInterpolator->SetArgsInfo(m_ArgsInfo);
199 typedef itk::WarpImageFilter<InputImageType, InputImageType, DeformationFieldType> BackwardWarpFilterType;
200 typename BackwardWarpFilterType::Pointer backwardWarpFilter= BackwardWarpFilterType::New();
201 backwardWarpFilter->SetDeformationField( deformationField );
202 backwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
203 backwardWarpFilter->SetOutputSpacing( deformationField->GetSpacing() );
204 backwardWarpFilter->SetOutputOrigin( input->GetOrigin() );
205 backwardWarpFilter->SetOutputSize( deformationField->GetLargestPossibleRegion().GetSize() );
206 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
207 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
208 backwardWarpFilter->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
209 warpFilter=backwardWarpFilter;
213 // -------------------------------------------
215 // -------------------------------------------
216 warpFilter->SetInput(input);
217 if (m_Verbose) std::cout<< "Warping the input..." <<std::endl;
219 warpFilter->Update();
221 catch( itk::ExceptionObject & excp ) {
222 std::cerr << "Problem warping the input image" << std::endl;
223 std::cerr << excp << std::endl;
229 typedef itk::ImageFileWriter<OutputImageType> WriterType;
230 typename WriterType::Pointer writer = WriterType::New();
231 writer->SetFileName(m_ArgsInfo.output_arg);
232 writer->SetInput(warpFilter->GetOutput());
240 #endif //#define clitkWarpImageGenericFilter_txx