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