]> Creatis software - clitk.git/blobdiff - common/vvImageReader.txx
Change N formula
[clitk.git] / common / vvImageReader.txx
index e3c2bca72d327e25743346b604dac59ba6c11bb0..2eba11c81bc628f107d7f738c0a6d750fba7ef8f 100644 (file)
@@ -3,7 +3,7 @@
 
   Authors belong to:
   - University of LYON              http://www.universite-lyon.fr/
-  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.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
@@ -14,7 +14,7 @@
 
   - BSD        See included LICENSE.txt file
   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
-  ======================================================================-====*/
+  ===========================================================================**/
 
 #ifndef VVIMAGEREADER_TXX
 #define VVIMAGEREADER_TXX
@@ -23,6 +23,8 @@
 #include <itkImageFileReader.h>
 #include <itkImageSeriesReader.h>
 #include <itkImageToVTKImageFilter.h>
+#include <itkFlexibleVectorCastImageFilter.h>
+#include "itkVectorImageToImageAdaptor.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")
@@ -54,7 +61,7 @@ void vvImageReader::UpdateWithDim(std::string InputPixelType)
   else
     std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
 
-  if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
+  if (mLastError.size()==0) {
     //ReadNkiImageTransform();
     ReadMatImageTransform();
   }
@@ -80,6 +87,7 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
       reader->SetFileName(*i);
       try {
         mImage->AddItkImage<InputImageType>(reader->GetOutput());
+        mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension-1>(reader->GetOutput());
       } catch ( itk::ExceptionObject & err ) {
         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
                   << " " << err << std::endl;
@@ -114,14 +122,97 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
     filter->SetExtractionRegion(extractedRegion);
     filter->SetInput(reader->GetOutput());
     filter->ReleaseDataFlagOn();
+    filter->SetDirectionCollapseToSubmatrix();
     try {
       mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
-    }
-    catch ( itk::ExceptionObject & err ) {
+      mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension-1>(filter->GetOutput());
+    catch ( itk::ExceptionObject & err ) {
       std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
                 << "(slice #" << mSlice << ") " << err << std::endl;
       return;
     }
+  } else if (mType == VECTORPIXELIMAGE) {
+    mImage=vvImage::New();
+    typedef itk::VectorImage< InputPixelType, VImageDimension-1 > InputImageType;
+    typedef itk::ImageFileReader<InputImageType> ReaderType;
+    typedef itk::Image<InputPixelType, VImageDimension> OutputImageType;
+    typename ReaderType::Pointer reader = ReaderType::New();
+    reader->SetFileName(mInputFilenames[0]);
+    reader->Update();
+    typename InputImageType::Pointer input= reader->GetOutput();
+
+    typedef itk::VectorImageToImageAdaptor<InputPixelType, VImageDimension-1> ImageAdaptorType;
+    typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
+    typename OutputImageType::Pointer output = OutputImageType::New();
+
+    adaptor->SetExtractComponentIndex(0);
+    adaptor->SetImage(input);
+
+    //Create the output
+    typename OutputImageType::IndexType index;
+    index.Fill(0);
+    typename OutputImageType::SizeType size;
+    size.Fill(input->GetNumberOfComponentsPerPixel());
+    typename OutputImageType::SpacingType spacing;
+    spacing.Fill(1);
+    typename OutputImageType::PointType origin;
+    origin.Fill(0);
+    typename OutputImageType::DirectionType direction;
+    direction.SetIdentity();
+    for (unsigned int pixelDim=0; pixelDim<VImageDimension-1; ++pixelDim)
+    {
+      size[pixelDim]=adaptor->GetLargestPossibleRegion().GetSize(pixelDim);
+      spacing[pixelDim]=input->GetSpacing()[pixelDim];
+      origin[pixelDim]=input->GetOrigin()[pixelDim];
+      for (unsigned int pixelDim2=0; pixelDim2<VImageDimension-1; ++pixelDim2)
+      {
+        direction[pixelDim][pixelDim2]=input->GetDirection()[pixelDim][pixelDim2];
+      }
+    }
+    typename OutputImageType::RegionType region;
+    region.SetSize(size);
+    region.SetIndex(index);
+    output->SetRegions(region);
+    output->SetOrigin(origin);
+    output->SetSpacing(spacing);
+    output->SetDirection(direction);
+    output->Allocate();
+
+    //Copy each channel
+    for (unsigned int pixelDim=0; pixelDim<input->GetNumberOfComponentsPerPixel(); ++pixelDim)
+    {
+      adaptor->SetExtractComponentIndex(pixelDim);
+
+      itk::ImageRegionIterator<InputImageType> imageIterator(input,input->GetLargestPossibleRegion());
+
+      while(!imageIterator.IsAtEnd())
+      {
+        typename OutputImageType::IndexType indexVector;
+        indexVector.Fill(0);
+        for (unsigned int indexDim=0; indexDim<VImageDimension-1; ++indexDim)
+        {
+          indexVector[indexDim]=imageIterator.GetIndex().GetElement(indexDim);
+        }
+        indexVector[VImageDimension-1]=pixelDim;
+
+        output->SetPixel(indexVector, adaptor->GetPixel(imageIterator.GetIndex()));
+        ++imageIterator;
+      }
+    }
+
+/*    if (VImageDimension == 4)
+      mType == VECTORPIXELIMAGEWITHTIME;
+    else
+      mType == VECTORPIXELIMAGE;*/
+
+    try {
+      mImage = vvImageFromITK<VImageDimension,InputPixelType>(output, mType == VECTORPIXELIMAGEWITHTIME);
+      mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension>(output);
+    } catch ( itk::ExceptionObject & err ) {
+      std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
+                << " " << err << std::endl;
+      return;
+    }
   } else {
     if (mInputFilenames.size() > 1) {
       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
@@ -132,7 +223,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 +246,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;
@@ -164,9 +257,63 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
       }
     }
   }
+
+  if (mType == DICOM && !mPatientCoordinateSystem)
+    mImage->InitializeTransform();
 }
 //----------------------------------------------------------------------------
 
+//----------------------------------------------------------------------------
+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 */