1 /*=========================================================================
3 Module: $RCSfile: bbitkResampleImageFilter.h,v $
5 Date: $Date: 2009/05/18 10:45:44 $
6 Version: $Revision: 1.10 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
35 #include "bbtkAtomicBlackBox.h"
36 #include "itkResampleImageFilter.h"
37 #include "bbitkImage.h"
38 #include "itkNearestNeighborInterpolateImageFunction.h"
39 #include "itkLinearInterpolateImageFunction.h"
40 #include "itkBSplineInterpolateImageFunction.h"
46 //===================================================
47 class /*BBTK_EXPORT*/ ResampleImageFilter
49 public bbtk::AtomicBlackBox
51 BBTK_BLACK_BOX_INTERFACE(ResampleImageFilter,
52 bbtk::AtomicBlackBox);
53 BBTK_DECLARE_INPUT(In,anyImagePointer);
54 BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
55 BBTK_DECLARE_INPUT(Interpolation,std::string);
56 BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
57 BBTK_PROCESS(ProcessSwitch);
59 inline void ProcessSwitch();
60 template <class T> void Process();
63 //===================================================
65 //===================================================
66 BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
67 bbtk::AtomicBlackBox);
68 BBTK_NAME("ResampleImageFilter");
69 BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
70 BBTK_DESCRIPTION("Resamples an image");
71 BBTK_CATEGORY("image;filter");
72 BBTK_INPUT(ResampleImageFilter,In,
73 "Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
74 BBTK_INPUT(ResampleImageFilter,Spacing,
75 "Spacing",std::vector<double>,"spacing");
76 BBTK_INPUT(ResampleImageFilter,Interpolation,
77 "Interpolation",std::string,"");
78 BBTK_OUTPUT(ResampleImageFilter,Out,
79 "Output image. Of the same type and dimension than the input image",
81 BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
82 //===================================================
86 //===================================================
87 void ResampleImageFilter::ProcessSwitch()
89 bbtk::TypeInfo t = bbGetInputIn().type();
90 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
92 //===================================================
94 //===================================================
96 void ResampleImageFilter::Process()
98 bbtkDebugMessageInc("Core",9,
99 "bbitk::ResampleImageFilter::Process<"
100 <<bbtk::TypeName<T>()<<">()"<<std::endl);
103 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
104 typename FilterType::Pointer filter = FilterType::New();
105 const unsigned int Dimension = ImageType::ImageDimension;
108 T* in = this->bbGetInputIn().get<T*>();
109 filter->SetInput( in );
111 // Size, Spacing, Origin and DefaultPixelVal
112 typename ImageType::SizeType size;
113 typename ImageType::SpacingType spacing;
114 typename ImageType::PointType origin;
115 typename ImageType::RegionType LPR;
116 LPR = in->GetLargestPossibleRegion();
117 size = LPR.GetSize();
118 // origin = LPR.GetIndex(); //in->GetOrigin();
119 for (unsigned int i=0;i<Dimension;++i)
121 origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
122 spacing[i] = bbGetInputSpacing()[i];
123 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
124 size[i] = (long)floor(tmp);
125 // size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
128 filter->SetOutputOrigin (origin);
129 filter->SetSize (size);
130 filter->SetOutputSpacing( spacing );
132 filter->SetDefaultPixelValue (0);
133 filter->SetOutputDirection( in->GetDirection() );
138 typedef itk::AffineTransform < double, Dimension> TransformType;
140 // Instance of the transform object to be passed to the resample filter
141 // By default, identity transform is applied
142 typename TransformType::Pointer transform = TransformType::New();
143 filter->SetTransform ( transform );
147 if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
148 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
149 // We create an interpolator of the found type
150 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
151 filter->SetInterpolator( interpolator );
153 else if ( bbGetInputInterpolation() == "BSpline") {
154 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
155 // We create an interpolator of the found type
156 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
157 filter->SetInterpolator(interpolator);
158 // When handling unsigned data, it is possible that the interpolated value is negative
159 // if ( (m_InputImage->GetComponentTypeAsString() == "uchar") ||
160 // (m_InputImage->GetComponentTypeAsString() == "ushort") ||
161 // (m_InputImage->GetComponentTypeAsString() == "uint") ) {
162 // std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
166 else { // if ( bbGetInputInterpolation() == "Linear" ) {
167 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
168 // We create an interpolator of the found type
169 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
170 filter->SetInterpolator( interpolator );
174 filter->GetOutput()->Register();
175 if (mOutput) mOutput->UnRegister();
176 this->bbSetOutputOut( filter->GetOutput() );
177 mOutput = filter->GetOutput();
179 bbtkDebugDecTab("Core",9);
181 //===================================================
184 //-----------------------------------------------------------------
185 void ResampleImageFilter::bbUserSetDefaultValues()
187 std::vector<double> res;
191 bbSetInputSpacing(res);
195 //-----------------------------------------------------------------
196 void ResampleImageFilter::bbUserInitializeProcessing()
200 //-----------------------------------------------------------------
201 void ResampleImageFilter::bbUserFinalizeProcessing()