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 ===================================================*/
30 #include "itkVectorResampleImageFilter.h"
31 #include "clitkConvertBLUTCoeffsToVFFilter.h"
36 //-------------------------------------------------------------------
37 // Update with the number of dimensions
38 //-------------------------------------------------------------------
39 template<unsigned int Dimension>
41 WarpImageGenericFilter::UpdateWithDim(std::string PixelType)
43 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
45 if(PixelType == "short") {
46 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
47 UpdateWithDimAndPixelType<Dimension, signed short>();
49 // else if(PixelType == "unsigned_short"){
50 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
51 // UpdateWithDimAndPixelType<Dimension, unsigned short>();
54 else if (PixelType == "unsigned_char") {
55 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
56 UpdateWithDimAndPixelType<Dimension, unsigned char>();
59 // else if (PixelType == "char"){
60 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
61 // UpdateWithDimAndPixelType<Dimension, signed char>();
64 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
65 UpdateWithDimAndPixelType<Dimension, float>();
70 //-------------------------------------------------------------------
71 // Update with the number of dimensions and the pixeltype
72 //-------------------------------------------------------------------
73 template <unsigned int Dimension, class PixelType>
75 WarpImageGenericFilter::UpdateWithDimAndPixelType()
79 typedef itk::Image<PixelType, Dimension> InputImageType;
80 typedef itk::Image<PixelType, Dimension> OutputImageType;
81 typedef itk::Vector<float, Dimension> DisplacementType;
82 typedef itk::Image<DisplacementType, Dimension> DeformationFieldType;
85 typedef itk::ImageFileReader<InputImageType> InputReaderType;
86 typename InputReaderType::Pointer reader = InputReaderType::New();
87 reader->SetFileName( m_InputFileName);
89 typename InputImageType::Pointer input= reader->GetOutput();
91 typename DeformationFieldType::Pointer deformationField;
92 if (m_ArgsInfo.coeff_given) {
93 typedef ConvertBLUTCoeffsToVFFilter<DeformationFieldType> FilterType;
94 typename FilterType::Pointer filter = FilterType::New();
95 filter->SetInputFileName(m_ArgsInfo.coeff_arg);
96 filter->SetLikeFileName(m_InputFileName);
97 filter->SetVerbose(m_Verbose);
99 deformationField = filter->GetOutput();
102 //Read the deformation field
103 typedef itk::ImageFileReader<DeformationFieldType> DeformationFieldReaderType;
104 typename DeformationFieldReaderType::Pointer deformationFieldReader= DeformationFieldReaderType::New();
105 deformationFieldReader->SetFileName(m_ArgsInfo.vf_arg);
106 deformationFieldReader->Update();
107 deformationField =deformationFieldReader->GetOutput();
110 // Intensity interpolator
111 typedef clitk::GenericVectorInterpolator<args_info_clitkWarpImage, DeformationFieldType, double> GenericInterpolatorType;
112 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
113 genericInterpolator->SetArgsInfo(m_ArgsInfo);
116 // -------------------------------------------
118 // -------------------------------------------
119 if (m_ArgsInfo.spacing_arg == 0) {
120 // Calculate the region
121 typename DeformationFieldType::SizeType newSize;
122 for (unsigned int i=0 ; i <Dimension; i++)
123 newSize[i]=(unsigned int) (input->GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i]/deformationField->GetSpacing()[i]);
125 // Get the interpolator
126 typedef clitk::GenericVectorInterpolator<args_info_clitkWarpImage, DeformationFieldType, double> GenericInterpolatorType;
127 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
128 genericInterpolator->SetArgsInfo(m_ArgsInfo);
130 // Resample to match the extent of the input
131 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
132 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
133 resampler->SetInput(deformationField);
134 resampler->SetOutputSpacing(deformationField->GetSpacing());
135 resampler->SetOutputDirection(deformationField->GetDirection());
136 resampler->SetSize(newSize);
137 resampler->SetOutputOrigin(input->GetOrigin());
138 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
141 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
144 } catch( itk::ExceptionObject & excp ) {
145 std::cerr << "Problem resampling the input vector field file" << std::endl;
146 std::cerr << excp << std::endl;
149 deformationField= resampler->GetOutput();
153 // -------------------------------------------
154 // Spacing like input
155 // -------------------------------------------
156 else if (!m_ArgsInfo.coeff_given) {
158 typename DeformationFieldType::SizeType newSize;
159 for (unsigned int i=0 ; i <Dimension; i++)
160 newSize[i]=input->GetLargestPossibleRegion().GetSize()[i];
162 // Resample to match the extent of the input
163 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
164 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
165 resampler->SetInput(deformationField);
166 resampler->SetOutputSpacing(input->GetSpacing());
167 resampler->SetOutputDirection(deformationField->GetDirection());
168 resampler->SetSize(newSize);
169 resampler->SetOutputOrigin(input->GetOrigin());
170 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
173 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
176 } catch( itk::ExceptionObject & excp ) {
177 std::cerr << "Problem resampling the input vector field file" << std::endl;
178 std::cerr << excp << std::endl;
181 deformationField= resampler->GetOutput();
185 // -------------------------------------------
187 // -------------------------------------------
188 typename itk::ImageToImageFilter<InputImageType, InputImageType>::Pointer warpFilter;
189 if (m_ArgsInfo.forward_flag) {
190 //Forward warping: always linear
191 typedef clitk::ForwardWarpImageFilter<InputImageType, InputImageType, DeformationFieldType> ForwardWarpFilterType;
192 typename ForwardWarpFilterType::Pointer forwardWarpFilter= ForwardWarpFilterType::New();
193 forwardWarpFilter->SetDeformationField( deformationField );
194 forwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
195 warpFilter=forwardWarpFilter;
198 // -------------------------------------------
200 // -------------------------------------------
202 // Get the interpolator
203 typedef clitk::GenericInterpolator<args_info_clitkWarpImage, InputImageType, double> GenericInterpolatorType;
204 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
205 genericInterpolator->SetArgsInfo(m_ArgsInfo);
208 typedef itk::WarpImageFilter<InputImageType, InputImageType, DeformationFieldType> BackwardWarpFilterType;
209 typename BackwardWarpFilterType::Pointer backwardWarpFilter= BackwardWarpFilterType::New();
210 backwardWarpFilter->SetDisplacementField( deformationField );
211 backwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
212 backwardWarpFilter->SetOutputSpacing( deformationField->GetSpacing() );
213 backwardWarpFilter->SetOutputOrigin( input->GetOrigin() );
214 backwardWarpFilter->SetOutputSize( deformationField->GetLargestPossibleRegion().GetSize() );
215 backwardWarpFilter->SetOutputDirection( input->GetDirection() );
216 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
217 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
218 backwardWarpFilter->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
219 warpFilter=backwardWarpFilter;
223 // -------------------------------------------
225 // -------------------------------------------
226 warpFilter->SetInput(input);
227 if (m_Verbose) std::cout<< "Warping the input..." <<std::endl;
229 warpFilter->Update();
230 } catch( itk::ExceptionObject & excp ) {
231 std::cerr << "Problem warping the input image" << std::endl;
232 std::cerr << excp << std::endl;
238 typedef itk::ImageFileWriter<OutputImageType> WriterType;
239 typename WriterType::Pointer writer = WriterType::New();
240 writer->SetFileName(m_ArgsInfo.output_arg);
241 writer->SetInput(warpFilter->GetOutput());
249 #endif //#define clitkWarpImageGenericFilter_txx