]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkResampleImageFilter.h
*** empty log message ***
[bbtk.git] / packages / itk / src / bbitkResampleImageFilter.h
1 #ifdef _USE_ITK_
2
3 #include <math.h>
4 #include "bbtkUserBlackBox.h"
5 #include "itkResampleImageFilter.h"
6 #include "bbitkImage.h"
7 #include "itkNearestNeighborInterpolateImageFunction.h"
8 #include "itkLinearInterpolateImageFunction.h"
9 #include "itkBSplineInterpolateImageFunction.h"
10
11 namespace bbitk
12 {
13
14  
15   class /*BBTK_EXPORT*/ ResampleImageFilter
16     : 
17     public bbtk::UserBlackBox
18   {
19     BBTK_USER_BLACK_BOX_INTERFACE(ResampleImageFilter,
20                                   bbtk::UserBlackBox);
21     BBTK_DECLARE_INPUT(In,anyImagePointer);
22     BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
23     BBTK_DECLARE_INPUT(Interpolation,std::string);
24     BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
25     BBTK_PROCESS(ProcessSwitch);
26   private :
27     inline void ProcessSwitch();
28     template <class T> void Process();
29     void bbUserConstructor() { Init(); }
30     void bbUserCopyConstructor() { Init(); }
31     void Init();
32     itk::Object* mOutput;
33   };
34   
35   BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
36                                 bbtk::UserBlackBox);
37   BBTK_NAME("ResampleImageFilter");
38   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
39   BBTK_DESCRIPTION("Resamples an image");
40   BBTK_INPUT(ResampleImageFilter,In,
41              "Input image. Can be any itk::Image<T,D>*",anyImagePointer);
42   BBTK_INPUT(ResampleImageFilter,Spacing,
43              "Spacing",std::vector<double>);
44   BBTK_INPUT(ResampleImageFilter,Interpolation,
45              "Interpolation",std::string);
46   BBTK_OUTPUT(ResampleImageFilter,Out,
47               "Output image. Of the same type and dimension than the input image",
48               anyImagePointer);
49   BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
50
51
52
53   void ResampleImageFilter::ProcessSwitch()
54   {
55     bbtk::TypeInfo t = bbGetInputIn().type();
56     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
57   }
58
59   template <class T> 
60   void ResampleImageFilter::Process()
61   {
62     bbtkDebugMessageInc("Core",9,
63                         "bbitk::ResampleImageFilter::Process<"
64                         <<bbtk::TypeName<T>()<<">()"<<std::endl);
65  
66     typedef T ImageType;
67     typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
68     typename FilterType::Pointer filter = FilterType::New();
69     const unsigned int Dimension = ImageType::ImageDimension;
70
71     // Input
72     T* in = this->bbGetInputIn().get<T*>();
73     filter->SetInput( in );
74
75     // Size, Spacing, Origin and DefaultPixelVal
76     typename ImageType::SizeType size;
77     typename ImageType::SpacingType spacing;
78     typename ImageType::PointType origin;
79     typename ImageType::RegionType LPR;
80     LPR = in->GetLargestPossibleRegion();
81     size = LPR.GetSize();
82     //    origin = LPR.GetIndex(); //in->GetOrigin();
83      for (int i=0;i<Dimension;++i) 
84       {
85         origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
86         spacing[i] = bbGetInputSpacing()[i];
87         double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
88         size[i] = (long)floor(tmp);
89 //      size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
90        }
91    
92     filter->SetOutputOrigin (origin);
93     filter->SetSize (size);
94     filter->SetOutputSpacing( spacing );
95
96     filter->SetDefaultPixelValue (0);
97     filter->SetOutputDirection( in->GetDirection() );
98
99
100
101     // Transform
102     typedef itk::AffineTransform < double, Dimension> TransformType;
103     
104     // Instance of the transform object to be passed to the resample filter
105     // By default, identity transform is applied
106     typename TransformType::Pointer transform =  TransformType::New();
107     filter->SetTransform ( transform );
108
109     
110     // Interpolation 
111     if  ( bbGetInputInterpolation() == "Linear" ) {
112       typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;     
113       // We create an interpolator of the found type 
114       typename InterpolatorType::Pointer interpolator =  InterpolatorType::New();
115       filter->SetInterpolator( interpolator );
116     }
117     else if  ( bbGetInputInterpolation() == "NearestNeighbor" ) {
118       typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;     
119       // We create an interpolator of the found type 
120       typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
121       filter->SetInterpolator( interpolator );
122     }
123     else if  ( bbGetInputInterpolation() == "BSpline") { 
124       typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType; 
125       // We create an interpolator of the found type 
126       typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
127       filter->SetInterpolator(interpolator);    
128       // When handling unsigned data, it is possible that the interpolated value is negative
129       // if ( (m_InputImage->GetComponentTypeAsString() == "uchar")   || 
130       //           (m_InputImage->GetComponentTypeAsString() == "ushort")  ||
131       //           (m_InputImage->GetComponentTypeAsString() == "uint") ) {   
132       //        std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
133       // }
134     } //end else if
135  
136
137     filter->Update();
138     filter->GetOutput()->Register();
139     if (mOutput) mOutput->UnRegister();
140     this->bbSetOutputOut( filter->GetOutput() );
141     mOutput = filter->GetOutput();
142
143     bbtkDebugDecTab("Core",9);
144   }
145   
146
147   void ResampleImageFilter::Init()
148   {
149     std::vector<double> res;
150     res.push_back(1);
151     res.push_back(1);
152     res.push_back(1);
153     bbSetInputSpacing(res);
154     mOutput = 0;
155   }
156
157 }
158 // EO namespace bbtk
159
160 #endif