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