1 /*=========================================================================
3 Module: $RCSfile: bbitkResampleImageFilter.h,v $
5 Date: $Date: 2008/10/17 08:18:21 $
6 Version: $Revision: 1.7 $
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();
60 void bbUserConstructor() { Init(); }
61 void bbUserCopyConstructor() { Init(); }
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);
85 void ResampleImageFilter::ProcessSwitch()
87 bbtk::TypeInfo t = bbGetInputIn().type();
88 BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
92 void ResampleImageFilter::Process()
94 bbtkDebugMessageInc("Core",9,
95 "bbitk::ResampleImageFilter::Process<"
96 <<bbtk::TypeName<T>()<<">()"<<std::endl);
99 typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
100 typename FilterType::Pointer filter = FilterType::New();
101 const unsigned int Dimension = ImageType::ImageDimension;
104 T* in = this->bbGetInputIn().get<T*>();
105 filter->SetInput( in );
107 // Size, Spacing, Origin and DefaultPixelVal
108 typename ImageType::SizeType size;
109 typename ImageType::SpacingType spacing;
110 typename ImageType::PointType origin;
111 typename ImageType::RegionType LPR;
112 LPR = in->GetLargestPossibleRegion();
113 size = LPR.GetSize();
114 // origin = LPR.GetIndex(); //in->GetOrigin();
115 for (unsigned int i=0;i<Dimension;++i)
117 origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
118 spacing[i] = bbGetInputSpacing()[i];
119 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
120 size[i] = (long)floor(tmp);
121 // size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
124 filter->SetOutputOrigin (origin);
125 filter->SetSize (size);
126 filter->SetOutputSpacing( spacing );
128 filter->SetDefaultPixelValue (0);
129 filter->SetOutputDirection( in->GetDirection() );
134 typedef itk::AffineTransform < double, Dimension> TransformType;
136 // Instance of the transform object to be passed to the resample filter
137 // By default, identity transform is applied
138 typename TransformType::Pointer transform = TransformType::New();
139 filter->SetTransform ( transform );
143 if ( bbGetInputInterpolation() == "NearestNeighbor" ) {
144 typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;
145 // We create an interpolator of the found type
146 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
147 filter->SetInterpolator( interpolator );
149 else if ( bbGetInputInterpolation() == "BSpline") {
150 typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType;
151 // We create an interpolator of the found type
152 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
153 filter->SetInterpolator(interpolator);
154 // When handling unsigned data, it is possible that the interpolated value is negative
155 // if ( (m_InputImage->GetComponentTypeAsString() == "uchar") ||
156 // (m_InputImage->GetComponentTypeAsString() == "ushort") ||
157 // (m_InputImage->GetComponentTypeAsString() == "uint") ) {
158 // std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
162 else { // if ( bbGetInputInterpolation() == "Linear" ) {
163 typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;
164 // We create an interpolator of the found type
165 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
166 filter->SetInterpolator( interpolator );
170 filter->GetOutput()->Register();
171 if (mOutput) mOutput->UnRegister();
172 this->bbSetOutputOut( filter->GetOutput() );
173 mOutput = filter->GetOutput();
175 bbtkDebugDecTab("Core",9);
179 void ResampleImageFilter::Init()
181 std::vector<double> res;
185 bbSetInputSpacing(res);