]> Creatis software - clitk.git/blob - tools/clitkInvertVFGenericFilter.txx
With ITKv5, change VectorResample and VectorCast Image Filter to Resample and Cast...
[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 #if ( ITK_VERSION_MAJOR < 5 )
22 #include "itkVectorResampleImageFilter.h"
23 #else
24 #include "itkResampleImageFilter.h"
25 #endif
26 #include "clitkConvertBLUTCoeffsToVFFilter.h"
27
28 /* =================================================
29  * @file   clitkInvertVFGenericFilter.txx
30  * @author
31  * @date
32  *
33  * @brief
34  *
35  ===================================================*/
36
37
38 namespace clitk
39 {
40
41 //-----------------------------------------------------------
42 // Constructor
43 //-----------------------------------------------------------
44 template<class args_info_type>
45 InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
46 {
47   m_Verbose=false;
48   m_InputFileName="";
49 }
50
51
52 //-----------------------------------------------------------
53 // Update
54 //-----------------------------------------------------------
55 template<class args_info_type>
56 void InvertVFGenericFilter<args_info_type>::Update()
57 {
58   // Read the Dimension and PixelType
59   int Dimension;
60   std::string PixelType;
61   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
62
63
64   // Call UpdateWithDim
65   if(Dimension==2) UpdateWithDim<2>(PixelType);
66   else if(Dimension==3) UpdateWithDim<3>(PixelType);
67   // else if (Dimension==4)UpdateWithDim<4>(PixelType);
68   else {
69     std::cout<<"Error, Only for 2 or 3  Dimensions!!!"<<std::endl ;
70     return;
71   }
72 }
73
74 //-------------------------------------------------------------------
75 // Update with the number of dimensions
76 //-------------------------------------------------------------------
77 template<class args_info_type>
78 template<unsigned int Dimension>
79 void
80 InvertVFGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
81 {
82   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
83
84   //    if(PixelType == "short"){
85   //       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
86   //       UpdateWithDimAndPixelType<Dimension, signed short>();
87   //     }
88   //    else if(PixelType == "unsigned_short"){
89   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
90   //       UpdateWithDimAndPixelType<Dimension, unsigned short>();
91   //     }
92
93   //     else if (PixelType == "unsigned_char"){
94   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
95   //       UpdateWithDimAndPixelType<Dimension, unsigned char>();
96   //     }
97
98   //     else if (PixelType == "char"){
99   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
100   //       UpdateWithDimAndPixelType<Dimension, signed char>();
101   //     }
102   //  else {
103   if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
104   UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
105   // }
106 }
107
108
109 //-------------------------------------------------------------------
110 // Update with the number of dimensions and the pixeltype
111 //-------------------------------------------------------------------
112 template<class args_info_type>
113 template <unsigned int Dimension, class  PixelType>
114 void
115 InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
116 {
117
118   // ImageTypes
119   typedef itk::Image<PixelType, Dimension> InputImageType;
120   typedef itk::Image<PixelType, Dimension> OutputImageType;
121
122   // Read the input
123   typedef itk::ImageFileReader<InputImageType> InputReaderType;
124   typename InputReaderType::Pointer reader = InputReaderType::New();
125   reader->SetFileName( m_InputFileName);
126   reader->Update();
127   typename InputImageType::Pointer input = reader->GetOutput();
128
129   // Filter
130   typename OutputImageType::Pointer output;
131   switch (m_ArgsInfo.type_arg) {
132
133     // clitk filter
134   case 0: {
135     // Create the InvertVFFilter
136     typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
137     typename FilterType::Pointer filter =FilterType::New();
138     if (m_ArgsInfo.like_given) {
139       typename FilterType::SpacingType spacing;
140       typename FilterType::SizeType size;
141       itk::ImageIOBase::Pointer header = readImageHeader(m_ArgsInfo.like_arg);
142       for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
143         size[i] = header->GetDimensions(i);
144         spacing[i] = header->GetSpacing(i);
145       }
146
147 #if ( ITK_VERSION_MAJOR < 5 )
148       typedef itk::VectorResampleImageFilter<InputImageType, OutputImageType> ResampleFilterType;
149 #else
150       typedef itk::ResampleImageFilter<InputImageType, OutputImageType> ResampleFilterType;
151 #endif
152       typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
153       resampler->SetInput(input);
154       resampler->SetOutputOrigin(input->GetOrigin());
155       resampler->SetOutputDirection(input->GetDirection());
156       resampler->SetOutputSpacing(spacing);
157       resampler->SetSize(size);
158       resampler->Update();
159       spacing = resampler->GetOutput()->GetSpacing();
160       size = resampler->GetOutput()->GetLargestPossibleRegion().GetSize();
161       filter->SetInput(resampler->GetOutput());
162     }
163     else
164       filter->SetInput(input);
165
166     filter->SetVerbose(m_Verbose);
167     if (m_ArgsInfo.threads_given) {
168 #if ITK_VERSION_MAJOR <= 4
169       filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
170 #else
171       filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
172 #endif
173     }
174     if (m_ArgsInfo.pad_given) {
175       PixelType pad;
176       if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
177         pad.Fill(m_ArgsInfo.pad_arg[0]);
178       else
179         for(unsigned int i=0; i<Dimension; i++)
180           pad[i]=m_ArgsInfo.pad_arg[i];
181     }
182     filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
183     filter->Update();
184     output=filter->GetOutput();
185
186     break;
187   }
188
189   case 1: {
190     // Create the InvertVFFilter
191     typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
192     typename FilterType::Pointer filter =FilterType::New();
193     if (m_ArgsInfo.like_given) {
194       typedef ConvertBLUTCoeffsToVFFilter<InputImageType> VFFilterType;
195       typename VFFilterType::Pointer vf_filter = VFFilterType::New();
196       vf_filter->SetInputFileName(m_InputFileName);
197       vf_filter->SetLikeFileName(m_ArgsInfo.like_arg);
198       vf_filter->SetVerbose(m_Verbose);
199       vf_filter->Update();
200       filter->SetInput(vf_filter->GetOutput());
201     }
202
203     filter->SetVerbose(m_Verbose);
204     if (m_ArgsInfo.threads_given) {
205 #if ITK_VERSION_MAJOR <= 4
206       filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
207 #else
208       filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
209 #endif
210     }
211     if (m_ArgsInfo.pad_given) {
212       PixelType pad;
213       if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
214         pad.Fill(m_ArgsInfo.pad_arg[0]);
215       else
216         for(unsigned int i=0; i<Dimension; i++)
217           pad[i]=m_ArgsInfo.pad_arg[i];
218     }
219     filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
220     filter->Update();
221     output=filter->GetOutput();
222
223     break;
224   }
225
226   case 2: {
227     // Create the InverseDeformationFieldFilter
228     typedef itk::InverseDisplacementFieldImageFilter<InputImageType,OutputImageType> FilterType;
229     typename FilterType::Pointer filter =FilterType::New();
230     filter->SetInput(input);
231     filter->SetOutputOrigin(input->GetOrigin());
232     filter->SetOutputSpacing(input->GetSpacing());
233     filter->SetSize(input->GetLargestPossibleRegion().GetSize());
234     filter->SetSubsamplingFactor(m_ArgsInfo.sampling_arg);
235     filter->Update();
236     output=filter->GetOutput();
237
238     break;
239   }
240
241     
242   }
243
244   // Output
245   typedef itk::ImageFileWriter<OutputImageType> WriterType;
246   typename WriterType::Pointer writer = WriterType::New();
247   writer->SetFileName(m_ArgsInfo.output_arg);
248   writer->SetInput(output);
249   writer->Update();
250
251 }
252
253
254 }//end clitk
255
256 #endif //#define clitkInvertVFGenericFilter_txx