2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbitkResampleImageFilter.h,v $
32 Date: $Date: 2012/11/16 08:50:39 $
33 Version: $Revision: 1.13 $
34 =========================================================================*/
42 #include "bbtkAtomicBlackBox.h"
43 #include "itkResampleImageFilter.h"
44 #include "bbitkImage.h"
45 #include "itkNearestNeighborInterpolateImageFunction.h"
46 #include "itkLinearInterpolateImageFunction.h"
47 #include "itkBSplineInterpolateImageFunction.h"
50 #include <itkAffineTransform.h>
56 //===================================================
57 class /*BBTK_EXPORT*/ ResampleImageFilter
59 public bbtk::AtomicBlackBox
61 BBTK_BLACK_BOX_INTERFACE(ResampleImageFilter, bbtk::AtomicBlackBox);
62 BBTK_DECLARE_INPUT(In,anyImagePointer);
63 BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
64 BBTK_DECLARE_INPUT(Interpolation,std::string);
65 BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
66 BBTK_PROCESS(ProcessSwitch);
68 inline void ProcessSwitch();
69 template <class T> void Process();
72 //===================================================
74 //===================================================
75 BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
76 bbtk::AtomicBlackBox);
77 BBTK_NAME("ResampleImageFilter");
78 BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
79 BBTK_DESCRIPTION("Resamples an image");
80 BBTK_CATEGORY("image;filter");
81 BBTK_INPUT(ResampleImageFilter,In,"Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
82 BBTK_INPUT(ResampleImageFilter,Spacing,"Spacing",std::vector<double>,"spacing");
83 BBTK_INPUT(ResampleImageFilter,Interpolation,"Interpolation: Linear (default) || BSpline || NearestNeighbor) ",std::string,"");
84 BBTK_OUTPUT(ResampleImageFilter,Out,"Output image. Of the same type and dimension than the input image",anyImagePointer,"");
85 BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
86 //===================================================
90 //===================================================
91 void ResampleImageFilter::ProcessSwitch()
93 bbtk::TypeInfo t = bbGetInputIn().type();
94 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
96 //===================================================
98 //===================================================
100 void ResampleImageFilter::Process()
102 bbtkDebugMessageInc("Core",9,
103 "bbitk::ResampleImageFilter::Process<"
104 <<bbtk::TypeName<T>()<<">()"<<std::endl);
107 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
108 typename FilterType::Pointer filter = FilterType::New();
109 const unsigned int Dimension = ImageType::ImageDimension;
112 T* in = this->bbGetInputIn().get<T*>();
113 filter->SetInput( in );
115 // Size, Spacing, Origin and DefaultPixelVal
116 typename ImageType::SizeType size;
117 typename ImageType::SpacingType spacing;
118 typename ImageType::PointType origin;
119 typename ImageType::RegionType LPR;
120 LPR = in->GetLargestPossibleRegion();
121 size = LPR.GetSize();
122 // origin = LPR.GetIndex(); //in->GetOrigin();
123 for (unsigned int i=0;i<Dimension;++i)
125 origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
126 spacing[i] = bbGetInputSpacing()[i];
127 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
128 size[i] = (long)floor(tmp);
129 // size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
132 filter->SetOutputOrigin (origin);
133 filter->SetSize (size);
134 filter->SetOutputSpacing( spacing );
136 filter->SetDefaultPixelValue (0);
137 filter->SetOutputDirection( in->GetDirection() );
142 typedef itk::AffineTransform < double, Dimension> TransformType;
144 // Instance of the transform object to be passed to the resample filter
145 // By default, identity transform is applied
146 typename TransformType::Pointer transform = TransformType::New();
147 filter->SetTransform ( transform );
151 if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
152 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
153 // We create an interpolator of the found type
154 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
155 filter->SetInterpolator( interpolator );
157 else if ( bbGetInputInterpolation() == "BSpline") {
158 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
159 // We create an interpolator of the found type
160 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
161 filter->SetInterpolator(interpolator);
162 // When handling unsigned data, it is possible that the interpolated value is negative
163 // if ( (m_InputImage->GetComponentTypeAsString() == "uchar") ||
164 // (m_InputImage->GetComponentTypeAsString() == "ushort") ||
165 // (m_InputImage->GetComponentTypeAsString() == "uint") ) {
166 // std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
170 else { // if ( bbGetInputInterpolation() == "Linear" ) {
171 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
172 // We create an interpolator of the found type
173 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
174 filter->SetInterpolator( interpolator );
178 filter->GetOutput()->Register();
179 if (mOutput) mOutput->UnRegister();
180 this->bbSetOutputOut( filter->GetOutput() );
181 mOutput = filter->GetOutput();
183 bbtkDebugDecTab("Core",9);
185 //===================================================
188 //-----------------------------------------------------------------
189 void ResampleImageFilter::bbUserSetDefaultValues()
191 std::vector<double> res;
195 bbSetInputSpacing(res);
199 //-----------------------------------------------------------------
200 void ResampleImageFilter::bbUserInitializeProcessing()
204 //-----------------------------------------------------------------
205 void ResampleImageFilter::bbUserFinalizeProcessing()
210 } // EO namespace bbitk