]> Creatis software - clitk.git/blobdiff - tools/clitkWarpImageGenericFilter.txx
With ITKv5, change VectorResample and VectorCast Image Filter to Resample and Cast...
[clitk.git] / tools / clitkWarpImageGenericFilter.txx
index ce979aa330690a1e08375cfca90024cfefbf208e..c3f58b37d225dc41181009664831ea7f1f093808 100644 (file)
  *
  ===================================================*/
 
+#if ( ITK_VERSION_MAJOR < 5 )
 #include "itkVectorResampleImageFilter.h"
-#include "clitkBSplineDeformableTransform.h"
-#if ITK_VERSION_MAJOR >= 4
-#include "itkTransformToDisplacementFieldSource.h"
 #else
-#include "itkTransformToDeformationFieldSource.h"
+#include "itkResampleImageFilter.h"
 #endif
+#include "clitkConvertBLUTCoeffsToVFFilter.h"
 
 namespace clitk
 {
@@ -71,63 +70,6 @@ WarpImageGenericFilter::UpdateWithDim(std::string PixelType)
   }
 }
 
-//-------------------------------------------------------------------
-// Convert Coefficient image to DVF
-//-------------------------------------------------------------------
-template<class DisplacementFieldType>
-typename DisplacementFieldType::Pointer
-WarpImageGenericFilter::CoeffsToDVF(std::string fileName, std::string likeFileName)
-{
-  typedef clitk::BSplineDeformableTransform<double, DisplacementFieldType::ImageDimension, DisplacementFieldType::ImageDimension> TransformType;
-  typedef typename TransformType::CoefficientImageType CoefficientImageType;
-
-  typedef itk::ImageFileReader<CoefficientImageType> CoeffReaderType;
-  typename CoeffReaderType::Pointer reader = CoeffReaderType::New();
-  reader->SetFileName(fileName);
-  reader->Update();
-
-  typename TransformType::Pointer transform = TransformType::New();
-  transform->SetCoefficientImage(reader->GetOutput());
-  
-#if ITK_VERSION_MAJOR >= 4
-      typedef itk::TransformToDisplacementFieldSource<DisplacementFieldType, double> ConvertorType;
-#else
-      typedef itk::TransformToDeformationFieldSource<DisplacementFieldType, double> ConvertorType;
-#endif
-
-  typedef itk::ImageIOBase ImageIOType;
-  typename ImageIOType::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(likeFileName.c_str(), itk::ImageIOFactory::ReadMode);
-  imageIO->SetFileName(likeFileName);
-  imageIO->ReadImageInformation();
-
-  typename ConvertorType::Pointer convertor= ConvertorType::New();
-  typename ConvertorType::SizeType output_size;
-  typename ConvertorType::SpacingType output_spacing;
-  typename ConvertorType::OriginType output_origin;
-  typename ConvertorType::DirectionType output_direction;
-  for (unsigned int i = 0; i < DisplacementFieldType::ImageDimension; i++) {
-    output_size[i] = imageIO->GetDimensions(i);
-    output_spacing[i] = imageIO->GetSpacing(i);
-    output_origin[i] = imageIO->GetOrigin(i);
-    for (unsigned int j = 0; j < DisplacementFieldType::ImageDimension; j++)
-      output_direction[i][j] = imageIO->GetDirection(i)[j];
-  }
-  
-  if (m_Verbose) {
-    std::cout << "Interpolating coefficients with grid:" << std::endl;
-    std::cout << output_size << output_spacing << std::endl;
-  }
-  
-  convertor->SetNumberOfThreads(1);
-  convertor->SetTransform(transform);
-  convertor->SetOutputOrigin(output_origin);
-  convertor->SetOutputSpacing(output_spacing);
-  convertor->SetOutputSize(output_size);
-  convertor->SetOutputDirection(output_direction);
-  convertor->Update();
-
-  return convertor->GetOutput();
-}
 
 //-------------------------------------------------------------------
 // Update with the number of dimensions and the pixeltype
@@ -152,7 +94,13 @@ WarpImageGenericFilter::UpdateWithDimAndPixelType()
   
   typename DeformationFieldType::Pointer deformationField;
   if (m_ArgsInfo.coeff_given) {
-    deformationField = CoeffsToDVF<DeformationFieldType>(m_ArgsInfo.coeff_arg, m_InputFileName);
+    typedef ConvertBLUTCoeffsToVFFilter<DeformationFieldType> FilterType;
+    typename FilterType::Pointer filter = FilterType::New();
+    filter->SetInputFileName(m_ArgsInfo.coeff_arg);
+    filter->SetLikeFileName(m_InputFileName);
+    filter->SetVerbose(m_Verbose);
+    filter->Update();
+    deformationField = filter->GetOutput();
   }
   else {
     //Read the deformation field
@@ -184,10 +132,16 @@ WarpImageGenericFilter::UpdateWithDimAndPixelType()
     genericInterpolator->SetArgsInfo(m_ArgsInfo);
 
     // Resample to match the extent of the input
+#if ( ITK_VERSION_MAJOR < 5 )
     typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
     resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
+#else
+    typename itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
+    resampler =itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
+#endif
     resampler->SetInput(deformationField);
     resampler->SetOutputSpacing(deformationField->GetSpacing());
+    resampler->SetOutputDirection(deformationField->GetDirection());
     resampler->SetSize(newSize);
     resampler->SetOutputOrigin(input->GetOrigin());
     resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
@@ -215,10 +169,16 @@ WarpImageGenericFilter::UpdateWithDimAndPixelType()
       newSize[i]=input->GetLargestPossibleRegion().GetSize()[i];
 
     // Resample to match the extent of the input
+#if ( ITK_VERSION_MAJOR < 5 )
     typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
     resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
+#else
+    typename itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
+    resampler =itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
+#endif
     resampler->SetInput(deformationField);
     resampler->SetOutputSpacing(input->GetSpacing());
+    resampler->SetOutputDirection(deformationField->GetDirection());
     resampler->SetSize(newSize);
     resampler->SetOutputOrigin(input->GetOrigin());
     resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
@@ -261,17 +221,19 @@ WarpImageGenericFilter::UpdateWithDimAndPixelType()
     //Backward mapping
     typedef itk::WarpImageFilter<InputImageType, InputImageType, DeformationFieldType> BackwardWarpFilterType;
     typename BackwardWarpFilterType::Pointer backwardWarpFilter= BackwardWarpFilterType::New();
-#if ITK_VERSION_MAJOR >= 4
     backwardWarpFilter->SetDisplacementField( deformationField );
-#else
-    backwardWarpFilter->SetDeformationField( deformationField );
-#endif
     backwardWarpFilter->SetEdgePaddingValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
     backwardWarpFilter->SetOutputSpacing( deformationField->GetSpacing() );
     backwardWarpFilter->SetOutputOrigin( input->GetOrigin() );
     backwardWarpFilter->SetOutputSize( deformationField->GetLargestPossibleRegion().GetSize() );
+    backwardWarpFilter->SetOutputDirection( input->GetDirection() );
+#if ( ITK_VERSION_MAJOR < 5 )
     typename itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
     resampler =itk::VectorResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
+#else
+    typename itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::Pointer
+    resampler =itk::ResampleImageFilter<DeformationFieldType, DeformationFieldType >::New();
+#endif
     backwardWarpFilter->SetInterpolator(genericInterpolator->GetInterpolatorPointer());
     warpFilter=backwardWarpFilter;
   }