]> Creatis software - clitk.git/commitdiff
new tool: clitkCoeffsToDVF
authorRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Wed, 25 Jan 2012 14:46:23 +0000 (15:46 +0100)
committerRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Wed, 25 Jan 2012 14:46:23 +0000 (15:46 +0100)
- converts a BLUT-coefficient image to a DVF

common/clitkCoeffsToDVF.h
tools/CMakeLists.txt
tools/clitkCoeffsToDVF.cxx [new file with mode: 0644]
tools/clitkCoeffsToDVF.ggo [new file with mode: 0644]

index 0fe30bb8c2d85127afc837227e0b1987329e7347..603ca70a35111e0e1eed09a6bb5f56f9c22afb28 100644 (file)
 #ifndef clitkCoeffsToDVF_h
 #define clitkCoeffsToDVF_h
 
+#include "itkImageFileReader.h"
+#include "itkImageIOBase.h"
+
 #include "clitkBSplineDeformableTransform.h"
+#include "clitkResampleBSplineDeformableTransformImageFilter.h"
 #if ITK_VERSION_MAJOR >= 4
 #include "itkTransformToDisplacementFieldSource.h"
 #else
 #include "itkTransformToDeformationFieldSource.h"
 #endif
+#include "itkBSplineDeformableTransform.h"
 
-//-------------------------------------------------------------------
-// Convert Coefficient image to DVF
-//-------------------------------------------------------------------
-template<class DisplacementFieldType>
-typename DisplacementFieldType::Pointer
-CoeffsToDVF(std::string fileName, std::string likeFileName, bool verbose = false)
+namespace clitk
 {
-  typedef clitk::BSplineDeformableTransform<double, DisplacementFieldType::ImageDimension, DisplacementFieldType::ImageDimension> TransformType;
-  typedef typename TransformType::CoefficientImageType CoefficientImageType;
+  //-------------------------------------------------------------------
+  // Initialize transform from coefficient images
+  //-------------------------------------------------------------------
+  template <class TransformType>
+  void 
+  SetInitialTransformParameters(typename TransformType::Pointer transform, const typename TransformType::CoefficientImageType::Pointer coefficientImage, typename TransformType::CoefficientImageType::SpacingType outputSpacing) 
+  {
+    unsigned int dim = TransformType::CoefficientImageType::ImageDimension;
+    transform->SetSplineOrder(3);
+    transform->SetGridRegion( coefficientImage->GetLargestPossibleRegion() );
+    transform->SetGridOrigin( coefficientImage->GetOrigin() );
+    transform->SetGridSpacing( coefficientImage->GetSpacing() );
+    transform->SetGridDirection( coefficientImage->GetDirection() );
+    typename TransformType::RegionType::SizeType samplingFactors;
+    for (unsigned int i=0; i< dim; i++) {
+        samplingFactors[i]= (int) ( coefficientImage->GetSpacing()[i]/ outputSpacing[i]);
+    }
+    transform->SetLUTSamplingFactors(samplingFactors);
+    
+    typedef typename TransformType::ParametersType     ParametersType;
+    const unsigned int numberOfParameters = transform->GetNumberOfParameters();
+    ParametersType params(numberOfParameters);
+    params.Fill( 0.0 );
 
-  typedef itk::ImageFileReader<CoefficientImageType> CoeffReaderType;
-  typename CoeffReaderType::Pointer reader = CoeffReaderType::New();
-  reader->SetFileName(fileName);
-  reader->Update();
+    typedef itk::ImageRegionConstIterator<typename TransformType::CoefficientImageType> Iterator;
+    Iterator it (coefficientImage, coefficientImage->GetLargestPossibleRegion() );
+    it.GoToBegin();
+    unsigned int i = 0;
+    while (! it.IsAtEnd()) {
+        for (unsigned int j = 0; j < dim; j++)
+            params[i+j]=it.Get()[j];
 
-  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
+        ++it;
+        i += dim;
+    }
 
-  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];
+    transform->SetParameters(params);
+    transform->SetBulkTransform(NULL);
   }
-  
-  if (verbose) {
-    std::cout << "Interpolating coefficients with grid:" << std::endl;
-    std::cout << output_size << output_spacing << std::endl;
+
+  //-------------------------------------------------------------------
+  // Convert Coefficient image to DVF
+  //-------------------------------------------------------------------
+  template<class DisplacementFieldType>
+  typename DisplacementFieldType::Pointer
+  BLUTCoeffsToDVF(std::string fileName, std::string likeFileName, bool verbose = false)
+  {
+    const unsigned int dim = DisplacementFieldType::ImageDimension;
+    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();
+
+  #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 < dim; 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];
+    }
+
+    typename CoefficientImageType::Pointer coeffs = reader->GetOutput();
+    typename TransformType::Pointer transform = TransformType::New();
+    SetInitialTransformParameters<TransformType>(transform, coeffs, output_spacing);
+
+    if (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();
   }
-  
-  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();
 }
 
 #endif
