]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkResampleImageFilter.h
*** empty log message ***
[bbtk.git] / packages / itk / src / bbitkResampleImageFilter.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbitkResampleImageFilter.h,v $
4   Language:  C++
5   Date:      $Date: 2009/05/18 10:45:44 $
6   Version:   $Revision: 1.10 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30
31
32 #ifdef _USE_ITK_
33
34 #include <math.h>
35 #include "bbtkAtomicBlackBox.h"
36 #include "itkResampleImageFilter.h"
37 #include "bbitkImage.h"
38 #include "itkNearestNeighborInterpolateImageFunction.h"
39 #include "itkLinearInterpolateImageFunction.h"
40 #include "itkBSplineInterpolateImageFunction.h"
41
42 namespace bbitk
43 {
44
45  
46  //===================================================
47    class /*BBTK_EXPORT*/ ResampleImageFilter
48     : 
49     public bbtk::AtomicBlackBox
50   {
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);
58   private :
59     inline void ProcessSwitch();
60     template <class T> void Process();
61     itk::Object* mOutput;
62   };
63  //===================================================
64    
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",
80               anyImagePointer,"");
81   BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
82  //===================================================
83  
84
85
86  //===================================================
87    void ResampleImageFilter::ProcessSwitch()
88   {
89     bbtk::TypeInfo t = bbGetInputIn().type();
90     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
91   }
92  //===================================================
93  
94  //===================================================
95    template <class T> 
96   void ResampleImageFilter::Process()
97   {
98     bbtkDebugMessageInc("Core",9,
99                         "bbitk::ResampleImageFilter::Process<"
100                         <<bbtk::TypeName<T>()<<">()"<<std::endl);
101  
102     typedef T ImageType;
103     typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
104     typename FilterType::Pointer filter = FilterType::New();
105     const unsigned int Dimension = ImageType::ImageDimension;
106
107     // Input
108     T* in = this->bbGetInputIn().get<T*>();
109     filter->SetInput( in );
110
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) 
120       {
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]);
126        }
127    
128     filter->SetOutputOrigin (origin);
129     filter->SetSize (size);
130     filter->SetOutputSpacing( spacing );
131
132     filter->SetDefaultPixelValue (0);
133     filter->SetOutputDirection( in->GetDirection() );
134
135
136
137     // Transform
138     typedef itk::AffineTransform < double, Dimension> TransformType;
139     
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 );
144
145     
146  
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 );
152     }
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;
163       // }
164     } //end else if
165     // Interpolation 
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 );
171     }
172
173     filter->Update();
174     filter->GetOutput()->Register();
175     if (mOutput) mOutput->UnRegister();
176     this->bbSetOutputOut( filter->GetOutput() );
177     mOutput = filter->GetOutput();
178
179     bbtkDebugDecTab("Core",9);
180   }
181    //===================================================
182  
183
184         //-----------------------------------------------------------------     
185         void ResampleImageFilter::bbUserSetDefaultValues()
186         {
187                 std::vector<double> res;
188                 res.push_back(1);
189                 res.push_back(1);
190                 res.push_back(1);
191                 bbSetInputSpacing(res);
192                 mOutput = 0;
193         }
194         
195         //-----------------------------------------------------------------     
196         void ResampleImageFilter::bbUserInitializeProcessing()
197         {
198         }
199         
200         //-----------------------------------------------------------------     
201         void ResampleImageFilter::bbUserFinalizeProcessing()
202         {
203         }       
204         
205
206 }
207 // EO namespace bbtk
208
209 #endif