]> Creatis software - clitk.git/commitdiff
VF composition with BSpline coefficients
authorRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Wed, 18 Jan 2012 16:51:04 +0000 (17:51 +0100)
committerRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Wed, 18 Jan 2012 16:51:04 +0000 (17:51 +0100)
- output resolution taken from --like image

tools/clitkComposeVF.cxx
tools/clitkComposeVF.ggo
tools/clitkComposeVFGenericFilter.cxx
tools/clitkComposeVFGenericFilter.h
tools/clitkComposeVFGenericFilter.txx

index 72b95cf3f0fce8d02792232b05e89a0c915b2d38..c2cead24fd92e323f35d0a70ed2839c576b874ce 100644 (file)
@@ -28,14 +28,23 @@ int main( int argc, char *argv[] )
   // Init command line
   GGO(clitkComposeVF, args_info);
   CLITK_INIT;
+
+  if (args_info.type_arg == 1 && !args_info.like_given) {
+    std::cerr << "--like must be used when type = 1" << std::endl;
+    return -1;
+  }
+
   
   //Creation of the generic filter
   clitk::ComposeVFGenericFilter::Pointer ComposeVFGenericFilter= clitk::ComposeVFGenericFilter::New();
-
+  
   //Pass the parameters
   ComposeVFGenericFilter->SetInput1(args_info.input1_arg);
   ComposeVFGenericFilter->SetInput2(args_info.input2_arg);
   ComposeVFGenericFilter->SetOutput(args_info.output_arg);
+  ComposeVFGenericFilter->SetInputType(args_info.type_arg);
+  if (args_info.like_given)
+    ComposeVFGenericFilter->SetLikeImage(args_info.like_arg);
   //JV how to pass for different dims?
   //ComposeVFGenericFilter->SetEdgePaddingValue(args_info.pad_arg);
   ComposeVFGenericFilter->SetVerbose(args_info.verbose_flag);
index 52f3e20dc3dd32156f634cb839523d2b07db5f6e..bf67f8c3c4c7c0741f5145e39163417620cd047b 100644 (file)
@@ -9,6 +9,8 @@ option "config"                 -       "Config file"                   string          no
 option "input1"                i       "Input1 VF filename"            string          yes
 option "input2"                j       "Input2 VF filename"            string          yes
 option "output"                o       "Output VF filename"            string          yes
+option "type"   t "Type of input images (0: DVF, 1: b-spline coefficient images)"   int yes default="1"
+option "like"   l "Image to read output parameters from (obligatory if type=1)"    string    no
 option "pad"                   p       "Edgepadding value"             float   multiple        no  default="0.0"
 option "verbose"               v       "Verbose"                       flag            off
 
index deeaad654e9de1aa3849524ab1aa99e7f40c7e3b..03b3d148eab44f033311669f481a8ec126e5cc13 100644 (file)
@@ -25,6 +25,7 @@ namespace clitk {
   clitk::ComposeVFGenericFilter::ComposeVFGenericFilter()
   {
     m_Verbose=false;
+    m_Type = 0;
   }
 
 
index c3e8bd38fb237ac709fe8c1efd25750a08909d27..b86f22b92974d89bcc8c1f8fc0d30a0de172cde5 100644 (file)
@@ -51,6 +51,8 @@ namespace clitk
     void SetInput1(const std::string m){m_InputName1=m;}
     void SetInput2(const std::string m){m_InputName2=m;}
     void SetOutput(const std::string m){m_OutputName=m;}
+    void SetLikeImage(const std::string like){m_LikeImage=like;}
+    void SetInputType(int t){m_Type=t;}
     void SetVerbose(const bool m){m_Verbose=m;}
  
 
@@ -65,10 +67,14 @@ namespace clitk
     //Templated members
     template<unsigned int Dimension> void UpdateWithDim(std::string PixelType);
     template<unsigned int Dimension, class PixelType> void UpdateWithDimAndPixelType(); 
-
+    template<class DisplacementFieldType> typename DisplacementFieldType::Pointer CoeffsToDVF(std::string fileName, std::string likeFileName);
+    
     std::string m_InputName1;
     std::string m_InputName2;
     std::string m_OutputName;
+    std::string m_LikeImage;
+    int m_Type;
+    
     bool m_Verbose;
   
   };
