1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef VVIMAGEFROMITK_H
19 #define VVIMAGEFROMITK_H
25 #include <itkExtractImageFilter.h>
27 //------------------------------------------------------------------------------
28 /**Converts the itk image to vv, handling the 4D problem
29 * The time_sequence boolean specifies that the image is to be interpreted as a time sequence,
30 * even if its dim is < 4. */
32 template<unsigned int Dim, class PixelType>
33 static inline void ReadTimeSequence (vvImage::Pointer& vv_image, typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
35 typedef itk::Image< PixelType, Dim > InputImageType;
36 typedef itk::Image< PixelType, Dim - 1 > ItkImageType;
37 typedef itk::ExtractImageFilter<InputImageType, ItkImageType> FilterType;
39 //extract the 3D slices and put them in a std::vector<vtkImageData*>
40 input->UpdateOutputInformation();
41 typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
42 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
43 typename InputImageType::IndexType start = inputRegion.GetIndex();
44 typename InputImageType::SizeType extractedRegionSize = inputSize;
45 typename InputImageType::RegionType extractedRegion;
46 extractedRegionSize[Dim - 1] = 0;
47 extractedRegion.SetSize(extractedRegionSize);
49 for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
51 extractedRegion.SetIndex(start);
53 typename FilterType::Pointer filter = FilterType::New();
54 #if ITK_VERSION_MAJOR == 4
55 filter->SetDirectionCollapseToSubmatrix();
57 filter->SetExtractionRegion(extractedRegion);
58 filter->SetInput(input);
59 filter->ReleaseDataFlagOn();
60 vv_image->AddItkImage<ItkImageType>(filter->GetOutput());
62 vv_image->SetTimeSpacing(input->GetSpacing()[Dim-1]);
63 vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]);
66 template<unsigned int Dim, class PixelType>
67 struct vvImageFromITK_Impl
69 static vvImage::Pointer Do (typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
71 vvImage::Pointer vv_image=vvImage::New();
72 typedef itk::Image< PixelType, Dim > InputImageType;
74 if (time_sequence) //The time sequence case: create a series of VTK images
75 ReadTimeSequence<Dim,PixelType>(vv_image, input, time_sequence);
76 else //Dim == 1,2,3 and not time_sequence
77 vv_image->AddItkImage<InputImageType>(input);
83 template<class PixelType>
84 struct vvImageFromITK_Impl<4u, PixelType>
86 static vvImage::Pointer Do (typename itk::Image<PixelType,4u>::Pointer input, bool time_sequence=false)
88 vvImage::Pointer vv_image = vvImage::New();
89 ReadTimeSequence<4u,PixelType>(vv_image, input, time_sequence);
94 template<unsigned int Dim, class PixelType> vvImage::Pointer vvImageFromITK(typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
96 assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
97 return vvImageFromITK_Impl<Dim, PixelType>::Do(input, time_sequence);
99 //------------------------------------------------------------------------------
101 #endif //vvImageFromITK