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 clitkNVectorImageTo4DImageGenericFilter_txx
19 #define clitkNVectorImageTo4DImageGenericFilter_txx
21 /* =================================================
22 * @file clitkNVectorImageTo4DImageGenericFilter.txx
28 ===================================================*/
30 #include "itkVectorImageToImageAdaptor.h"
35 //-------------------------------------------------------------------
36 // Update with the number of dimensions
37 //-------------------------------------------------------------------
38 template<unsigned int Dimension>
40 NVectorImageTo4DImageGenericFilter::UpdateWithDim(std::string PixelType, unsigned int Components)
42 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
44 if (PixelType == "short") {
45 UpdateWithDimAndPixelType<Dimension, short>();
46 } else if (PixelType == "unsigned short") {
47 UpdateWithDimAndPixelType<Dimension, unsigned short>();
48 } else if (PixelType == "unsigned_short") {
49 UpdateWithDimAndPixelType<Dimension, unsigned short>();
50 } else if (PixelType == "char") {
51 UpdateWithDimAndPixelType<Dimension, char>();
52 } else if (PixelType == "unsigned_char") {
53 UpdateWithDimAndPixelType<Dimension, unsigned char>();
54 } else if (PixelType == "int") {
55 UpdateWithDimAndPixelType<Dimension, int>();
56 } else if (PixelType == "unsigned_int") {
57 UpdateWithDimAndPixelType<Dimension, unsigned int>();
58 } else if (PixelType == "double") {
59 UpdateWithDimAndPixelType<Dimension, double>();
60 } else if (PixelType == "float") {
61 UpdateWithDimAndPixelType<Dimension, float>();
63 std::cerr << "Error, pixel type : \"" << PixelType << "\" unknown !" << std::endl;
68 //-------------------------------------------------------------------
69 // Update with the number of dimensions and the pixeltype
70 //-------------------------------------------------------------------
71 template <unsigned int Dimension, class PixelType>
73 NVectorImageTo4DImageGenericFilter::UpdateWithDimAndPixelType()
76 typedef itk::VectorImage<PixelType, Dimension> InputImageType;
77 typedef itk::Image<PixelType, Dimension+1> OutputImageType;
80 typedef itk::ImageFileReader<InputImageType> InputReaderType;
81 typename InputReaderType::Pointer reader = InputReaderType::New();
82 reader->SetFileName( m_InputFileName);
84 typename InputImageType::Pointer input= reader->GetOutput();
87 typedef itk::VectorImageToImageAdaptor<PixelType, Dimension> ImageAdaptorType;
88 typedef itk::ImageFileWriter<OutputImageType> WriterType;
89 typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
90 typename OutputImageType::Pointer output = OutputImageType::New();
91 typename WriterType::Pointer writer = WriterType::New();
93 adaptor->SetExtractComponentIndex(0);
94 adaptor->SetImage(input);
95 std::string fileName=m_ArgsInfo.output_arg;
98 typename OutputImageType::IndexType index;
100 typename OutputImageType::SizeType size;
101 size.Fill(input->GetNumberOfComponentsPerPixel());
102 typename OutputImageType::SpacingType spacing;
104 typename OutputImageType::PointType origin;
106 typename OutputImageType::DirectionType direction;
107 direction.SetIdentity();
108 for (unsigned int pixelDim=0; pixelDim<Dimension; ++pixelDim)
110 size[pixelDim]=adaptor->GetLargestPossibleRegion().GetSize(pixelDim);
111 spacing[pixelDim]=input->GetSpacing()[pixelDim];
112 origin[pixelDim]=input->GetOrigin()[pixelDim];
113 for (unsigned int pixelDim2=0; pixelDim2<Dimension; ++pixelDim2)
115 direction[pixelDim][pixelDim2]=input->GetDirection()[pixelDim][pixelDim2];
118 typename OutputImageType::RegionType region;
119 region.SetSize(size);
120 region.SetIndex(index);
121 output->SetRegions(region);
122 output->SetOrigin(origin);
123 output->SetDirection(direction);
124 output->SetSpacing(spacing);
126 writer->SetInput(output);
129 for (unsigned int pixelDim=0; pixelDim<input->GetNumberOfComponentsPerPixel(); ++pixelDim)
131 adaptor->SetExtractComponentIndex(pixelDim);
133 itk::ImageRegionIterator<InputImageType> imageIterator(input,input->GetLargestPossibleRegion());
135 while(!imageIterator.IsAtEnd())
137 typename OutputImageType::IndexType indexVector;
139 for (unsigned int indexDim=0; indexDim<Dimension; ++indexDim)
141 indexVector[indexDim]=imageIterator.GetIndex().GetElement(indexDim);
143 indexVector[Dimension]=pixelDim;
145 output->SetPixel(indexVector, adaptor->GetPixel(imageIterator.GetIndex()));
150 writer->SetFileName(fileName);
155 #endif //#define clitkNVectorImageTo4DImageGenericFilter_txx