]> Creatis software - clitk.git/blob - tools/clitkInvertVFGenericFilter.txx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / tools / clitkInvertVFGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
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
20
21 #include "itkVectorResampleImageFilter.h"
22 #include "clitkConvertBLUTCoeffsToVFFilter.h"
23
24 /* =================================================
25  * @file   clitkInvertVFGenericFilter.txx
26  * @author
27  * @date
28  *
29  * @brief
30  *
31  ===================================================*/
32
33
34 namespace clitk
35 {
36
37 //-----------------------------------------------------------
38 // Constructor
39 //-----------------------------------------------------------
40 template<class args_info_type>
41 InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
42 {
43   m_Verbose=false;
44   m_InputFileName="";
45 }
46
47
48 //-----------------------------------------------------------
49 // Update
50 //-----------------------------------------------------------
51 template<class args_info_type>
52 void InvertVFGenericFilter<args_info_type>::Update()
53 {
54   // Read the Dimension and PixelType
55   int Dimension;
56   std::string PixelType;
57   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
58
59
60   // Call UpdateWithDim
61   if(Dimension==2) UpdateWithDim<2>(PixelType);
62   else if(Dimension==3) UpdateWithDim<3>(PixelType);
63   // else if (Dimension==4)UpdateWithDim<4>(PixelType);
64   else {
65     std::cout<<"Error, Only for 2 or 3  Dimensions!!!"<<std::endl ;
66     return;
67   }
68 }
69
70 //-------------------------------------------------------------------
71 // Update with the number of dimensions
72 //-------------------------------------------------------------------
73 template<class args_info_type>
74 template<unsigned int Dimension>
75 void
76 InvertVFGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
77 {
78   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
79
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>();
83   //     }
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>();
87   //     }
88
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>();
92   //     }
93
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>();
97   //     }
98   //  else {
99   if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
100   UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
101   // }
102 }
103
104
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>
110 void
111 InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
112 {
113
114   // ImageTypes
115   typedef itk::Image<PixelType, Dimension> InputImageType;
116   typedef itk::Image<PixelType, Dimension> OutputImageType;
117
118   // Read the input
119   typedef itk::ImageFileReader<InputImageType> InputReaderType;
120   typename InputReaderType::Pointer reader = InputReaderType::New();
121   reader->SetFileName( m_InputFileName);
122   reader->Update();
123   typename InputImageType::Pointer input = reader->GetOutput();
124
125   // Filter
126   typename OutputImageType::Pointer output;
127   switch (m_ArgsInfo.type_arg) {
128
129     // clitk filter
130   case 0: {
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);
141       }
142
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);
150       resampler->Update();
151       spacing = resampler->GetOutput()->GetSpacing();
152       size = resampler->GetOutput()->GetLargestPossibleRegion().GetSize();
153       filter->SetInput(resampler->GetOutput());
154     }
155     else
156       filter->SetInput(input);
157
158     filter->SetVerbose(m_Verbose);
159     if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
160     if (m_ArgsInfo.pad_given) {
161       PixelType pad;
162       if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
163         pad.Fill(m_ArgsInfo.pad_arg[0]);
164       else
165         for(unsigned int i=0; i<Dimension; i++)
166           pad[i]=m_ArgsInfo.pad_arg[i];
167     }
168     filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
169     filter->Update();
170     output=filter->GetOutput();
171
172     break;
173   }
174
175   case 1: {
176     // Create the InvertVFFilter
177     typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
178     typename FilterType::Pointer filter =FilterType::New();
179     if (m_ArgsInfo.like_given) {
180       typedef ConvertBLUTCoeffsToVFFilter<InputImageType> VFFilterType;
181       typename VFFilterType::Pointer vf_filter = VFFilterType::New();
182       vf_filter->SetInputFileName(m_InputFileName);
183       vf_filter->SetLikeFileName(m_ArgsInfo.like_arg);
184       vf_filter->SetVerbose(m_Verbose);
185       vf_filter->Update();
186       filter->SetInput(vf_filter->GetOutput());
187     }
188
189     filter->SetVerbose(m_Verbose);
190     if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
191     if (m_ArgsInfo.pad_given) {
192       PixelType pad;
193       if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
194         pad.Fill(m_ArgsInfo.pad_arg[0]);
195       else
196         for(unsigned int i=0; i<Dimension; i++)
197           pad[i]=m_ArgsInfo.pad_arg[i];
198     }
199     filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
200     filter->Update();
201     output=filter->GetOutput();
202
203     break;
204   }
205
206   case 2: {
207     // Create the InverseDeformationFieldFilter
208 #if ITK_VERSION_MAJOR >= 4
209     typedef itk::InverseDisplacementFieldImageFilter<InputImageType,OutputImageType> FilterType;
210 #else
211     typedef itk::InverseDeformationFieldImageFilter<InputImageType,OutputImageType> FilterType;
212 #endif
213     typename FilterType::Pointer filter =FilterType::New();
214     filter->SetInput(input);
215     filter->SetOutputOrigin(input->GetOrigin());
216     filter->SetOutputSpacing(input->GetSpacing());
217     filter->SetSize(input->GetLargestPossibleRegion().GetSize());
218     filter->SetSubsamplingFactor(m_ArgsInfo.sampling_arg);
219     filter->Update();
220     output=filter->GetOutput();
221
222     break;
223   }
224
225     
226   }
227
228   // Output
229   typedef itk::ImageFileWriter<OutputImageType> WriterType;
230   typename WriterType::Pointer writer = WriterType::New();
231   writer->SetFileName(m_ArgsInfo.output_arg);
232   writer->SetInput(output);
233   writer->Update();
234
235 }
236
237
238 }//end clitk
239
240 #endif //#define clitkInvertVFGenericFilter_txx