]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkResampleImageFilter.h
no message
[bbtk.git] / packages / itk / src / bbitkResampleImageFilter.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbitkResampleImageFilter.h,v $
4   Language:  C++
5   Date:      $Date: 2011/05/03 12:24:22 $
6   Version:   $Revision: 1.11 $
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, 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);
57   private :
58     inline void ProcessSwitch();
59     template <class T> void Process();
60     itk::Object* mOutput;
61   };
62  //===================================================
63    
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  //===================================================
77  
78
79
80  //===================================================
81    void ResampleImageFilter::ProcessSwitch()
82   {
83     bbtk::TypeInfo t = bbGetInputIn().type();
84     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
85   }
86  //===================================================
87  
88  //===================================================
89    template <class T> 
90   void ResampleImageFilter::Process()
91   {
92     bbtkDebugMessageInc("Core",9,
93                         "bbitk::ResampleImageFilter::Process<"
94                         <<bbtk::TypeName<T>()<<">()"<<std::endl);
95  
96     typedef T ImageType;
97     typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
98     typename FilterType::Pointer filter = FilterType::New();
99     const unsigned int Dimension = ImageType::ImageDimension;
100
101     // Input
102     T* in = this->bbGetInputIn().get<T*>();
103     filter->SetInput( in );
104
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) 
114       {
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]);
120        }
121    
122     filter->SetOutputOrigin (origin);
123     filter->SetSize (size);
124     filter->SetOutputSpacing( spacing );
125
126     filter->SetDefaultPixelValue (0);
127     filter->SetOutputDirection( in->GetDirection() );
128
129
130
131     // Transform
132     typedef itk::AffineTransform < double, Dimension> TransformType;
133     
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 );
138
139     
140  
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 );
146     }
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;
157       // }
158     } //end else if
159     // Interpolation 
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 );
165     }
166
167     filter->Update();
168     filter->GetOutput()->Register();
169     if (mOutput) mOutput->UnRegister();
170     this->bbSetOutputOut( filter->GetOutput() );
171     mOutput = filter->GetOutput();
172
173     bbtkDebugDecTab("Core",9);
174   }
175    //===================================================
176  
177
178         //-----------------------------------------------------------------     
179         void ResampleImageFilter::bbUserSetDefaultValues()
180         {
181                 std::vector<double> res;
182                 res.push_back(1);
183                 res.push_back(1);
184                 res.push_back(1);
185                 bbSetInputSpacing(res);
186                 mOutput = 0;
187         }
188         
189         //-----------------------------------------------------------------     
190         void ResampleImageFilter::bbUserInitializeProcessing()
191         {
192         }
193         
194         //-----------------------------------------------------------------     
195         void ResampleImageFilter::bbUserFinalizeProcessing()
196         {
197         }       
198         
199
200 } // EO namespace bbitk
201
202 #endif   // _USE_ITK_