]> Creatis software - clitk.git/blobdiff - common/vvImageReader.txx
Add missing clitkElastixTransformToMatrix
[clitk.git] / common / vvImageReader.txx
index 46385bebb58b13b8af00f261b368d30e67926413..3ef04c6e0487f9b6bcec51e294e4f3075a6032e0 100644 (file)
@@ -23,6 +23,7 @@
 #include <itkImageFileReader.h>
 #include <itkImageSeriesReader.h>
 #include <itkImageToVTKImageFilter.h>
+#include <itkFlexibleVectorCastImageFilter.h>
 
 #include <vtkTransform.h>
 
 template<unsigned int VImageDimension>
 void vvImageReader::UpdateWithDim(std::string InputPixelType)
 {
-  if (mType == VECTORFIELD)
-    UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
+  if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME)
+  {
+    if (VImageDimension == 4)
+      UpdateWithDimAndInputVectorPixelType<itk::Vector<float,3>,VImageDimension>();
+    else
+      UpdateWithDimAndInputVectorPixelType<itk::Vector<float,VImageDimension>,VImageDimension>();
+  }
   else if (InputPixelType == "short")
     UpdateWithDimAndInputPixelType<short,VImageDimension>();
   else if (InputPixelType == "unsigned_short")
@@ -114,10 +120,12 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
     filter->SetExtractionRegion(extractedRegion);
     filter->SetInput(reader->GetOutput());
     filter->ReleaseDataFlagOn();
+#if ITK_VERSION_MAJOR == 4
+    filter->SetDirectionCollapseToSubmatrix();
+#endif
     try {
       mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
-    }
-    catch ( itk::ExceptionObject & err ) {
+    } catch ( itk::ExceptionObject & err ) {
       std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
                 << "(slice #" << mSlice << ") " << err << std::endl;
       return;
@@ -132,7 +140,12 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
 
       try {
         if (mType == IMAGEWITHTIME)
-          mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
+        {
+          std::cerr << "We should never come here:" << std::endl
+            << "  Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl
+            << "  You are probably looking for MERGEDWITHTIME Type." << std::endl;
+          return;
+        }
         else
           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
       } catch ( itk::ExceptionObject & err ) {
@@ -150,10 +163,7 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
       reader->ReleaseDataFlagOn();
 
       try {
-        if (mType == IMAGEWITHTIME)
-          mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
-        else
-          mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
+        mImage = vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(), mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
       } catch ( itk::ExceptionObject & err ) {
         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
                   << " " << err << std::endl;
@@ -167,6 +177,57 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
 }
 //----------------------------------------------------------------------------
 
+//----------------------------------------------------------------------------
+template<class InputPixelType, unsigned int VImageDimension>
+void vvImageReader::UpdateWithDimAndInputVectorPixelType()
+{
+  typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
+  typename InputImageType::Pointer input;
+
+  if (mInputFilenames.size() > 1) {
+    typedef itk::ImageSeriesReader<InputImageType> ReaderType;
+    typename ReaderType::Pointer reader = ReaderType::New();
+    reader->SetFileNames(mInputFilenames);
+    reader->ReleaseDataFlagOn();
+    try {
+      reader->Update();
+      input = reader->GetOutput();
+    } catch ( itk::ExceptionObject & err ) {
+      std::cerr << "Error while reading image series:" << err << std::endl;
+      std::stringstream error;
+      error << err;
+      mLastError = error.str();
+      return;
+    }
+  } else {
+    typedef itk::ImageFileReader<InputImageType> ReaderType;
+    typename ReaderType::Pointer reader = ReaderType::New();
+    reader->SetFileName(mInputFilenames[0]);
+    reader->ReleaseDataFlagOn();
+    try {
+      reader->Update();
+      input = reader->GetOutput();
+    } catch ( itk::ExceptionObject & err ) {
+      std::cerr << "Error while reading " << mInputFilenames[0].c_str()
+        << " " << err << std::endl;
+      std::stringstream error;
+      error << err;
+      mLastError = error.str();
+      return;
+    }
+  }
+  
+  typedef itk::Image< itk::Vector<float , 3>, VImageDimension > VectorImageType;
+  typedef itk::FlexibleVectorCastImageFilter<InputImageType, VectorImageType> CasterType;
+  typename VectorImageType::Pointer casted_input;
+  typename CasterType::Pointer caster = CasterType::New();
+  caster->SetInput(input);
+  caster->Update();
+  casted_input = caster->GetOutput();
+  
+  mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
+}
+//----------------------------------------------------------------------------
 
 #endif /* end #define vvImageReader_TXX */