]> Creatis software - clitk.git/blob - filters/clitkImageResampleGenericFilter.cxx
Reformatted using new coding style
[clitk.git] / filters / clitkImageResampleGenericFilter.cxx
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://oncora1.lyon.fnclcc.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 CLITKIMAGERESAMPLEGENERICFILTER2_CXX
19 #define CLITKIMAGERESAMPLEGENERICFILTER2_CXX
20 /**
21  -------------------------------------------------------------------
22  * @file   clitkImageResampleGenericFilter.cxx
23  * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24  * @date   23 Feb 2008 08:37:53
25
26  * @brief
27
28  -------------------------------------------------------------------*/
29
30 #include "clitkImageResampleGenericFilter.h"
31
32 // itk include
33 #include "itkImage.h"
34 #include "itkImageFileReader.h"
35 #include "itkImageSeriesReader.h"
36 #include "itkImageFileWriter.h"
37 #include "itkRecursiveGaussianImageFilter.h"
38 #include "itkResampleImageFilter.h"
39 #include "itkAffineTransform.h"
40 #include "itkNearestNeighborInterpolateImageFunction.h"
41 #include "itkLinearInterpolateImageFunction.h"
42 #include "itkBSplineInterpolateImageFunction.h"
43 #include "itkBSplineInterpolateImageFunctionWithLUT.h"
44 #include "itkCommand.h"
45
46 //--------------------------------------------------------------------
47 clitk::ImageResampleGenericFilter::ImageResampleGenericFilter():
48   ImageToImageGenericFilter<Self>("ImageResample")
49 {
50   mApplyGaussianFilterBefore = false;
51   mDefaultPixelValue = 0.0;
52   mInterpolatorName = "NN";
53   mBSplineOrder=3;
54   InitializeImageTypeWithDim<2>();
55   InitializeImageTypeWithDim<3>();
56   InitializeImageTypeWithDim<4>();
57 }
58 //--------------------------------------------------------------------
59
60
61 //--------------------------------------------------------------------
62 template<unsigned int Dim>
63 void clitk::ImageResampleGenericFilter::InitializeImageTypeWithDim()
64 {
65   ADD_DEFAULT_IMAGE_TYPES(Dim);
66 }
67 //--------------------------------------------------------------------
68
69
70 //--------------------------------------------------------------------
71 template<class InputImageType>
72 void clitk::ImageResampleGenericFilter::UpdateWithInputImageType()
73 {
74
75   // Some typedefs
76   typedef typename InputImageType::SizeType    SizeType;
77   typedef typename InputImageType::SpacingType SpacingType;
78   typedef typename InputImageType::PointType   PointType;
79   typedef typename InputImageType::PixelType   PixelType;
80   static unsigned int dim = InputImageType::ImageDimension;
81
82   // Reading input
83   typename InputImageType::Pointer input = this->GetInput<InputImageType>(0);
84
85   // Warning
86   if (!std::numeric_limits<PixelType>::is_signed) {
87     if ((mInterpolatorName == "bspline") || (mInterpolatorName == "blut")) {
88       std::cerr << "Warning : input pixel type is not signed, use bspline interpolation at your own risk ..." << std::endl;
89     }
90   }
91
92   // Check options
93   if (mOutputSize.size() != dim) {
94     std::cerr << "Please set size with " << dim << " dimensions." << std::endl;
95     return;
96   }
97   if (mOutputSpacing.size() != dim) {
98     std::cerr << "Please set spacing with " << dim << " dimensions." << std::endl;
99     return;
100   }
101   mOutputOrigin.resize(dim);
102
103   if (mApplyGaussianFilterBefore && mSigma.size() != dim) {
104     std::cerr << "Please set sigma with " << dim << " dimensions." << std::endl;
105     return;
106   }
107
108   // Create Image Filter
109   typedef itk::ResampleImageFilter<InputImageType,InputImageType> FilterType;
110   typename FilterType::Pointer filter = FilterType::New();
111
112   // Instance of the transform object to be passed to the resample
113   // filter. By default, identity transform is applied
114   typedef itk::AffineTransform<double, InputImageType::ImageDimension> TransformType;
115   typename TransformType::Pointer transform =  TransformType::New();
116   filter->SetTransform(transform);
117
118   // Set filter's parameters
119   SizeType outputSize;
120   SpacingType outputSpacing;
121   PointType outputOrigin;
122   for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
123     outputSize[i] = mOutputSize[i];
124     outputSpacing[i] = mOutputSpacing[i];
125     outputOrigin[i] = input->GetOrigin()[i];
126   }
127
128   filter->SetSize(outputSize);
129   filter->SetOutputSpacing(outputSpacing);
130   filter->SetOutputOrigin(outputOrigin);
131   filter->SetDefaultPixelValue(static_cast<PixelType>(mDefaultPixelValue));//DS TODO//JV comme ça?
132
133   // Select interpolator
134   if (mInterpolatorName == "nn") {
135     typedef itk::NearestNeighborInterpolateImageFunction<InputImageType, double> InterpolatorType;
136     typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
137     filter->SetInterpolator(interpolator);
138   } else {
139     if (mInterpolatorName == "linear") {
140       typedef itk::LinearInterpolateImageFunction<InputImageType, double> InterpolatorType;
141       typename InterpolatorType::Pointer interpolator =  InterpolatorType::New();
142       filter->SetInterpolator(interpolator);
143     } else {
144       if (mInterpolatorName == "bspline") {
145         typedef itk::BSplineInterpolateImageFunction<InputImageType, double> InterpolatorType;
146         typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
147         interpolator->SetSplineOrder(mBSplineOrder);
148         filter->SetInterpolator(interpolator);
149       } else {
150         if (mInterpolatorName == "blut") {
151           typedef itk::BSplineInterpolateImageFunctionWithLUT<InputImageType, double> InterpolatorType;
152           typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
153           interpolator->SetSplineOrder(mBSplineOrder);
154           interpolator->SetLUTSamplingFactor(mSamplingFactors[0]);
155           filter->SetInterpolator(interpolator);
156         } else {
157           std::cerr << "Sorry, I do not know the interpolator '" << mInterpolatorName
158                     << "'. Known interpolators are :  nn, linear, bspline, blut" << std::endl;
159           exit(0);
160         }
161       }
162     }
163   }
164
165   // Build initial Gaussian bluring (if needed)
166   typedef itk::RecursiveGaussianImageFilter<InputImageType, InputImageType> GaussianFilterType;
167   std::vector<typename GaussianFilterType::Pointer> gaussianFilters;
168   if (mApplyGaussianFilterBefore) {
169     for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
170       // Create filter
171       gaussianFilters.push_back(GaussianFilterType::New());
172       // Set options
173       gaussianFilters[i]->SetDirection(i);
174       gaussianFilters[i]->SetOrder(GaussianFilterType::ZeroOrder);
175       gaussianFilters[i]->SetNormalizeAcrossScale(false);
176       gaussianFilters[i]->SetSigma(mSigma[i]); // in millimeter !
177       // Set input
178       if (i==0) gaussianFilters[i]->SetInput(input);
179       else gaussianFilters[i]->SetInput(gaussianFilters[i-1]->GetOutput());
180     }
181     filter->SetInput(gaussianFilters[InputImageType::ImageDimension-1]->GetOutput());
182   } else {
183     filter->SetInput(input);
184   }
185
186   // Go !
187   try {
188     filter->Update();
189   } catch( itk::ExceptionObject & err ) {
190     std::cerr << "Error while filtering " << mInputFilenames[0].c_str()
191               << " " << err << std::endl;
192     exit(0);
193   }
194
195   // Get result
196   typename InputImageType::Pointer outputImage = filter->GetOutput();
197
198   // Write/save results
199   this->SetNextOutput<InputImageType>(outputImage);
200
201 }
202 //--------------------------------------------------------------------
203
204
205 //--------------------------------------------------------------------
206 void clitk::ImageResampleGenericFilter::SetOutputSize(const std::vector<int> & size)
207 {
208   mOutputSize.resize(size.size());
209   std::copy(size.begin(), size.end(), mOutputSize.begin());
210 }
211 //--------------------------------------------------------------------
212
213 //--------------------------------------------------------------------
214 void clitk::ImageResampleGenericFilter::SetOutputSpacing(const std::vector<double> & spacing)
215 {
216   mOutputSpacing.resize(spacing.size());
217   std::copy(spacing.begin(), spacing.end(), mOutputSpacing.begin());
218 }
219 //--------------------------------------------------------------------
220
221 //--------------------------------------------------------------------
222 void clitk::ImageResampleGenericFilter::SetInterpolationName(const std::string & inter)
223 {
224   mInterpolatorName = inter;
225 }
226 //--------------------------------------------------------------------
227
228 //--------------------------------------------------------------------
229 void clitk::ImageResampleGenericFilter::SetGaussianSigma(const std::vector<double> & sigma)
230 {
231   mApplyGaussianFilterBefore = true;
232   mSigma.resize(sigma.size());
233   std::copy(sigma.begin(), sigma.end(), mSigma.begin());
234 }
235 //--------------------------------------------------------------------
236
237 #endif
238