]> Creatis software - clitk.git/commitdiff
added tool to interpolate VFs
authorRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Thu, 24 Jan 2013 13:26:49 +0000 (14:26 +0100)
committerRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Thu, 24 Jan 2013 13:26:49 +0000 (14:26 +0100)
- support for float/double;Dim=3;Comp=3; vectors only

tools/CMakeLists.txt
tools/clitkVFInterpolate.cxx [new file with mode: 0644]
tools/clitkVFInterpolate.ggo [new file with mode: 0644]
tools/clitkVFInterpolateGenericFilter.cxx [new file with mode: 0644]
tools/clitkVFInterpolateGenericFilter.h [new file with mode: 0644]
tools/clitkVFInterpolateGenericFilter.txx [new file with mode: 0644]

index 505c2109f1d2a0b48ea66394cd629df46290ebd2..36fb8d919a2855a0940f5035b424559d40867a65 100644 (file)
@@ -54,6 +54,11 @@ IF (CLITK_BUILD_TOOLS)
   TARGET_LINK_LIBRARIES(clitkVFResample clitkCommon ${ITK_LIBRARIES})
   SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkVFResample)
 
+  WRAP_GGO(clitkVFInterpolate_GGO_C clitkVFInterpolate.ggo)
+  ADD_EXECUTABLE(clitkVFInterpolate clitkVFInterpolate.cxx clitkVFInterpolateGenericFilter.cxx ${clitkVFInterpolate_GGO_C})
+  TARGET_LINK_LIBRARIES(clitkVFInterpolate clitkCommon ${ITK_LIBRARIES})
+  SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkVFInterpolate)
+
   WRAP_GGO(clitkImageCreate_GGO_C clitkImageCreate.ggo)
   ADD_EXECUTABLE(clitkImageCreate clitkImageCreate.cxx ${clitkImageCreate_GGO_C})
   TARGET_LINK_LIBRARIES(clitkImageCreate clitkCommon  ${ITK_LIBRARIES})
