1 /*=========================================================================
3 Module: $RCSfile: bbitkResampleImageFilter.h,v $
5 Date: $Date: 2009/05/15 14:57:58 $
6 Version: $Revision: 1.9 $
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 class /*BBTK_EXPORT*/ ResampleImageFilter
48 public bbtk::AtomicBlackBox
50 BBTK_BLACK_BOX_INTERFACE(ResampleImageFilter,
51 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();
63 BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
64 bbtk::AtomicBlackBox);
65 BBTK_NAME("ResampleImageFilter");
66 BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
67 BBTK_DESCRIPTION("Resamples an image");
68 BBTK_CATEGORY("image;filter");
69 BBTK_INPUT(ResampleImageFilter,In,
70 "Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
71 BBTK_INPUT(ResampleImageFilter,Spacing,
72 "Spacing",std::vector<double>,"spacing");
73 BBTK_INPUT(ResampleImageFilter,Interpolation,
74 "Interpolation",std::string,"");
75 BBTK_OUTPUT(ResampleImageFilter,Out,
76 "Output image. Of the same type and dimension than the input image",
78 BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
82 void ResampleImageFilter::ProcessSwitch()
84 bbtk::TypeInfo t = bbGetInputIn().type();
85 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
89 void ResampleImageFilter::Process()
91 bbtkDebugMessageInc("Core",9,
92 "bbitk::ResampleImageFilter::Process<"
93 <<bbtk::TypeName<T>()<<">()"<<std::endl);
96 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
97 typename FilterType::Pointer filter = FilterType::New();
98 const unsigned int Dimension = ImageType::ImageDimension;
101 T* in = this->bbGetInputIn().get<T*>();
102 filter->SetInput( in );
104 // Size, Spacing, Origin and DefaultPixelVal
105 typename ImageType::SizeType size;
106 typename ImageType::SpacingType spacing;
107 typename ImageType::PointType origin;
108 typename ImageType::RegionType LPR;
109 LPR = in->GetLargestPossibleRegion();
110 size = LPR.GetSize();
111 // origin = LPR.GetIndex(); //in->GetOrigin();
112 for (unsigned int i=0;i<Dimension;++i)
114 origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
115 spacing[i] = bbGetInputSpacing()[i];
116 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
117 size[i] = (long)floor(tmp);
118 // size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
121 filter->SetOutputOrigin (origin);
122 filter->SetSize (size);
123 filter->SetOutputSpacing( spacing );
125 filter->SetDefaultPixelValue (0);
126 filter->SetOutputDirection( in->GetDirection() );
131 typedef itk::AffineTransform < double, Dimension> TransformType;
133 // Instance of the transform object to be passed to the resample filter
134 // By default, identity transform is applied
135 typename TransformType::Pointer transform = TransformType::New();
136 filter->SetTransform ( transform );
140 if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
141 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
142 // We create an interpolator of the found type
143 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
144 filter->SetInterpolator( interpolator );
146 else if ( bbGetInputInterpolation() == "BSpline") {
147 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
148 // We create an interpolator of the found type
149 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
150 filter->SetInterpolator(interpolator);
151 // When handling unsigned data, it is possible that the interpolated value is negative
152 // if ( (m_InputImage->GetComponentTypeAsString() == "uchar") ||
153 // (m_InputImage->GetComponentTypeAsString() == "ushort") ||
154 // (m_InputImage->GetComponentTypeAsString() == "uint") ) {
155 // std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
159 else { // if ( bbGetInputInterpolation() == "Linear" ) {
160 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
161 // We create an interpolator of the found type
162 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
163 filter->SetInterpolator( interpolator );
167 filter->GetOutput()->Register();
168 if (mOutput) mOutput->UnRegister();
169 this->bbSetOutputOut( filter->GetOutput() );
170 mOutput = filter->GetOutput();
172 bbtkDebugDecTab("Core",9);
176 //-----------------------------------------------------------------
177 void ResampleImageFilter::bbUserSetDefaultValues()
179 std::vector<double> res;
183 bbSetInputSpacing(res);
187 //-----------------------------------------------------------------
188 void ResampleImageFilter::bbUserInitializeProcessing()
192 //-----------------------------------------------------------------
193 void ResampleImageFilter::bbUserFinalizeProcessing()