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