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 CLITKVFRESAMPLEGENERICFILTER_CXX
19 #define CLITKVFRESAMPLEGENERICFILTER_CXX
21 -------------------------------------------------------------------
22 * @file clitkVFResampleGenericFilter.cxx
23 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24 * @date 23 Feb 2008 08:37:53
28 -------------------------------------------------------------------*/
30 #include "clitkVFResampleGenericFilter.h"
32 //--------------------------------------------------------------------
33 clitk::VFResampleGenericFilter::VFResampleGenericFilter():
34 clitk::ImageToImageGenericFilter<Self>("VFResample")
36 InitializeImageType<2>();
37 InitializeImageType<3>();
38 InitializeImageType<4>();
39 mApplyGaussianFilterBefore = false;
40 mDefaultPixelValue = 0.0;
41 mInterpolatorName = "NN";
44 //--------------------------------------------------------------------
47 //--------------------------------------------------------------------
48 template<unsigned int Dim>
49 void clitk::VFResampleGenericFilter::InitializeImageType()
51 ADD_IMAGE_TYPE(Dim, float);
53 //--------------------------------------------------------------------
56 //--------------------------------------------------------------------
57 template<class ImageType>
58 void clitk::VFResampleGenericFilter::UpdateWithInputImageType()
61 if (m_NbOfComponents == 1) {
62 std::cerr << "Error, only one components ? Use clitkImageResample instead." << std::endl;
65 typedef typename ImageType::PixelType PixelType;
66 if (m_NbOfComponents == 2) Update_WithDimAndPixelTypeAndComponent<ImageType::ImageDimension,PixelType,2>();
67 if (m_NbOfComponents == 3) Update_WithDimAndPixelTypeAndComponent<ImageType::ImageDimension,PixelType,3>();
68 if (m_NbOfComponents == 4) Update_WithDimAndPixelTypeAndComponent<ImageType::ImageDimension,PixelType,4>();
70 //--------------------------------------------------------------------
73 //--------------------------------------------------------------------
74 template<unsigned int Dim, class PixelType, unsigned int DimCompo>
75 void clitk::VFResampleGenericFilter::Update_WithDimAndPixelTypeAndComponent()
78 typedef itk::Vector<PixelType, DimCompo> DisplacementType;
79 typedef itk::Image< DisplacementType, Dim > ImageType;
81 typename ImageType::Pointer input = clitk::readImage<ImageType>(m_InputFilenames, m_IOVerbose);
84 typename ImageType::Pointer outputImage = ComputeImage<ImageType>(input);
87 SetNextOutput<ImageType>(outputImage);
89 //--------------------------------------------------------------------
91 //--------------------------------------------------------------------
92 template<class ImageType>
93 typename ImageType::Pointer
94 clitk::VFResampleGenericFilter::ComputeImage(typename ImageType::Pointer inputImage)
98 static unsigned int dim = ImageType::ImageDimension;
99 if (mOutputSize.size() != dim) {
100 std::cerr << "Please set size with " << dim << " dimensions." << std::endl;
103 if (mOutputSpacing.size() != dim) {
104 std::cerr << "Please set spacing with " << dim << " dimensions." << std::endl;
107 mOutputOrigin.resize(dim);
109 if (mApplyGaussianFilterBefore && mSigma.size() != dim) {
110 std::cerr << "Please set sigma with " << dim << " dimensions." << std::endl;
115 typedef typename ImageType::SizeType SizeType;
116 typedef typename ImageType::SpacingType SpacingType;
117 typedef typename ImageType::PointType PointType;
119 // Create Image Filter
120 typedef itk::VectorResampleImageFilter<ImageType,ImageType> FilterType;
121 typename FilterType::Pointer filter = FilterType::New();
123 // Instance of the transform object to be passed to the resample
124 // filter. By default, identity transform is applied
125 typedef itk::AffineTransform<double, ImageType::ImageDimension> TransformType;
126 typename TransformType::Pointer transform = TransformType::New();
127 filter->SetTransform(transform);
129 // Set filter's parameters
131 SpacingType outputSpacing;
132 PointType outputOrigin;
133 for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
134 outputSize[i] = mOutputSize[i];
135 outputSpacing[i] = mOutputSpacing[i];
136 outputOrigin[i] = inputImage->GetOrigin()[i];
139 filter->SetSize(outputSize);
140 filter->SetOutputSpacing(outputSpacing);
141 filter->SetOutputOrigin(outputOrigin);
142 filter->SetDefaultPixelValue(static_cast<typename ImageType::PixelType>(mDefaultPixelValue));
144 // Select interpolator
145 if (mInterpolatorName == "nn") {
146 typedef itk::VectorNearestNeighborInterpolateImageFunction<ImageType, double> InterpolatorType;
147 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
148 filter->SetInterpolator(interpolator);
150 if (mInterpolatorName == "linear") {
151 typedef itk::VectorLinearInterpolateImageFunction<ImageType, double> InterpolatorType;
152 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
153 filter->SetInterpolator(interpolator);
155 std::cerr << "Sorry, I do not know the interpolator (for vector field) '" << mInterpolatorName
156 << "'. Known interpolators are : nn, linear" << std::endl;
161 // Build initial Gaussian bluring (if needed)
162 typedef itk::RecursiveGaussianImageFilter<ImageType, ImageType> GaussianFilterType;
163 std::vector<typename GaussianFilterType::Pointer> gaussianFilters;
164 if (mApplyGaussianFilterBefore) {
165 for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
167 gaussianFilters.push_back(GaussianFilterType::New());
169 gaussianFilters[i]->SetDirection(i);
170 gaussianFilters[i]->SetOrder(GaussianFilterType::ZeroOrder);
171 gaussianFilters[i]->SetNormalizeAcrossScale(false);
172 gaussianFilters[i]->SetSigma(mSigma[i]); // in millimeter !
174 if (i==0) gaussianFilters[i]->SetInput(inputImage);
175 else gaussianFilters[i]->SetInput(gaussianFilters[i-1]->GetOutput());
177 filter->SetInput(gaussianFilters[ImageType::ImageDimension-1]->GetOutput());
179 filter->SetInput(inputImage);
185 } catch( itk::ExceptionObject & err ) {
186 std::cerr << "Error while filtering " << m_InputFilenames[0].c_str()
187 << " " << err << std::endl;
192 return filter->GetOutput();
195 //--------------------------------------------------------------------
198 //--------------------------------------------------------------------
199 void clitk::VFResampleGenericFilter::SetOutputSize(const std::vector<int> & size)
201 mOutputSize.resize(size.size());
202 std::copy(size.begin(), size.end(), mOutputSize.begin());
204 //--------------------------------------------------------------------
206 //--------------------------------------------------------------------
207 void clitk::VFResampleGenericFilter::SetOutputSpacing(const std::vector<double> & spacing)
209 mOutputSpacing.resize(spacing.size());
210 std::copy(spacing.begin(), spacing.end(), mOutputSpacing.begin());
212 //--------------------------------------------------------------------
214 //--------------------------------------------------------------------
215 void clitk::VFResampleGenericFilter::SetInterpolationName(const std::string & inter)
217 mInterpolatorName = inter;
219 //--------------------------------------------------------------------
221 //--------------------------------------------------------------------
222 void clitk::VFResampleGenericFilter::SetGaussianSigma(const std::vector<double> & sigma)
224 mApplyGaussianFilterBefore = true;
225 mSigma.resize(sigma.size());
226 std::copy(sigma.begin(), sigma.end(), mSigma.begin());
228 //--------------------------------------------------------------------