]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkResampleImageFilter.h
#3073 BBTK Bug New Normal - message documentation in boxes
[bbtk.git] / packages / itk / src / bbitkResampleImageFilter.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbitkResampleImageFilter.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:50:39 $
33   Version:   $Revision: 1.13 $
34 =========================================================================*/
35
36
37
38
39 #ifdef _USE_ITK_
40
41 #include <math.h>
42 #include "bbtkAtomicBlackBox.h"
43 #include "itkResampleImageFilter.h"
44 #include "bbitkImage.h"
45 #include "itkNearestNeighborInterpolateImageFunction.h"
46 #include "itkLinearInterpolateImageFunction.h"
47 #include "itkBSplineInterpolateImageFunction.h"
48
49 // For ITK4
50 #include <itkAffineTransform.h>
51
52 namespace bbitk
53 {
54
55  
56  //===================================================
57    class /*BBTK_EXPORT*/ ResampleImageFilter
58     : 
59     public bbtk::AtomicBlackBox
60   {
61     BBTK_BLACK_BOX_INTERFACE(ResampleImageFilter, bbtk::AtomicBlackBox);
62         BBTK_DECLARE_INPUT(In,anyImagePointer);
63         BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
64         BBTK_DECLARE_INPUT(Interpolation,std::string);
65         BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
66     BBTK_PROCESS(ProcessSwitch);
67   private :
68     inline void ProcessSwitch();
69     template <class T> void Process();
70     itk::Object* mOutput;
71   };
72  //===================================================
73    
74  //===================================================
75    BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
76                                 bbtk::AtomicBlackBox);
77   BBTK_NAME("ResampleImageFilter");
78   BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
79   BBTK_DESCRIPTION("Resamples an image");
80   BBTK_CATEGORY("image;filter");
81   BBTK_INPUT(ResampleImageFilter,In,"Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
82   BBTK_INPUT(ResampleImageFilter,Spacing,"Spacing",std::vector<double>,"spacing");
83   BBTK_INPUT(ResampleImageFilter,Interpolation,"Interpolation: Linear (default) || BSpline || NearestNeighbor)  ",std::string,"");
84   BBTK_OUTPUT(ResampleImageFilter,Out,"Output image. Of the same type and dimension than the input image",anyImagePointer,"");
85   BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
86  //===================================================
87  
88
89
90  //===================================================
91    void ResampleImageFilter::ProcessSwitch()
92   {
93     bbtk::TypeInfo t = bbGetInputIn().type();
94     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
95   }
96  //===================================================
97  
98  //===================================================
99    template <class T> 
100   void ResampleImageFilter::Process()
101   {
102     bbtkDebugMessageInc("Core",9,
103                         "bbitk::ResampleImageFilter::Process<"
104                         <<bbtk::TypeName<T>()<<">()"<<std::endl);
105  
106     typedef T ImageType;
107     typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
108     typename FilterType::Pointer filter = FilterType::New();
109     const unsigned int Dimension = ImageType::ImageDimension;
110
111     // Input
112     T* in = this->bbGetInputIn().get<T*>();
113     filter->SetInput( in );
114
115     // Size, Spacing, Origin and DefaultPixelVal
116     typename ImageType::SizeType size;
117     typename ImageType::SpacingType spacing;
118     typename ImageType::PointType origin;
119     typename ImageType::RegionType LPR;
120     LPR = in->GetLargestPossibleRegion();
121     size = LPR.GetSize();
122     //    origin = LPR.GetIndex(); //in->GetOrigin();
123      for (unsigned int i=0;i<Dimension;++i) 
124       {
125         origin[i] = LPR.GetIndex()[i]*in->GetSpacing()[i];
126         spacing[i] = bbGetInputSpacing()[i];
127         double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
128         size[i] = (long)floor(tmp);
129 //      size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
130        }
131    
132     filter->SetOutputOrigin (origin);
133     filter->SetSize (size);
134     filter->SetOutputSpacing( spacing );
135
136     filter->SetDefaultPixelValue (0);
137     filter->SetOutputDirection( in->GetDirection() );
138
139
140
141     // Transform
142     typedef itk::AffineTransform < double, Dimension> TransformType;
143     
144     // Instance of the transform object to be passed to the resample filter
145     // By default, identity transform is applied
146     typename TransformType::Pointer transform =  TransformType::New();
147     filter->SetTransform ( transform );
148
149     
150  
151     if  ( bbGetInputInterpolation() == "NearestNeighbor" ) {
152       typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;     
153       // We create an interpolator of the found type 
154       typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
155       filter->SetInterpolator( interpolator );
156     }
157     else if  ( bbGetInputInterpolation() == "BSpline") { 
158       typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType; 
159       // We create an interpolator of the found type 
160       typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
161       filter->SetInterpolator(interpolator);    
162       // When handling unsigned data, it is possible that the interpolated value is negative
163       // if ( (m_InputImage->GetComponentTypeAsString() == "uchar")   || 
164       //           (m_InputImage->GetComponentTypeAsString() == "ushort")  ||
165       //           (m_InputImage->GetComponentTypeAsString() == "uint") ) {   
166       //        std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
167       // }
168     } //end else if
169     // Interpolation 
170     else { // if ( bbGetInputInterpolation() == "Linear" ) {
171       typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;     
172       // We create an interpolator of the found type 
173       typename InterpolatorType::Pointer interpolator =  InterpolatorType::New();
174       filter->SetInterpolator( interpolator );
175     }
176
177     filter->Update();
178     filter->GetOutput()->Register();
179     if (mOutput) mOutput->UnRegister();
180     this->bbSetOutputOut( filter->GetOutput() );
181     mOutput = filter->GetOutput();
182
183     bbtkDebugDecTab("Core",9);
184   }
185    //===================================================
186  
187
188         //-----------------------------------------------------------------     
189         void ResampleImageFilter::bbUserSetDefaultValues()
190         {
191                 std::vector<double> res;
192                 res.push_back(1);
193                 res.push_back(1);
194                 res.push_back(1);
195                 bbSetInputSpacing(res);
196                 mOutput = 0;
197         }
198         
199         //-----------------------------------------------------------------     
200         void ResampleImageFilter::bbUserInitializeProcessing()
201         {
202         }
203         
204         //-----------------------------------------------------------------     
205         void ResampleImageFilter::bbUserFinalizeProcessing()
206         {
207         }       
208         
209
210 } // EO namespace bbitk
211
212 #endif   // _USE_ITK_