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 #if ( ITK_VERSION_MAJOR < 5 )
22 #include "itkVectorResampleImageFilter.h"
24 #include "itkResampleImageFilter.h"
26 #include "clitkConvertBLUTCoeffsToVFFilter.h"
28 /* =================================================
29 * @file clitkInvertVFGenericFilter.txx
35 ===================================================*/
41 //-----------------------------------------------------------
43 //-----------------------------------------------------------
44 template<class args_info_type>
45 InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
52 //-----------------------------------------------------------
54 //-----------------------------------------------------------
55 template<class args_info_type>
56 void InvertVFGenericFilter<args_info_type>::Update()
58 // Read the Dimension and PixelType
60 std::string PixelType;
61 ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
65 if(Dimension==2) UpdateWithDim<2>(PixelType);
66 else if(Dimension==3) UpdateWithDim<3>(PixelType);
67 // else if (Dimension==4)UpdateWithDim<4>(PixelType);
69 std::cout<<"Error, Only for 2 or 3 Dimensions!!!"<<std::endl ;
74 //-------------------------------------------------------------------
75 // Update with the number of dimensions
76 //-------------------------------------------------------------------
77 template<class args_info_type>
78 template<unsigned int Dimension>
80 InvertVFGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
82 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
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>();
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>();
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>();
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>();
103 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
104 UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
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>
115 InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
119 typedef itk::Image<PixelType, Dimension> InputImageType;
120 typedef itk::Image<PixelType, Dimension> OutputImageType;
123 typedef itk::ImageFileReader<InputImageType> InputReaderType;
124 typename InputReaderType::Pointer reader = InputReaderType::New();
125 reader->SetFileName( m_InputFileName);
127 typename InputImageType::Pointer input = reader->GetOutput();
130 typename OutputImageType::Pointer output;
131 switch (m_ArgsInfo.type_arg) {
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);
147 #if ( ITK_VERSION_MAJOR < 5 )
148 typedef itk::VectorResampleImageFilter<InputImageType, OutputImageType> ResampleFilterType;
150 typedef itk::ResampleImageFilter<InputImageType, OutputImageType> ResampleFilterType;
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);
159 spacing = resampler->GetOutput()->GetSpacing();
160 size = resampler->GetOutput()->GetLargestPossibleRegion().GetSize();
161 filter->SetInput(resampler->GetOutput());
164 filter->SetInput(input);
166 filter->SetVerbose(m_Verbose);
167 if (m_ArgsInfo.threads_given) {
168 #if ITK_VERSION_MAJOR <= 4
169 filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
171 filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
174 if (m_ArgsInfo.pad_given) {
176 if (m_ArgsInfo.pad_given != (pad.GetNumberOfComponents()) )
177 pad.Fill(m_ArgsInfo.pad_arg[0]);
179 for(unsigned int i=0; i<Dimension; i++)
180 pad[i]=m_ArgsInfo.pad_arg[i];
182 filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
184 output=filter->GetOutput();
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);
200 filter->SetInput(vf_filter->GetOutput());
203 filter->SetVerbose(m_Verbose);
204 if (m_ArgsInfo.threads_given) {
205 #if ITK_VERSION_MAJOR <= 4
206 filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
208 filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
211 if (m_ArgsInfo.pad_given) {
213 if (m_ArgsInfo.pad_given != (pad.GetNumberOfComponents()) )
214 pad.Fill(m_ArgsInfo.pad_arg[0]);
216 for(unsigned int i=0; i<Dimension; i++)
217 pad[i]=m_ArgsInfo.pad_arg[i];
219 filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
221 output=filter->GetOutput();
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);
236 output=filter->GetOutput();
245 typedef itk::ImageFileWriter<OutputImageType> WriterType;
246 typename WriterType::Pointer writer = WriterType::New();
247 writer->SetFileName(m_ArgsInfo.output_arg);
248 writer->SetInput(output);
256 #endif //#define clitkInvertVFGenericFilter_txx