1 /*=========================================================================
3 Module: $RCSfile: bbitkResampleImageFilter.h,v $
5 Date: $Date: 2011/05/03 12:24:22 $
6 Version: $Revision: 1.11 $
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, bbtk::AtomicBlackBox);
52 BBTK_DECLARE_INPUT(In,anyImagePointer);
53 BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
54 BBTK_DECLARE_INPUT(Interpolation,std::string);
55 BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
56 BBTK_PROCESS(ProcessSwitch);
58 inline void ProcessSwitch();
59 template <class T> void Process();
62 //===================================================
64 //===================================================
65 BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
66 bbtk::AtomicBlackBox);
67 BBTK_NAME("ResampleImageFilter");
68 BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
69 BBTK_DESCRIPTION("Resamples an image");
70 BBTK_CATEGORY("image;filter");
71 BBTK_INPUT(ResampleImageFilter,In,"Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
72 BBTK_INPUT(ResampleImageFilter,Spacing,"Spacing",std::vector<double>,"spacing");
73 BBTK_INPUT(ResampleImageFilter,Interpolation,"Interpolation: Linear (default) || BSpline || NearestNeighbor) ",std::string,"");
74 BBTK_OUTPUT(ResampleImageFilter,Out,"Output image. Of the same type and dimension than the input image",anyImagePointer,"");
75 BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
76 //===================================================
80 //===================================================
81 void ResampleImageFilter::ProcessSwitch()
83 bbtk::TypeInfo t = bbGetInputIn().type();
84 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
86 //===================================================
88 //===================================================
90 void ResampleImageFilter::Process()
92 bbtkDebugMessageInc("Core",9,
93 "bbitk::ResampleImageFilter::Process<"
94 <<bbtk::TypeName<T>()<<">()"<<std::endl);
97 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
98 typename FilterType::Pointer filter = FilterType::New();
99 const unsigned int Dimension = ImageType::ImageDimension;
102 T* in = this->bbGetInputIn().get<T*>();
103 filter->SetInput( in );
105 // Size, Spacing, Origin and DefaultPixelVal
106 typename ImageType::SizeType size;
107 typename ImageType::SpacingType spacing;
108 typename ImageType::PointType origin;
109 typename ImageType::RegionType LPR;
110 LPR = in->GetLargestPossibleRegion();
111 size = LPR.GetSize();
112 // origin = LPR.GetIndex(); //in->GetOrigin();
113 for (unsigned int i=0;i<Dimension;++i)
115 origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
116 spacing[i] = bbGetInputSpacing()[i];
117 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
118 size[i] = (long)floor(tmp);
119 // size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
122 filter->SetOutputOrigin (origin);
123 filter->SetSize (size);
124 filter->SetOutputSpacing( spacing );
126 filter->SetDefaultPixelValue (0);
127 filter->SetOutputDirection( in->GetDirection() );
132 typedef itk::AffineTransform < double, Dimension> TransformType;
134 // Instance of the transform object to be passed to the resample filter
135 // By default, identity transform is applied
136 typename TransformType::Pointer transform = TransformType::New();
137 filter->SetTransform ( transform );
141 if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
142 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
143 // We create an interpolator of the found type
144 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
145 filter->SetInterpolator( interpolator );
147 else if ( bbGetInputInterpolation() == "BSpline") {
148 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
149 // We create an interpolator of the found type
150 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
151 filter->SetInterpolator(interpolator);
152 // When handling unsigned data, it is possible that the interpolated value is negative
153 // if ( (m_InputImage->GetComponentTypeAsString() == "uchar") ||
154 // (m_InputImage->GetComponentTypeAsString() == "ushort") ||
155 // (m_InputImage->GetComponentTypeAsString() == "uint") ) {
156 // std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
160 else { // if ( bbGetInputInterpolation() == "Linear" ) {
161 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
162 // We create an interpolator of the found type
163 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
164 filter->SetInterpolator( interpolator );
168 filter->GetOutput()->Register();
169 if (mOutput) mOutput->UnRegister();
170 this->bbSetOutputOut( filter->GetOutput() );
171 mOutput = filter->GetOutput();
173 bbtkDebugDecTab("Core",9);
175 //===================================================
178 //-----------------------------------------------------------------
179 void ResampleImageFilter::bbUserSetDefaultValues()
181 std::vector<double> res;
185 bbSetInputSpacing(res);
189 //-----------------------------------------------------------------
190 void ResampleImageFilter::bbUserInitializeProcessing()
194 //-----------------------------------------------------------------
195 void ResampleImageFilter::bbUserFinalizeProcessing()
200 } // EO namespace bbitk