]> Creatis software - clitk.git/blobdiff - common/vvImageReader.txx
Remove clitkAffineRegistrationLib compilation when CLTK_USE_REGISTRATION is OFF
[clitk.git] / common / vvImageReader.txx
index 02b2193367dd2d7b3107e286dfccc3d32fc946ee..2eba11c81bc628f107d7f738c0a6d750fba7ef8f 100644 (file)
@@ -24,6 +24,7 @@
 #include <itkImageSeriesReader.h>
 #include <itkImageToVTKImageFilter.h>
 #include <itkFlexibleVectorCastImageFilter.h>
+#include "itkVectorImageToImageAdaptor.h"
 
 #include <vtkTransform.h>
 
@@ -86,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;
@@ -120,16 +122,97 @@ 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());
+      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;
@@ -174,6 +257,9 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
       }
     }
   }
+
+  if (mType == DICOM && !mPatientCoordinateSystem)
+    mImage->InitializeTransform();
 }
 //----------------------------------------------------------------------------