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 #if ( ITK_VERSION_MAJOR < 5 )
31 #include "itkVectorResampleImageFilter.h"
33 #include "itkResampleImageFilter.h"
35 #include "clitkConvertBLUTCoeffsToVFFilter.h"
40 //-------------------------------------------------------------------
41 // Update with the number of dimensions
42 //-------------------------------------------------------------------
43 template<unsigned int Dimension>
45 WarpImageGenericFilter::UpdateWithDim(std::string PixelType)
47 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
49 if(PixelType == "short") {
50 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
51 UpdateWithDimAndPixelType<Dimension, signed short>();
53 // else if(PixelType == "unsigned_short"){
54 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
55 // UpdateWithDimAndPixelType<Dimension, unsigned short>();
58 else if (PixelType == "unsigned_char") {
59 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
60 UpdateWithDimAndPixelType<Dimension, unsigned char>();
63 // else if (PixelType == "char"){
64 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
65 // UpdateWithDimAndPixelType<Dimension, signed char>();
68 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
69 UpdateWithDimAndPixelType<Dimension, float>();
74 //-------------------------------------------------------------------
75 // Update with the number of dimensions and the pixeltype
76 //-------------------------------------------------------------------
77 template <unsigned int Dimension, class PixelType>
79 WarpImageGenericFilter::UpdateWithDimAndPixelType()
83 typedef itk::Image<PixelType, Dimension> InputImageType;
84 typedef itk::Image<PixelType, Dimension> OutputImageType;
85 typedef itk::Vector<float, Dimension> DisplacementType;
86 typedef itk::Image<DisplacementType, Dimension> DeformationFieldType;
89 typedef itk::ImageFileReader<InputImageType> InputReaderType;
90 typename InputReaderType::Pointer reader = InputReaderType::New();
91 reader->SetFileName( m_InputFileName);
93 typename InputImageType::Pointer input= reader->GetOutput();
95 typename DeformationFieldType::Pointer deformationField;
96 if (m_ArgsInfo.coeff_given) {
97 typedef ConvertBLUTCoeffsToVFFilter<DeformationFieldType> FilterType;
98 typename FilterType::Pointer filter = FilterType::New();
99 filter->SetInputFileName(m_ArgsInfo.coeff_arg);
100 filter->SetLikeFileName(m_InputFileName);
101 filter->SetVerbose(m_Verbose);
103 deformationField = filter->GetOutput();
106 //Read the deformation field
107 typedef itk::ImageFileReader<DeformationFieldType> DeformationFieldReaderType;
108 typename DeformationFieldReaderType::Pointer deformationFieldReader= DeformationFieldReaderType::New();
109 deformationFieldReader->SetFileName(m_ArgsInfo.vf_arg);
110 deformationFieldReader->Update();
111 deformationField =deformationFieldReader->GetOutput();
114 // Intensity interpolator
115 typedef clitk::GenericVectorInterpolator<args_info_clitkWarpImage, DeformationFieldType, double> GenericInterpolatorType;
116 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
117 genericInterpolator->SetArgsInfo(m_ArgsInfo);
120 // -------------------------------------------
122 // -------------------------------------------
123 if (m_ArgsInfo.spacing_arg == 0) {
124 // Calculate the region
125 typename DeformationFieldType::SizeType newSize;
126 for (unsigned int i=0 ; i <Dimension; i++)
127 newSize[i]=(unsigned int) (input->GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i]/deformationField->GetSpacing()[i]);
129 // Get the interpolator
130 typedef clitk::GenericVectorInterpolator<args_info_clitkWarpImage, DeformationFieldType, double> GenericInterpolatorType;
131 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
132 genericInterpolator->SetArgsInfo(m_ArgsInfo);
134 // Resample to match the extent of the input
135 #if ( ITK_VERSION_MAJOR < 5 )
136 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
137 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
139 typename itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
140 resampler =itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
142 resampler->SetInput(deformationField);
143 resampler->SetOutputSpacing(deformationField->GetSpacing());
144 resampler->SetOutputDirection(deformationField->GetDirection());
145 resampler->SetSize(newSize);
146 resampler->SetOutputOrigin(input->GetOrigin());
147 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
150 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
153 } catch( itk::ExceptionObject & excp ) {
154 std::cerr << "Problem resampling the input vector field file" << std::endl;
155 std::cerr << excp << std::endl;
158 deformationField= resampler->GetOutput();
162 // -------------------------------------------
163 // Spacing like input
164 // -------------------------------------------
165 else if (!m_ArgsInfo.coeff_given) {
167 typename DeformationFieldType::SizeType newSize;
168 for (unsigned int i=0 ; i <Dimension; i++)
169 newSize[i]=input->GetLargestPossibleRegion().GetSize()[i];
171 // Resample to match the extent of the input
172 #if ( ITK_VERSION_MAJOR < 5 )
173 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
174 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
176 typename itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
177 resampler =itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
179 resampler->SetInput(deformationField);
180 resampler->SetOutputSpacing(input->GetSpacing());
181 resampler->SetOutputDirection(deformationField->GetDirection());
182 resampler->SetSize(newSize);
183 resampler->SetOutputOrigin(input->GetOrigin());
184 resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
187 if (m_Verbose) std::cout<< "Resampling the VF..." <<std::endl;
190 } catch( itk::ExceptionObject & excp ) {
191 std::cerr << "Problem resampling the input vector field file" << std::endl;
192 std::cerr << excp << std::endl;
195 deformationField= resampler->GetOutput();
199 // -------------------------------------------
201 // -------------------------------------------
202 typename itk::ImageToImageFilter<InputImageType, InputImageType>::Pointer warpFilter;
203 if (m_ArgsInfo.forward_flag) {
204 //Forward warping: always linear
205 typedef clitk::ForwardWarpImageFilter<InputImageType, InputImageType, DeformationFieldType> ForwardWarpFilterType;
206 typename ForwardWarpFilterType::Pointer forwardWarpFilter= ForwardWarpFilterType::New();
207 forwardWarpFilter->SetDeformationField( deformationField );
208 forwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
209 warpFilter=forwardWarpFilter;
212 // -------------------------------------------
214 // -------------------------------------------
216 // Get the interpolator
217 typedef clitk::GenericInterpolator<args_info_clitkWarpImage, InputImageType, double> GenericInterpolatorType;
218 typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
219 genericInterpolator->SetArgsInfo(m_ArgsInfo);
222 typedef itk::WarpImageFilter<InputImageType, InputImageType, DeformationFieldType> BackwardWarpFilterType;
223 typename BackwardWarpFilterType::Pointer backwardWarpFilter= BackwardWarpFilterType::New();
224 backwardWarpFilter->SetDisplacementField( deformationField );
225 backwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
226 backwardWarpFilter->SetOutputSpacing( deformationField->GetSpacing() );
227 backwardWarpFilter->SetOutputOrigin( input->GetOrigin() );
228 backwardWarpFilter->SetOutputSize( deformationField->GetLargestPossibleRegion().GetSize() );
229 backwardWarpFilter->SetOutputDirection( input->GetDirection() );
230 #if ( ITK_VERSION_MAJOR < 5 )
231 typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
232 resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
234 typename itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
235 resampler =itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
237 backwardWarpFilter->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
238 warpFilter=backwardWarpFilter;
242 // -------------------------------------------
244 // -------------------------------------------
245 warpFilter->SetInput(input);
246 if (m_Verbose) std::cout<< "Warping the input..." <<std::endl;
248 warpFilter->Update();
249 } catch( itk::ExceptionObject & excp ) {
250 std::cerr << "Problem warping the input image" << std::endl;
251 std::cerr << excp << std::endl;
257 typedef itk::ImageFileWriter<OutputImageType> WriterType;
258 typename WriterType::Pointer writer = WriterType::New();
259 writer->SetFileName(m_ArgsInfo.output_arg);
260 writer->SetInput(warpFilter->GetOutput());
268 #endif //#define clitkWarpImageGenericFilter_txx