index 7b1a9fa7ec3b8d93dcf3d2a337907900d574ee6f..a201e18144dffd14f59c1bf05bac32006843a1db 100644 (file)
@@ -360,8 +360,12 @@ IF (CLITK_BUILD_TOOLS)
   TARGET_LINK_LIBRARIES(clitkCatImage clitkCommon ${ITK_LIBRARIES})
   SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkCatImage)
 
-  IF(CLITK_EXPERIMENTAL)
+  WRAP_GGO(clitkCoeffsToDVF_GGO_C clitkCoeffsToDVF.ggo)
+  ADD_EXECUTABLE(clitkCoeffsToDVF clitkCoeffsToDVF.cxx ${clitkCoeffsToDVF_GGO_C})
+  TARGET_LINK_LIBRARIES(clitkCoeffsToDVF clitkCommon ${ITK_LIBRARIES})
+  SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkCoeffsToDVF)
 
+  IF(CLITK_EXPERIMENTAL)
     WRAP_GGO(clitkBinaryImageToMesh_GGO_C clitkBinaryImageToMesh.ggo)
     ADD_EXECUTABLE(clitkBinaryImageToMesh clitkBinaryImageToMesh.cxx ${clitkBinaryImageToMesh_GGO_C})
     TARGET_LINK_LIBRARIES(clitkBinaryImageToMesh ${ITK_LIBRARIES} ${VTK_LIBRARIES})
@@ -379,7 +383,7 @@ IF (CLITK_BUILD_TOOLS)
   ENDIF(CLITK_EXPERIMENTAL)
 
   SET_TARGET_PROPERTIES(${TOOLS_INSTALL} PROPERTIES INSTALL_RPATH "${VTK_DIR}:${ITK_DIR}" )  
-  INSTALL (TARGETS ${TOOLS_INSTALL} DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE)
+  INSTALL (TARGETS ${TOOLS_INSTALL} DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 
 ENDIF(CLITK_BUILD_TOOLS)
 
diff --git a/tools/clitkCoeffsToDVF.cxx b/tools/clitkCoeffsToDVF.cxx
new file mode 100644 (file)
index 0000000..266f8ae
--- /dev/null
@@ -0,0 +1,56 @@
+#include "clitkCoeffsToDVF_ggo.h"
+#include "clitkCoeffsToDVF.h"
+#include "itkImage.h"
+#include "itkImageFileWriter.h"
+#include "itkImageIOFactory.h"
+#include <string>
+
+template <class DisplacementFieldType> 
+void
+Write(typename DisplacementFieldType::Pointer dvf, std::string fileName)
+{
+  typedef itk::ImageFileWriter<DisplacementFieldType> ImageWriterType;
+  typename ImageWriterType::Pointer writer = ImageWriterType::New();
+  writer->SetFileName(fileName);
+  writer->SetInput(dvf);
+  writer->Update();
+}
+
+int main(int argc, char** argv) 
+{
+  GGO(clitkCoeffsToDVF, args_info);
+  CLITK_INIT;
+
+  typename itk::ImageIOBase::Pointer image_io = itk::ImageIOFactory::CreateImageIO(args_info.input_arg, itk::ImageIOFactory::ReadMode);
+  image_io->SetFileName(args_info.input_arg);
+  image_io->ReadImageInformation();
+  
+  unsigned int ndim = image_io->GetNumberOfDimensions();
+  switch (ndim) {
+    case 2:
+    {
+      unsigned const dim = 2;
+      typedef itk::Vector<double, dim>  PixelType;
+      typedef itk::Image<PixelType, dim> DVFType;
+      typename DVFType::Pointer dvf = clitk::BLUTCoeffsToDVF<DVFType>(args_info.input_arg, args_info.like_arg);
+      Write<DVFType>(dvf, args_info.output_arg);
+    }
+    break;
+    
+    case 3:
+    {
+      unsigned const dim = 3;
+      typedef itk::Vector<double, dim>  PixelType;
+      typedef itk::Image<PixelType, dim> DVFType;
+      typename DVFType::Pointer dvf = clitk::BLUTCoeffsToDVF<DVFType>(args_info.input_arg, args_info.like_arg);
+      Write<DVFType>(dvf, args_info.output_arg);
+    }
+    break;
+    
+    default:
+      std::cerr << "Unsupported image dimension (either 2 or 3)" << std::endl;
+      return -1;
+  }
+  
+  return 0;
+}
\ No newline at end of file
diff --git a/tools/clitkCoeffsToDVF.ggo b/tools/clitkCoeffsToDVF.ggo
new file mode 100644 (file)
index 0000000..a3d9d5e
--- /dev/null
@@ -0,0 +1,12 @@
+#File clitkComposeVF.ggo
+#Author: RĂ´mulo Pinho
+#Date : Wed 24 January 2012
+
+package "clitk"
+version "Convert a BLUT-coefficient image to a vector field."
+
+option "config"       - "Config file"     string    no
+option "input"       i     "Input1 VF filename"    string    yes
+option "output"       o     "Output VF filename"    string    yes
+option "like"   l "Image to read output parameters from"    string    yes
+