diff --git a/tools/clitkVFInterpolate.cxx b/tools/clitkVFInterpolate.cxx
new file mode 100644 (file)
index 0000000..eaff9b1
--- /dev/null
@@ -0,0 +1,78 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+#ifndef CLITKVFRESAMPLE_CXX
+#define CLITKVFRESAMPLE_CXX
+
+// clitk
+#include "clitkVFInterpolate_ggo.h"
+#include "clitkIO.h"
+#include "clitkVFInterpolateGenericFilter.h"
+
+//--------------------------------------------------------------------
+int main(int argc, char * argv[])
+{
+
+    // Init command line
+    GGO(clitkVFInterpolate, args_info);
+    CLITK_INIT;
+
+    // Read input image header to check image dimension
+    itk::ImageIOBase::Pointer header = clitk::readImageHeader(args_info.input1_arg);
+    unsigned int dim = header->GetNumberOfDimensions();
+    std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
+
+    // Print image info if verbose
+    if (args_info.verbose_flag) {
+        std::cout << "Input image <" << args_info.input1_arg << "> is ";
+        clitk::printImageHeader(header, std::cout);
+        std::cout << std::endl;
+    }
+
+    // Get input size/spacing
+    std::vector<int>    inputSize;
+    std::vector<double> inputSpacing;
+    inputSize.resize(dim);
+    inputSpacing.resize(dim);
+    for (unsigned int i=0; i<dim; i++) {
+        inputSpacing[i] = header->GetSpacing(i);
+        inputSize[i] = header->GetDimensions(i);
+    }
+
+    if (args_info.verbose_flag) {
+        std::cout << "Output image will be : " << std::endl;
+    }
+
+    // Create a filter
+    clitk::VFInterpolateGenericFilter::Pointer filter = clitk::VFInterpolateGenericFilter::New();
+    filter->SetInputFilename(args_info.input1_arg);
+    filter->SetInputFilename2(args_info.input2_arg);
+    filter->SetInterpolationName(args_info.interp_arg);
+    filter->SetDistance(args_info.distance_arg);
+//     filter->SetBSplineOrder(args_info.order_arg);
+//     filter->SetBLUTSampling(args_info.sampling_arg);
+    filter->SetOutputFilename(args_info.output_arg);
+
+    // Go !
+    filter->Update();
+
+    // this is the end my friend
+    return 0;
+}// end main
+//--------------------------------------------------------------------
+
+#endif  /* end #define CLITKVFRESAMPLE_CXX */
diff --git a/tools/clitkVFInterpolate.ggo b/tools/clitkVFInterpolate.ggo
new file mode 100644 (file)
index 0000000..f6e0c1b
--- /dev/null
@@ -0,0 +1,15 @@
+#File clitkImageInterpolate.ggo
+package "clitkImageInterpolate"
+version "1.0"
+purpose "Interpolate an image. You can specify the interpolation, you can apply a Gaussian filter before."
+
+option "config"                -       "Config file"                     string        no
+option "input1"                i       "Input image filename"            string        yes
+option "input2"    j "Input image filename"      string    yes
+option "output"        o       "Output image filename"           string        yes
+option "distance"     d     "Distance (d in [0,1])"     float  yes
+option "verbose"       v       "Verbose"                         flag          off
+option "interp"                -       "Interpolation type: {nn, linear}"  string  no default="nn"
+#option "order"        b       "BSpline ordre (range 0-5)"             int     no      default="3"
+#option "sampling"     s       "BLUT sampling value"                   int     no      default="30"
+
diff --git a/tools/clitkVFInterpolateGenericFilter.cxx b/tools/clitkVFInterpolateGenericFilter.cxx
new file mode 100644 (file)
index 0000000..777bb13
--- /dev/null
@@ -0,0 +1,181 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+#ifndef CLITKVFRESAMPLEGENERICFILTER_CXX
+#define CLITKVFRESAMPLEGENERICFILTER_CXX
+
+#include "clitkVFInterpolateGenericFilter.h"
+#include "itkInterpolateImageFilter.h"
+#include "itkLinearInterpolateImageFunction.h"
+#include "itkNearestNeighborInterpolateImageFunction.h"
+#include "itkNthElementImageAdaptor.h"
+
+//--------------------------------------------------------------------
+clitk::VFInterpolateGenericFilter::VFInterpolateGenericFilter():
+  clitk::ImageToImageGenericFilter<Self>("VFInterpolate")
+{
+  //InitializeImageType<2>();
+  InitializeImageType<3>();
+  //  InitializeImageType<4>();
+  mInterpolatorName = "nn";
+  mBSplineOrder=3;
+  mDistance=0;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<unsigned int Dim>
+void clitk::VFInterpolateGenericFilter::InitializeImageType()
+{
+  //typedef itk::Vector<float,Dim> v3f;
+  //ADD_IMAGE_TYPE(Dim, v3f);
+//   ADD_VEC_IMAGE_TYPE(Dim,2,double)
+  ADD_VEC_IMAGE_TYPE(Dim,3,float)
+  ADD_VEC_IMAGE_TYPE(Dim,3,double)
+  
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+void clitk::VFInterpolateGenericFilter::UpdateWithInputImageType()
+{
+
+  if (m_NbOfComponents == 1) {
+    std::cerr << "Error, only one components ? Use clitkImageInterpolate instead." << std::endl;
+    exit(0);
+  }
+  typedef typename ImageType::PixelType PixelType;
+//   if (m_NbOfComponents == 2) Update_WithDimAndPixelTypeAndComponent<ImageType::ImageDimension,PixelType,2>();
+  if (m_NbOfComponents == 3) Update_WithDimAndPixelTypeAndComponent<ImageType::ImageDimension,PixelType,3>();
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<unsigned int Dim, class PixelType, unsigned int DimCompo>
+void clitk::VFInterpolateGenericFilter::Update_WithDimAndPixelTypeAndComponent()
+{
+  // Reading input
+  //  typedef itk::Vector<PixelType, DimCompo> DisplacementType;
+  typedef PixelType DisplacementType;
+  typedef itk::Image< DisplacementType, Dim > ImageType;
+
+  typename ImageType::Pointer input1 = clitk::readImage<ImageType>(m_InputFilenames[0], m_IOVerbose);
+  typename ImageType::Pointer input2 = clitk::readImage<ImageType>(mInputFilename2, m_IOVerbose);
+  
+  // Main filter
+  typename ImageType::Pointer outputImage = ComputeImage<ImageType>(input1, input2);
+
+  // Write results
+  SetNextOutput<ImageType>(outputImage);
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template<class ImageType>
+typename ImageType::Pointer
+clitk::VFInterpolateGenericFilter::ComputeImage(typename ImageType::Pointer inputImage1, typename ImageType::Pointer inputImage2)
+{
+
+  // Some typedefs
+  typedef itk::Image<typename ImageType::PixelType::ValueType, ImageType::ImageDimension> ScalarImageType;
+  typedef itk::Image<typename ImageType::PixelType::ValueType, ImageType::ImageDimension + 1> InterpolationImageType;
+  typedef itk::NthElementImageAdaptor<ImageType, typename ImageType::PixelType::ValueType> ImageAdaptorType;
+  typename ImageAdaptorType::Pointer adaptor1 = ImageAdaptorType::New();
+  typename ImageAdaptorType::Pointer adaptor2 = ImageAdaptorType::New();
+  adaptor1->SetImage(inputImage1);
+  adaptor2->SetImage(inputImage2);
+  
+  // Create Image Filter
+  typedef itk::InterpolateImageFilter<ImageAdaptorType, ScalarImageType> FilterType;
+  typename FilterType::Pointer filter = FilterType::New();
+  filter->SetInput1(adaptor1);
+  filter->SetInput2(adaptor2);
+  filter->SetDistance(mDistance);
+
+  // Select interpolator
+  if (mInterpolatorName == "nn") {
+    typedef itk::NearestNeighborInterpolateImageFunction<InterpolationImageType> InterpolatorType;
+    typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
+    filter->SetInterpolator(interpolator);
+  } else {
+    if (mInterpolatorName == "linear") {
+      typedef itk::LinearInterpolateImageFunction<InterpolationImageType> InterpolatorType;
+      typename InterpolatorType::Pointer interpolator =  InterpolatorType::New();
+      filter->SetInterpolator(interpolator);
+    } else {
+      std::cerr << "Sorry, I do not know the interpolator (for vector field) '" << mInterpolatorName
+                << "'. Known interpolators are :  nn, linear" << std::endl;
+      exit(0);
+    }
+  }
+
+  typename ImageType::Pointer output = ImageType::New();
+  typename ImageAdaptorType::Pointer adaptorOutput = ImageAdaptorType::New();
+  output->CopyInformation(inputImage1);
+  output->SetRegions(inputImage1->GetLargestPossibleRegion());
+  output->Allocate();
+
+  typedef itk::ImageRegionIterator<ScalarImageType> IteratorType1;
+  typedef itk::ImageRegionIterator<ImageAdaptorType> IteratorType2;
+  
+  for (unsigned int i = 0; i < ImageType::PixelType::Dimension; i++) {
+    adaptor1->SelectNthElement(i);
+    adaptor2->SelectNthElement(i);
+
+    // Go !
+    try {
+      filter->Update();
+    } catch( itk::ExceptionObject & err ) {
+      std::cerr << "Error while filtering " << m_InputFilenames[0].c_str()
+                << " " << err << std::endl;
+      exit(0);
+    }
+
+    adaptorOutput->SelectNthElement(i);
+    adaptorOutput->SetImage(output);
+    
+    IteratorType1 it1(filter->GetOutput(), filter->GetOutput()->GetLargestPossibleRegion());
+    IteratorType2 it2(adaptorOutput, adaptorOutput->GetLargestPossibleRegion());
+    
+    it1.GoToBegin();
+    it2.GoToBegin();
+    while ( ! it1.IsAtEnd() ) {
+      it2.Set(it1.Get());
+      ++it1;
+      ++it2;
+    }
+  }
+  
+  // Return result
+  return output;
+
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+void clitk::VFInterpolateGenericFilter::SetInterpolationName(const std::string & inter)
+{
+  mInterpolatorName = inter;
+}
+//--------------------------------------------------------------------
+
+#endif
+
diff --git a/tools/clitkVFInterpolateGenericFilter.h b/tools/clitkVFInterpolateGenericFilter.h
new file mode 100644 (file)
index 0000000..2d84dcf
--- /dev/null
@@ -0,0 +1,102 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to: 
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+#ifndef CLITKIMAGERESAMPLEGENERICFILTER_H
+#define CLITKIMAGERESAMPLEGENERICFILTER_H
+/**
+ -------------------------------------------------------------------
+ * @file   clitkVFInterpolateGenericFilter.h
+ * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
+ * @date   23 Feb 2008 08:37:53
+
+ * @brief  
+ -------------------------------------------------------------------*/
+
+// clitk include
+#include "clitkCommon.h"
+#include "clitkImageCommon.h"
+#include "clitkImageToImageGenericFilter.h"
+
+// itk include
+#include "itkImage.h"
+#include "itkVectorImage.h"
+#include "itkFixedArray.h"
+#include "itkImageFileReader.h"
+#include "itkImageSeriesReader.h"
+#include "itkImageFileWriter.h"
+#include "itkRecursiveGaussianImageFilter.h"
+#include "itkInterpolateImageFilter.h"
+#include "itkAffineTransform.h"
+#include "itkVectorNearestNeighborInterpolateImageFunction.h"
+#include "itkVectorLinearInterpolateImageFunction.h"
+#include "itkBSplineInterpolateImageFunction.h"
+#include "itkBSplineInterpolateImageFunctionWithLUT.h"
+#include "itkCommand.h"
+
+namespace clitk {
+  
+  //--------------------------------------------------------------------
+  class VFInterpolateGenericFilter: 
+    public clitk::ImageToImageGenericFilter<VFInterpolateGenericFilter> {
+    
+  public: 
+    // constructor
+    VFInterpolateGenericFilter();
+
+    // Types
+    typedef VFInterpolateGenericFilter       Self;
+    typedef itk::SmartPointer<Self>       Pointer;
+    typedef itk::SmartPointer<const Self> ConstPointer;
+
+    // New
+    itkNewMacro(Self);
+    
+    void SetInputFilename1(const std::string& file) { mInputFilename1 = file; }
+    void SetInputFilename2(const std::string& file) { mInputFilename2 = file; }
+    void SetInterpolationName(const std::string & inter);
+    void SetDistance(double distance) { mDistance = distance; }
+    void SetBSplineOrder(int o) { mBSplineOrder = o; }
+    void SetBLUTSampling(int b) { mSamplingFactors.resize(1); mSamplingFactors[0] = b; }
+
+   //--------------------------------------------------------------------
+    // Main function called each time the filter is updated
+    template<class InputImageType>  
+    void UpdateWithInputImageType();
+
+  protected:
+    template<unsigned int Dim> void InitializeImageType();
+    //--------------------------------------------------------------------
+    std::string mInputFilename1, mInputFilename2;
+    std::string mInterpolatorName;
+    int mBSplineOrder;
+    std::vector<int> mSamplingFactors;
+    double mDistance;
+
+    //--------------------------------------------------------------------
+    template<unsigned int Dim, class PixelType, unsigned int DimCompo> 
+    void Update_WithDimAndPixelTypeAndComponent();
+    template<class ImageType>
+    typename ImageType::Pointer ComputeImage(typename ImageType::Pointer inputImage1, typename ImageType::Pointer inputImage2);
+    
+  }; // end class VFInterpolateGenericFilter
+  //--------------------------------------------------------------------
+    
+} // end namespace
+//--------------------------------------------------------------------
+    
+#endif /* end #define CLITKIMAGERESAMPLEGENERICFILTER_H */
+
diff --git a/tools/clitkVFInterpolateGenericFilter.txx b/tools/clitkVFInterpolateGenericFilter.txx
new file mode 100644 (file)
index 0000000..7b4aea1
--- /dev/null
@@ -0,0 +1,34 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+#ifndef CLITKVFRESAMPLEGENERICFILTER_TXX
+#define CLITKVFRESAMPLEGENERICFILTER_TXX
+/**
+   ------------------------------------------------=
+   * @file   clitkVFInterpolateGenericFilter.txx
+   * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
+   * @date   23 Feb 2008 08:40:11
+   *
+   * @brief
+   *
+   *
+   ------------------------------------------------=*/
+
+//--------------------------------------------------------------------
+
+#endif /* end #define CLITKVFRESAMPLEGENERICFILTER_TXX */
+