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://oncora1.lyon.fnclcc.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 CLITKIMAGERESAMPLEGENERICFILTER2_CXX
19 #define CLITKIMAGERESAMPLEGENERICFILTER2_CXX
21 -------------------------------------------------------------------
22 * @file clitkImageResampleGenericFilter.cxx
23 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24 * @date 23 Feb 2008 08:37:53
28 -------------------------------------------------------------------*/
30 #include "clitkImageResampleGenericFilter.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"
46 //--------------------------------------------------------------------
47 clitk::ImageResampleGenericFilter::ImageResampleGenericFilter():
48 ImageToImageGenericFilter<Self>("ImageResample")
50 mApplyGaussianFilterBefore = false;
51 mDefaultPixelValue = 0.0;
52 mInterpolatorName = "NN";
54 InitializeImageTypeWithDim<2>();
55 InitializeImageTypeWithDim<3>();
56 InitializeImageTypeWithDim<4>();
58 //--------------------------------------------------------------------
61 //--------------------------------------------------------------------
62 template<unsigned int Dim>
63 void clitk::ImageResampleGenericFilter::InitializeImageTypeWithDim()
65 ADD_DEFAULT_IMAGE_TYPES(Dim);
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 template<class InputImageType>
72 void clitk::ImageResampleGenericFilter::UpdateWithInputImageType()
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;
83 typename InputImageType::Pointer input = this->GetInput<InputImageType>(0);
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;
93 if (mOutputSize.size() != dim) {
94 std::cerr << "Please set size with " << dim << " dimensions." << std::endl;
97 if (mOutputSpacing.size() != dim) {
98 std::cerr << "Please set spacing with " << dim << " dimensions." << std::endl;
101 mOutputOrigin.resize(dim);
103 if (mApplyGaussianFilterBefore && mSigma.size() != dim) {
104 std::cerr << "Please set sigma with " << dim << " dimensions." << std::endl;
108 // Create Image Filter
109 typedef itk::ResampleImageFilter<InputImageType,InputImageType> FilterType;
110 typename FilterType::Pointer filter = FilterType::New();
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);
118 // Set filter's parameters
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];
128 filter->SetSize(outputSize);
129 filter->SetOutputSpacing(outputSpacing);
130 filter->SetOutputOrigin(outputOrigin);
131 filter->SetDefaultPixelValue(static_cast<PixelType>(mDefaultPixelValue));//DS TODO//JV comme ça?
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);
139 if (mInterpolatorName == "linear") {
140 typedef itk::LinearInterpolateImageFunction<InputImageType, double> InterpolatorType;
141 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
142 filter->SetInterpolator(interpolator);
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);
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);
157 std::cerr << "Sorry, I do not know the interpolator '" << mInterpolatorName
158 << "'. Known interpolators are : nn, linear, bspline, blut" << std::endl;
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++) {
171 gaussianFilters.push_back(GaussianFilterType::New());
173 gaussianFilters[i]->SetDirection(i);
174 gaussianFilters[i]->SetOrder(GaussianFilterType::ZeroOrder);
175 gaussianFilters[i]->SetNormalizeAcrossScale(false);
176 gaussianFilters[i]->SetSigma(mSigma[i]); // in millimeter !
178 if (i==0) gaussianFilters[i]->SetInput(input);
179 else gaussianFilters[i]->SetInput(gaussianFilters[i-1]->GetOutput());
181 filter->SetInput(gaussianFilters[InputImageType::ImageDimension-1]->GetOutput());
183 filter->SetInput(input);
189 } catch( itk::ExceptionObject & err ) {
190 std::cerr << "Error while filtering " << mInputFilenames[0].c_str()
191 << " " << err << std::endl;
196 typename InputImageType::Pointer outputImage = filter->GetOutput();
198 // Write/save results
199 this->SetNextOutput<InputImageType>(outputImage);
202 //--------------------------------------------------------------------
205 //--------------------------------------------------------------------
206 void clitk::ImageResampleGenericFilter::SetOutputSize(const std::vector<int> & size)
208 mOutputSize.resize(size.size());
209 std::copy(size.begin(), size.end(), mOutputSize.begin());
211 //--------------------------------------------------------------------
213 //--------------------------------------------------------------------
214 void clitk::ImageResampleGenericFilter::SetOutputSpacing(const std::vector<double> & spacing)
216 mOutputSpacing.resize(spacing.size());
217 std::copy(spacing.begin(), spacing.end(), mOutputSpacing.begin());
219 //--------------------------------------------------------------------
221 //--------------------------------------------------------------------
222 void clitk::ImageResampleGenericFilter::SetInterpolationName(const std::string & inter)
224 mInterpolatorName = inter;
226 //--------------------------------------------------------------------
228 //--------------------------------------------------------------------
229 void clitk::ImageResampleGenericFilter::SetGaussianSigma(const std::vector<double> & sigma)
231 mApplyGaussianFilterBefore = true;
232 mSigma.resize(sigma.size());
233 std::copy(sigma.begin(), sigma.end(), mSigma.begin());
235 //--------------------------------------------------------------------