1 /*=========================================================================
3 Module: $RCSfile: bbitkResampleImageFilter.h,v $
5 Date: $Date: 2011/06/30 09:39:41 $
6 Version: $Revision: 1.12 $
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"
43 #include <itkAffineTransform.h>
49 //===================================================
50 class /*BBTK_EXPORT*/ ResampleImageFilter
52 public bbtk::AtomicBlackBox
54 BBTK_BLACK_BOX_INTERFACE(ResampleImageFilter, bbtk::AtomicBlackBox);
55 BBTK_DECLARE_INPUT(In,anyImagePointer);
56 BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
57 BBTK_DECLARE_INPUT(Interpolation,std::string);
58 BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
59 BBTK_PROCESS(ProcessSwitch);
61 inline void ProcessSwitch();
62 template <class T> void Process();
65 //===================================================
67 //===================================================
68 BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
69 bbtk::AtomicBlackBox);
70 BBTK_NAME("ResampleImageFilter");
71 BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
72 BBTK_DESCRIPTION("Resamples an image");
73 BBTK_CATEGORY("image;filter");
74 BBTK_INPUT(ResampleImageFilter,In,"Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
75 BBTK_INPUT(ResampleImageFilter,Spacing,"Spacing",std::vector<double>,"spacing");
76 BBTK_INPUT(ResampleImageFilter,Interpolation,"Interpolation: Linear (default) || BSpline || NearestNeighbor) ",std::string,"");
77 BBTK_OUTPUT(ResampleImageFilter,Out,"Output image. Of the same type and dimension than the input image",anyImagePointer,"");
78 BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
79 //===================================================
83 //===================================================
84 void ResampleImageFilter::ProcessSwitch()
86 bbtk::TypeInfo t = bbGetInputIn().type();
87 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
89 //===================================================
91 //===================================================
93 void ResampleImageFilter::Process()
95 bbtkDebugMessageInc("Core",9,
96 "bbitk::ResampleImageFilter::Process<"
97 <<bbtk::TypeName<T>()<<">()"<<std::endl);
100 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
101 typename FilterType::Pointer filter = FilterType::New();
102 const unsigned int Dimension = ImageType::ImageDimension;
105 T* in = this->bbGetInputIn().get<T*>();
106 filter->SetInput( in );
108 // Size, Spacing, Origin and DefaultPixelVal
109 typename ImageType::SizeType size;
110 typename ImageType::SpacingType spacing;
111 typename ImageType::PointType origin;
112 typename ImageType::RegionType LPR;
113 LPR = in->GetLargestPossibleRegion();
114 size = LPR.GetSize();
115 // origin = LPR.GetIndex(); //in->GetOrigin();
116 for (unsigned int i=0;i<Dimension;++i)
118 origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
119 spacing[i] = bbGetInputSpacing()[i];
120 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
121 size[i] = (long)floor(tmp);
122 // size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
125 filter->SetOutputOrigin (origin);
126 filter->SetSize (size);
127 filter->SetOutputSpacing( spacing );
129 filter->SetDefaultPixelValue (0);
130 filter->SetOutputDirection( in->GetDirection() );
135 typedef itk::AffineTransform < double, Dimension> TransformType;
137 // Instance of the transform object to be passed to the resample filter
138 // By default, identity transform is applied
139 typename TransformType::Pointer transform = TransformType::New();
140 filter->SetTransform ( transform );
144 if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
145 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
146 // We create an interpolator of the found type
147 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
148 filter->SetInterpolator( interpolator );
150 else if ( bbGetInputInterpolation() == "BSpline") {
151 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
152 // We create an interpolator of the found type
153 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
154 filter->SetInterpolator(interpolator);
155 // When handling unsigned data, it is possible that the interpolated value is negative
156 // if ( (m_InputImage->GetComponentTypeAsString() == "uchar") ||
157 // (m_InputImage->GetComponentTypeAsString() == "ushort") ||
158 // (m_InputImage->GetComponentTypeAsString() == "uint") ) {
159 // std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
163 else { // if ( bbGetInputInterpolation() == "Linear" ) {
164 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
165 // We create an interpolator of the found type
166 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
167 filter->SetInterpolator( interpolator );
171 filter->GetOutput()->Register();
172 if (mOutput) mOutput->UnRegister();
173 this->bbSetOutputOut( filter->GetOutput() );
174 mOutput = filter->GetOutput();
176 bbtkDebugDecTab("Core",9);
178 //===================================================
181 //-----------------------------------------------------------------
182 void ResampleImageFilter::bbUserSetDefaultValues()
184 std::vector<double> res;
188 bbSetInputSpacing(res);
192 //-----------------------------------------------------------------
193 void ResampleImageFilter::bbUserInitializeProcessing()
197 //-----------------------------------------------------------------
198 void ResampleImageFilter::bbUserFinalizeProcessing()
203 } // EO namespace bbitk