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 clitkInvertVFGenericFilter_txx
19 #define clitkInvertVFGenericFilter_txx
21 #include "itkVectorResampleImageFilter.h"
22 #include "clitkConvertBLUTCoeffsToVFFilter.h"
24 /* =================================================
25 * @file clitkInvertVFGenericFilter.txx
31 ===================================================*/
37 //-----------------------------------------------------------
39 //-----------------------------------------------------------
40 template<class args_info_type>
41 InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
48 //-----------------------------------------------------------
50 //-----------------------------------------------------------
51 template<class args_info_type>
52 void InvertVFGenericFilter<args_info_type>::Update()
54 // Read the Dimension and PixelType
56 std::string PixelType;
57 ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
61 if(Dimension==2) UpdateWithDim<2>(PixelType);
62 else if(Dimension==3) UpdateWithDim<3>(PixelType);
63 // else if (Dimension==4)UpdateWithDim<4>(PixelType);
65 std::cout<<"Error, Only for 2 or 3 Dimensions!!!"<<std::endl ;
70 //-------------------------------------------------------------------
71 // Update with the number of dimensions
72 //-------------------------------------------------------------------
73 template<class args_info_type>
74 template<unsigned int Dimension>
76 InvertVFGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
78 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
80 // if(PixelType == "short"){
81 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
82 // UpdateWithDimAndPixelType<Dimension, signed short>();
84 // else if(PixelType == "unsigned_short"){
85 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
86 // UpdateWithDimAndPixelType<Dimension, unsigned short>();
89 // else if (PixelType == "unsigned_char"){
90 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
91 // UpdateWithDimAndPixelType<Dimension, unsigned char>();
94 // else if (PixelType == "char"){
95 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
96 // UpdateWithDimAndPixelType<Dimension, signed char>();
99 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
100 UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
105 //-------------------------------------------------------------------
106 // Update with the number of dimensions and the pixeltype
107 //-------------------------------------------------------------------
108 template<class args_info_type>
109 template <unsigned int Dimension, class PixelType>
111 InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
115 typedef itk::Image<PixelType, Dimension> InputImageType;
116 typedef itk::Image<PixelType, Dimension> OutputImageType;
119 typedef itk::ImageFileReader<InputImageType> InputReaderType;
120 typename InputReaderType::Pointer reader = InputReaderType::New();
121 reader->SetFileName( m_InputFileName);
123 typename InputImageType::Pointer input = reader->GetOutput();
126 typename OutputImageType::Pointer output;
127 switch (m_ArgsInfo.type_arg) {
131 // Create the InvertVFFilter
132 typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
133 typename FilterType::Pointer filter =FilterType::New();
134 if (m_ArgsInfo.like_given) {
135 typename FilterType::SpacingType spacing;
136 typename FilterType::SizeType size;
137 itk::ImageIOBase::Pointer header = readImageHeader(m_ArgsInfo.like_arg);
138 for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
139 size[i] = header->GetDimensions(i);
140 spacing[i] = header->GetSpacing(i);
143 typedef itk::VectorResampleImageFilter<InputImageType, OutputImageType> ResampleFilterType;
144 typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
145 resampler->SetInput(input);
146 resampler->SetOutputOrigin(input->GetOrigin());
147 resampler->SetOutputDirection(input->GetDirection());
148 resampler->SetOutputSpacing(spacing);
149 resampler->SetSize(size);
151 spacing = resampler->GetOutput()->GetSpacing();
152 size = resampler->GetOutput()->GetLargestPossibleRegion().GetSize();
153 filter->SetInput(resampler->GetOutput());
156 filter->SetInput(input);
158 filter->SetVerbose(m_Verbose);
159 if (m_ArgsInfo.threads_given) {
160 #if ITK_VERSION_MAJOR <= 4
161 filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
163 filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
166 if (m_ArgsInfo.pad_given) {
168 if (m_ArgsInfo.pad_given != (pad.GetNumberOfComponents()) )
169 pad.Fill(m_ArgsInfo.pad_arg[0]);
171 for(unsigned int i=0; i<Dimension; i++)
172 pad[i]=m_ArgsInfo.pad_arg[i];
174 filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
176 output=filter->GetOutput();
182 // Create the InvertVFFilter
183 typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
184 typename FilterType::Pointer filter =FilterType::New();
185 if (m_ArgsInfo.like_given) {
186 typedef ConvertBLUTCoeffsToVFFilter<InputImageType> VFFilterType;
187 typename VFFilterType::Pointer vf_filter = VFFilterType::New();
188 vf_filter->SetInputFileName(m_InputFileName);
189 vf_filter->SetLikeFileName(m_ArgsInfo.like_arg);
190 vf_filter->SetVerbose(m_Verbose);
192 filter->SetInput(vf_filter->GetOutput());
195 filter->SetVerbose(m_Verbose);
196 if (m_ArgsInfo.threads_given) {
197 #if ITK_VERSION_MAJOR <= 4
198 filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
200 filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
203 if (m_ArgsInfo.pad_given) {
205 if (m_ArgsInfo.pad_given != (pad.GetNumberOfComponents()) )
206 pad.Fill(m_ArgsInfo.pad_arg[0]);
208 for(unsigned int i=0; i<Dimension; i++)
209 pad[i]=m_ArgsInfo.pad_arg[i];
211 filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
213 output=filter->GetOutput();
219 // Create the InverseDeformationFieldFilter
220 typedef itk::InverseDisplacementFieldImageFilter<InputImageType,OutputImageType> FilterType;
221 typename FilterType::Pointer filter =FilterType::New();
222 filter->SetInput(input);
223 filter->SetOutputOrigin(input->GetOrigin());
224 filter->SetOutputSpacing(input->GetSpacing());
225 filter->SetSize(input->GetLargestPossibleRegion().GetSize());
226 filter->SetSubsamplingFactor(m_ArgsInfo.sampling_arg);
228 output=filter->GetOutput();
237 typedef itk::ImageFileWriter<OutputImageType> WriterType;
238 typename WriterType::Pointer writer = WriterType::New();
239 writer->SetFileName(m_ArgsInfo.output_arg);
240 writer->SetInput(output);
248 #endif //#define clitkInvertVFGenericFilter_txx