00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef _USE_ITK_
00033
00034 #include <math.h>
00035 #include "bbtkAtomicBlackBox.h"
00036 #include "itkResampleImageFilter.h"
00037 #include "bbitkImage.h"
00038 #include "itkNearestNeighborInterpolateImageFunction.h"
00039 #include "itkLinearInterpolateImageFunction.h"
00040 #include "itkBSplineInterpolateImageFunction.h"
00041
00042 namespace bbitk
00043 {
00044
00045
00046 class ResampleImageFilter
00047 :
00048 public bbtk::AtomicBlackBox
00049 {
00050 BBTK_BLACK_BOX_INTERFACE(ResampleImageFilter,
00051 bbtk::AtomicBlackBox);
00052 BBTK_DECLARE_INPUT(In,anyImagePointer);
00053 BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
00054 BBTK_DECLARE_INPUT(Interpolation,std::string);
00055 BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
00056 BBTK_PROCESS(ProcessSwitch);
00057 private :
00058 inline void ProcessSwitch();
00059 template <class T> void Process();
00060 void bbUserConstructor() { Init(); }
00061 void bbUserCopyConstructor() { Init(); }
00062 void Init();
00063 itk::Object* mOutput;
00064 };
00065
00066 BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
00067 bbtk::AtomicBlackBox);
00068 BBTK_NAME("ResampleImageFilter");
00069 BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
00070 BBTK_DESCRIPTION("Resamples an image");
00071 BBTK_CATEGORY("image;filter");
00072 BBTK_INPUT(ResampleImageFilter,In,
00073 "Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
00074 BBTK_INPUT(ResampleImageFilter,Spacing,
00075 "Spacing",std::vector<double>,"spacing");
00076 BBTK_INPUT(ResampleImageFilter,Interpolation,
00077 "Interpolation",std::string,"");
00078 BBTK_OUTPUT(ResampleImageFilter,Out,
00079 "Output image. Of the same type and dimension than the input image",
00080 anyImagePointer,"");
00081 BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
00082
00083
00084
00085 void ResampleImageFilter::ProcessSwitch()
00086 {
00087 bbtk::TypeInfo t = bbGetInputIn().type();
00088 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
00089 }
00090
00091 template <class T>
00092 void ResampleImageFilter::Process()
00093 {
00094 bbtkDebugMessageInc("Core",9,
00095 "bbitk::ResampleImageFilter::Process<"
00096 <<bbtk::TypeName<T>()<<">()"<<std::endl);
00097
00098 typedef T ImageType;
00099 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
00100 typename FilterType::Pointer filter = FilterType::New();
00101 const unsigned int Dimension = ImageType::ImageDimension;
00102
00103
00104 T* in = this->bbGetInputIn().get<T*>();
00105 filter->SetInput( in );
00106
00107
00108 typename ImageType::SizeType size;
00109 typename ImageType::SpacingType spacing;
00110 typename ImageType::PointType origin;
00111 typename ImageType::RegionType LPR;
00112 LPR = in->GetLargestPossibleRegion();
00113 size = LPR.GetSize();
00114
00115 for (unsigned int i=0;i<Dimension;++i)
00116 {
00117 origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
00118 spacing[i] = bbGetInputSpacing()[i];
00119 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
00120 size[i] = (long)floor(tmp);
00121
00122 }
00123
00124 filter->SetOutputOrigin (origin);
00125 filter->SetSize (size);
00126 filter->SetOutputSpacing( spacing );
00127
00128 filter->SetDefaultPixelValue (0);
00129 filter->SetOutputDirection( in->GetDirection() );
00130
00131
00132
00133
00134 typedef itk::AffineTransform < double, Dimension> TransformType;
00135
00136
00137
00138 typename TransformType::Pointer transform = TransformType::New();
00139 filter->SetTransform ( transform );
00140
00141
00142
00143 if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
00144 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
00145
00146 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
00147 filter->SetInterpolator( interpolator );
00148 }
00149 else if ( bbGetInputInterpolation() == "BSpline") {
00150 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
00151
00152 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
00153 filter->SetInterpolator(interpolator);
00154
00155
00156
00157
00158
00159
00160 }
00161
00162 else {
00163 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
00164
00165 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
00166 filter->SetInterpolator( interpolator );
00167 }
00168
00169 filter->Update();
00170 filter->GetOutput()->Register();
00171 if (mOutput) mOutput->UnRegister();
00172 this->bbSetOutputOut( filter->GetOutput() );
00173 mOutput = filter->GetOutput();
00174
00175 bbtkDebugDecTab("Core",9);
00176 }
00177
00178
00179 void ResampleImageFilter::Init()
00180 {
00181 std::vector<double> res;
00182 res.push_back(1);
00183 res.push_back(1);
00184 res.push_back(1);
00185 bbSetInputSpacing(res);
00186 mOutput = 0;
00187 }
00188
00189 }
00190
00191
00192 #endif