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") {
49 mApplyGaussianFilterBefore = false;
50 mDefaultPixelValue = 0.0;
51 mInterpolatorName = "NN";
53 InitializeImageTypeWithDim<2>();
54 InitializeImageTypeWithDim<3>();
55 InitializeImageTypeWithDim<4>();
57 //--------------------------------------------------------------------
60 //--------------------------------------------------------------------
61 template<unsigned int Dim>
62 void clitk::ImageResampleGenericFilter::InitializeImageTypeWithDim() {
63 ADD_DEFAULT_IMAGE_TYPES(Dim);
65 //--------------------------------------------------------------------
68 //--------------------------------------------------------------------
69 template<class InputImageType>
70 void clitk::ImageResampleGenericFilter::UpdateWithInputImageType() {
73 typedef typename InputImageType::SizeType SizeType;
74 typedef typename InputImageType::SpacingType SpacingType;
75 typedef typename InputImageType::PointType PointType;
76 typedef typename InputImageType::PixelType PixelType;
77 static unsigned int dim = InputImageType::ImageDimension;
80 typename InputImageType::Pointer input = this->GetInput<InputImageType>(0);
83 if (!std::numeric_limits<PixelType>::is_signed) {
84 if ((mInterpolatorName == "bspline") || (mInterpolatorName == "blut")) {
85 std::cerr << "Warning : input pixel type is not signed, use bspline interpolation at your own risk ..." << std::endl;
90 if (mOutputSize.size() != dim) {
91 std::cerr << "Please set size with " << dim << " dimensions." << std::endl;
94 if (mOutputSpacing.size() != dim) {
95 std::cerr << "Please set spacing with " << dim << " dimensions." << std::endl;
98 mOutputOrigin.resize(dim);
100 if (mApplyGaussianFilterBefore && mSigma.size() != dim) {
101 std::cerr << "Please set sigma with " << dim << " dimensions." << std::endl;
105 // Create Image Filter
106 typedef itk::ResampleImageFilter<InputImageType,InputImageType> FilterType;
107 typename FilterType::Pointer filter = FilterType::New();
109 // Instance of the transform object to be passed to the resample
110 // filter. By default, identity transform is applied
111 typedef itk::AffineTransform<double, InputImageType::ImageDimension> TransformType;
112 typename TransformType::Pointer transform = TransformType::New();
113 filter->SetTransform(transform);
115 // Set filter's parameters
117 SpacingType outputSpacing;
118 PointType outputOrigin;
119 for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
120 outputSize[i] = mOutputSize[i];
121 outputSpacing[i] = mOutputSpacing[i];
122 outputOrigin[i] = input->GetOrigin()[i];
125 filter->SetSize(outputSize);
126 filter->SetOutputSpacing(outputSpacing);
127 filter->SetOutputOrigin(outputOrigin);
128 filter->SetDefaultPixelValue(static_cast<PixelType>(mDefaultPixelValue));//DS TODO//JV comme ça?
130 // Select interpolator
131 if (mInterpolatorName == "nn") {
132 typedef itk::NearestNeighborInterpolateImageFunction<InputImageType, double> InterpolatorType;
133 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
134 filter->SetInterpolator(interpolator);
137 if (mInterpolatorName == "linear") {
138 typedef itk::LinearInterpolateImageFunction<InputImageType, double> InterpolatorType;
139 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
140 filter->SetInterpolator(interpolator);
143 if (mInterpolatorName == "bspline") {
144 typedef itk::BSplineInterpolateImageFunction<InputImageType, double> InterpolatorType;
145 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
146 interpolator->SetSplineOrder(mBSplineOrder);
147 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);
158 std::cerr << "Sorry, I do not know the interpolator '" << mInterpolatorName
159 << "'. Known interpolators are : nn, linear, bspline, blut" << std::endl;
166 // Build initial Gaussian bluring (if needed)
167 typedef itk::RecursiveGaussianImageFilter<InputImageType, InputImageType> GaussianFilterType;
168 std::vector<typename GaussianFilterType::Pointer> gaussianFilters;
169 if (mApplyGaussianFilterBefore) {
170 for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
172 gaussianFilters.push_back(GaussianFilterType::New());
174 gaussianFilters[i]->SetDirection(i);
175 gaussianFilters[i]->SetOrder(GaussianFilterType::ZeroOrder);
176 gaussianFilters[i]->SetNormalizeAcrossScale(false);
177 gaussianFilters[i]->SetSigma(mSigma[i]); // in millimeter !
179 if (i==0) gaussianFilters[i]->SetInput(input);
180 else gaussianFilters[i]->SetInput(gaussianFilters[i-1]->GetOutput());
182 filter->SetInput(gaussianFilters[InputImageType::ImageDimension-1]->GetOutput());
185 filter->SetInput(input);
192 catch( itk::ExceptionObject & err ) {
193 std::cerr << "Error while filtering " << mInputFilenames[0].c_str()
194 << " " << err << std::endl;
199 typename InputImageType::Pointer outputImage = filter->GetOutput();
201 // Write/save results
202 this->SetNextOutput<InputImageType>(outputImage);
205 //--------------------------------------------------------------------
208 //--------------------------------------------------------------------
209 void clitk::ImageResampleGenericFilter::SetOutputSize(const std::vector<int> & size) {
210 mOutputSize.resize(size.size());
211 std::copy(size.begin(), size.end(), mOutputSize.begin());
213 //--------------------------------------------------------------------
215 //--------------------------------------------------------------------
216 void clitk::ImageResampleGenericFilter::SetOutputSpacing(const std::vector<double> & spacing) {
217 mOutputSpacing.resize(spacing.size());
218 std::copy(spacing.begin(), spacing.end(), mOutputSpacing.begin());
220 //--------------------------------------------------------------------
222 //--------------------------------------------------------------------
223 void clitk::ImageResampleGenericFilter::SetInterpolationName(const std::string & inter) {
224 mInterpolatorName = inter;
226 //--------------------------------------------------------------------
228 //--------------------------------------------------------------------
229 void clitk::ImageResampleGenericFilter::SetGaussianSigma(const std::vector<double> & sigma) {
230 mApplyGaussianFilterBefore = true;
231 mSigma.resize(sigma.size());
232 std::copy(sigma.begin(), sigma.end(), mSigma.begin());
234 //--------------------------------------------------------------------