index 80d1d001a9bdf172a8df55a572ed7c4fb8e770e7..07df9882fe573cafd268b4b78a83d9e914cc80f7 100644 (file)
 #define __clitkComposeVFGenericFilter_txx
 #include "clitkComposeVFGenericFilter.h"
 
+#include "clitkBSplineDeformableTransform.h"
+#include "clitkBSplineDeformableTransformInitializer.h"
+#if ITK_VERSION_MAJOR >= 4
+#include "itkTransformToDisplacementFieldSource.h"
+#else
+#include "itkTransformToDeformationFieldSource.h"
+#endif
 
 namespace clitk
 {
@@ -36,43 +43,102 @@ namespace clitk
       }
   }
 
+  template<class DisplacementFieldType>
+  typename DisplacementFieldType::Pointer ComposeVFGenericFilter::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();
+  }
+  
   template<unsigned int Dimension, class PixelType>
   void ComposeVFGenericFilter::UpdateWithDimAndPixelType()
   {
-
-    //Define the image type
     typedef itk::Vector<PixelType, Dimension> DisplacementType;
     typedef itk::Image<DisplacementType, Dimension> ImageType;
+    typename ImageType::Pointer input1, input2;
 
-    //Read the input1
-    typedef itk::ImageFileReader<ImageType> ImageReaderType;
-    typename  ImageReaderType::Pointer reader1= ImageReaderType::New();
-    reader1->SetFileName(m_InputName1);
-    reader1->Update();
-    typename ImageType::Pointer input1 =reader1->GetOutput();
-    //Read the input2
-    typename  ImageReaderType::Pointer reader2= ImageReaderType::New();
-    reader2->SetFileName(m_InputName2);
-    reader2->Update();
-    typename ImageType::Pointer input2=reader2->GetOutput();
-
-    //Create the ComposeVFFilter
-    typedef clitk::ComposeVFFilter<ImageType,ImageType> FilterType;
-    typename FilterType::Pointer filter =FilterType::New();
-    filter->SetInput1(input1);
-    filter->SetInput2(input2);
-    filter->SetVerbose(m_Verbose);
-    filter->Update();
-    
-    //Write the output
-    typedef itk::ImageFileWriter<ImageType> WriterType;
-    typename WriterType::Pointer writer = WriterType::New();
-    writer->SetFileName(m_OutputName);
-    writer->SetInput(filter->GetOutput());
-    writer->Update();
+    //Define the image type
+    if (m_Type == 1) {
+      input1 = this->CoeffsToDVF<ImageType>(m_InputName1, m_LikeImage);
+      input2 = this->CoeffsToDVF<ImageType>(m_InputName2, m_LikeImage);
+    }
+    else {
+      //Read the input1
+      typedef itk::ImageFileReader<ImageType> ImageReaderType;
+      typename  ImageReaderType::Pointer reader1= ImageReaderType::New();
+      reader1->SetFileName(m_InputName1);
+      reader1->Update();
+      input1 =reader1->GetOutput();
+
+      //Read the input2
+      typename  ImageReaderType::Pointer reader2= ImageReaderType::New();
+      reader2->SetFileName(m_InputName2);
+      reader2->Update();
+      input2=reader2->GetOutput();
+    }
 
+      //Create the ComposeVFFilter
+      typedef clitk::ComposeVFFilter<ImageType,ImageType> FilterType;
+      typename FilterType::Pointer filter =FilterType::New();
+      filter->SetInput1(input1);
+      filter->SetInput2(input2);
+      filter->SetVerbose(m_Verbose);
+      filter->Update();
+
+      //Write the output
+      typedef itk::ImageFileWriter<ImageType> WriterType;
+      typename WriterType::Pointer writer = WriterType::New();
+      writer->SetFileName(m_OutputName);
+      writer->SetInput(filter->GetOutput());
+      writer->Update();
+    
   }
 }