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 clitk4DImageToNVectorImageGenericFilter_txx
19 #define clitk4DImageToNVectorImageGenericFilter_txx
21 /* =================================================
22 * @file clitk4DImageToNVectorImageGenericFilter.txx
28 ===================================================*/
30 #include "itkComposeImageFilter.h"
31 #include "itkExtractImageFilter.h"
36 //-------------------------------------------------------------------
37 // Update with the number of dimensions
38 //-------------------------------------------------------------------
39 template<unsigned int Dimension>
41 FourDImageToNVectorImageGenericFilter::UpdateWithDim(std::string PixelType, unsigned int Components)
43 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
45 if (PixelType == "short") {
46 UpdateWithDimAndPixelType<Dimension, short>();
47 } else if (PixelType == "unsigned short") {
48 UpdateWithDimAndPixelType<Dimension, unsigned short>();
49 } else if (PixelType == "unsigned_short") {
50 UpdateWithDimAndPixelType<Dimension, unsigned short>();
51 } else if (PixelType == "char") {
52 UpdateWithDimAndPixelType<Dimension, char>();
53 } else if (PixelType == "unsigned_char") {
54 UpdateWithDimAndPixelType<Dimension, unsigned char>();
55 } else if (PixelType == "int") {
56 UpdateWithDimAndPixelType<Dimension, int>();
57 } else if (PixelType == "unsigned_int") {
58 UpdateWithDimAndPixelType<Dimension, unsigned int>();
59 } else if (PixelType == "double") {
60 UpdateWithDimAndPixelType<Dimension, double>();
61 } else if (PixelType == "float") {
62 UpdateWithDimAndPixelType<Dimension, float>();
64 std::cerr << "Error, pixel type : \"" << PixelType << "\" unknown !" << std::endl;
69 //-------------------------------------------------------------------
70 // Update with the number of dimensions and the pixeltype
71 //-------------------------------------------------------------------
72 template <unsigned int Dimension, class PixelType>
74 FourDImageToNVectorImageGenericFilter::UpdateWithDimAndPixelType()
77 typedef itk::Image<PixelType, Dimension> InputImageType;
78 typedef itk::Image<PixelType, Dimension-1> MedianImageType;
79 typedef itk::VectorImage<PixelType, Dimension-1> OutputImageType;
82 typedef itk::ImageFileReader<InputImageType> InputReaderType;
83 typename InputReaderType::Pointer reader = InputReaderType::New();
84 reader->SetFileName(m_InputFileName);
86 typename InputImageType::Pointer input= reader->GetOutput();
87 std::string fileName=m_ArgsInfo.output_arg;
90 typedef itk::ComposeImageFilter<MedianImageType> ImageToVectorImageFilterType;
91 typename ImageToVectorImageFilterType::Pointer imageToVectorImageFilter = ImageToVectorImageFilterType::New();
92 typedef itk::ExtractImageFilter<InputImageType, MedianImageType> ExtractFilterType;
93 typename ExtractFilterType::Pointer extractFilter = ExtractFilterType::New();
94 extractFilter->SetDirectionCollapseToSubmatrix();
95 typedef itk::ImageFileWriter<OutputImageType> WriterType;
96 typename WriterType::Pointer writer = WriterType::New();
98 extractFilter->SetInput(input);
99 typename InputImageType::SizeType size;
100 typename OutputImageType::DirectionType direction;
101 direction.SetIdentity();
102 for (unsigned int nbDimension=0; nbDimension<Dimension-1; ++nbDimension)
104 size[nbDimension] = input->GetLargestPossibleRegion().GetSize(nbDimension);
105 for (unsigned int pixelDim2=0; pixelDim2<Dimension-1; ++pixelDim2)
107 direction[nbDimension][pixelDim2]=input->GetDirection()[nbDimension][pixelDim2];
110 size[Dimension-1] = 0;
111 typename MedianImageType::Pointer tempImage = MedianImageType::New();
113 //Extract All "time" slices
114 for (unsigned int nbSlice=0; nbSlice<input->GetLargestPossibleRegion().GetSize(Dimension-1); ++nbSlice)
116 typename InputImageType::RegionType desiredRegion;
117 typename InputImageType::IndexType index;
119 index[Dimension-1]=nbSlice;
120 desiredRegion.SetSize(size);
121 extractFilter->SetInput(input);
122 desiredRegion.SetIndex(index);
123 extractFilter->SetExtractionRegion(desiredRegion);
124 extractFilter->Update();
125 tempImage = extractFilter->GetOutput();
126 tempImage->DisconnectPipeline();
127 extractFilter->Update();
128 imageToVectorImageFilter->SetInput(nbSlice, tempImage);
131 imageToVectorImageFilter->Update();
133 // Copy transform matrix
134 imageToVectorImageFilter->GetOutput()->SetDirection(direction);
137 writer->SetInput(imageToVectorImageFilter->GetOutput());
138 writer->SetFileName(fileName);
143 #endif //#define clitk4DImageToNVectorImageGenericFilter_txx