4 #include "bbtkAtomicBlackBox.h"
5 #include "itkResampleImageFilter.h"
6 #include "bbitkImage.h"
7 #include "itkNearestNeighborInterpolateImageFunction.h"
8 #include "itkLinearInterpolateImageFunction.h"
9 #include "itkBSplineInterpolateImageFunction.h"
15 class /*BBTK_EXPORT*/ ResampleImageFilter
17 public bbtk::AtomicBlackBox
19 BBTK_USER_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);
27 inline void ProcessSwitch();
28 template <class T> void Process();
29 void bbUserConstructor() { Init(); }
30 void bbUserCopyConstructor() { Init(); }
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>);
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",
50 BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
54 void ResampleImageFilter::ProcessSwitch()
56 bbtk::TypeInfo t = bbGetInputIn().type();
57 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
61 void ResampleImageFilter::Process()
63 bbtkDebugMessageInc("Core",9,
64 "bbitk::ResampleImageFilter::Process<"
65 <<bbtk::TypeName<T>()<<">()"<<std::endl);
68 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
69 typename FilterType::Pointer filter = FilterType::New();
70 const unsigned int Dimension = ImageType::ImageDimension;
73 T* in = this->bbGetInputIn().get<T*>();
74 filter->SetInput( in );
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();
83 // origin = LPR.GetIndex(); //in->GetOrigin();
84 for (int i=0;i<Dimension;++i)
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]);
93 filter->SetOutputOrigin (origin);
94 filter->SetSize (size);
95 filter->SetOutputSpacing( spacing );
97 filter->SetDefaultPixelValue (0);
98 filter->SetOutputDirection( in->GetDirection() );
103 typedef itk::AffineTransform < double, Dimension> TransformType;
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 );
112 if ( bbGetInputInterpolation() == "Linear" ) {
113 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
114 // We create an interpolator of the found type
115 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
116 filter->SetInterpolator( interpolator );
118 else if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
119 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
120 // We create an interpolator of the found type
121 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
122 filter->SetInterpolator( interpolator );
124 else if ( bbGetInputInterpolation() == "BSpline") {
125 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
126 // We create an interpolator of the found type
127 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
128 filter->SetInterpolator(interpolator);
129 // When handling unsigned data, it is possible that the interpolated value is negative
130 // if ( (m_InputImage->GetComponentTypeAsString() == "uchar") ||
131 // (m_InputImage->GetComponentTypeAsString() == "ushort") ||
132 // (m_InputImage->GetComponentTypeAsString() == "uint") ) {
133 // std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
139 filter->GetOutput()->Register();
140 if (mOutput) mOutput->UnRegister();
141 this->bbSetOutputOut( filter->GetOutput() );
142 mOutput = filter->GetOutput();
144 bbtkDebugDecTab("Core",9);
148 void ResampleImageFilter::Init()
150 std::vector<double> res;
154 bbSetInputSpacing(res);