]> Creatis software - clitk.git/blob - itk/clitkSegmentationUtils.h
add getMemoryUsage
[clitk.git] / itk / clitkSegmentationUtils.h
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19 #ifndef CLITKSEGMENTATIONUTILS_H
20 #define CLITKSEGMENTATIONUTILS_H
21
22 // clitk
23 #include "clitkCommon.h"
24 #include "clitkAutoCropFilter.h"
25 #include "clitkLabelizeParameters.h"
26
27 // itk
28 #include <itkBoundingBox.h>
29
30 namespace clitk {
31
32   //--------------------------------------------------------------------
33   template<class ImageType>
34   void ComputeBBFromImageRegion(typename ImageType::Pointer image, 
35                                 typename ImageType::RegionType region,
36                                 typename itk::BoundingBox<unsigned long, 
37                                                           ImageType::ImageDimension>::Pointer bb);
38   
39   //--------------------------------------------------------------------
40   template<int Dimension>
41   void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
42                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
43                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2);
44
45   //--------------------------------------------------------------------
46   template<class ImageType>
47   void ComputeRegionFromBB(typename ImageType::Pointer image, 
48                            const typename itk::BoundingBox<unsigned long, 
49                            ImageType::ImageDimension>::Pointer bb, 
50                            typename ImageType::RegionType & region);
51   //--------------------------------------------------------------------
52   template<class TInternalImageType, class TMaskInternalImageType>
53   typename TInternalImageType::Pointer
54   SetBackground(const TInternalImageType * input,
55                 const TMaskInternalImageType * mask, 
56                 typename TMaskInternalImageType::PixelType maskBG, 
57                 typename TInternalImageType::PixelType outValue, 
58                 bool inPlace);
59   //--------------------------------------------------------------------
60
61
62   //--------------------------------------------------------------------
63   template<class ImageType>
64   int GetNumberOfConnectedComponentLabels(typename ImageType::Pointer input, 
65                                           typename ImageType::PixelType BG, 
66                                           bool isFullyConnected);
67   //--------------------------------------------------------------------
68
69
70   //-------------------------------------------------------------------- 
71   template<class TImageType>
72   typename TImageType::Pointer
73   Labelize(const TImageType * input, 
74            typename TImageType::PixelType BG, 
75            bool isFullyConnected, 
76            int minimalComponentSize);
77   template<class TImageType>
78   typename TImageType::Pointer
79   LabelizeAndCountNumberOfObjects(const TImageType * input, 
80                                   typename TImageType::PixelType BG, 
81                                   bool isFullyConnected, 
82                                   int minimalComponentSize, 
83                                   int & nb);
84   //--------------------------------------------------------------------
85
86
87   //--------------------------------------------------------------------
88   template<class ImageType>
89   typename ImageType::Pointer
90   RemoveLabels(typename ImageType::Pointer input, 
91                typename ImageType::PixelType BG, 
92                std::vector<typename ImageType::PixelType> & labelsToRemove);
93   //--------------------------------------------------------------------
94
95
96   //--------------------------------------------------------------------
97   template<class ImageType>
98   typename ImageType::Pointer
99   AutoCrop(typename ImageType::Pointer input, 
100            typename ImageType::PixelType BG) {
101     typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
102     typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
103     autoCropFilter->SetInput(input);
104     autoCropFilter->SetBackgroundValue(BG);
105     autoCropFilter->Update();   
106     return autoCropFilter->GetOutput();
107   }
108   //--------------------------------------------------------------------
109
110
111   //--------------------------------------------------------------------
112   template<class TImageType>
113   typename TImageType::Pointer
114   KeepLabels(const TImageType * input,
115              typename TImageType::PixelType BG, 
116              typename TImageType::PixelType FG,  
117              typename TImageType::PixelType firstKeep, 
118              typename TImageType::PixelType lastKeep, 
119              bool useLastKeep);
120   //--------------------------------------------------------------------
121
122
123   //--------------------------------------------------------------------
124   template<class TImageType>
125   typename TImageType::Pointer
126   LabelizeAndSelectLabels(typename TImageType::Pointer input,
127                           typename TImageType::PixelType BG, 
128                           typename TImageType::PixelType FG, 
129                           bool isFullyConnected,
130                           int minimalComponentSize,
131                           LabelizeParameters<typename TImageType::PixelType> * param);
132
133   //--------------------------------------------------------------------
134   template<class ImageType>
135   typename ImageType::Pointer
136   ResizeImageLike(typename ImageType::Pointer input,
137                   typename ImageType::Pointer like, 
138                   typename ImageType::PixelType BG);
139
140
141   //--------------------------------------------------------------------
142   template<class MaskImageType>
143   typename MaskImageType::Pointer
144   SliceBySliceRelativePosition(const MaskImageType * input,
145                                const MaskImageType * object,
146                                int direction, 
147                                double threshold, 
148                                std::string orientation, 
149                                bool uniqueConnectedComponent=false, 
150                                double spacing=-1, 
151                                bool notflag=false);
152
153   //--------------------------------------------------------------------
154   // In a binary image, search for the point belonging to the FG that
155   // is the most exterma in the direction 'direction' (or in the
156   // opposite if notFlag is given). 
157   template<class ImageType>
158   bool
159   FindExtremaPointInAGivenDirection(const ImageType * input, 
160                                     typename ImageType::PixelType bg, 
161                                     int direction, bool opposite, 
162                                     typename ImageType::PointType & p);
163
164   //--------------------------------------------------------------------
165
166   //--------------------------------------------------------------------
167   // Same as above but check that the found point is not more than
168   // 'distanceMax' away from 'refPoint'
169   template<class ImageType>
170   bool
171   FindExtremaPointInAGivenDirection(const ImageType * input, 
172                                     typename ImageType::PixelType bg, 
173                                     int direction, bool opposite, 
174                                     typename ImageType::PointType refPoint,
175                                     double distanceMax, 
176                                     typename ImageType::PointType & p);
177
178   //--------------------------------------------------------------------
179
180   //--------------------------------------------------------------------
181   template<class ImageType>
182   typename ImageType::Pointer
183   CropImageAlongOneAxis(typename ImageType::Pointer image, 
184                         int dim, double min, double max, 
185                         bool autoCrop = false,
186                         typename ImageType::PixelType BG=0);
187   template<class ImageType>
188   typename ImageType::Pointer
189   CropImageAbove(typename ImageType::Pointer image, 
190                  int dim, double min, 
191                  bool autoCrop = false,
192                  typename ImageType::PixelType BG=0);
193   template<class ImageType>
194   typename ImageType::Pointer
195   CropImageBelow(typename ImageType::Pointer image, 
196                  int dim, double max,
197                  bool autoCrop = false,
198                  typename ImageType::PixelType BG=0);
199   //--------------------------------------------------------------------
200
201
202   //--------------------------------------------------------------------
203   template<class ImageType>
204   void
205   ComputeCentroids(typename ImageType::Pointer image, 
206                    typename ImageType::PixelType BG, 
207                    std::vector<typename ImageType::PointType> & centroids);
208   //--------------------------------------------------------------------
209
210
211   //--------------------------------------------------------------------
212   template<class ImageType>
213   void
214   ExtractSlices(typename ImageType::Pointer image, 
215                 int dim, 
216                 std::vector< typename itk::Image<typename ImageType::PixelType, 
217                 ImageType::ImageDimension-1>::Pointer > & slices);
218   //--------------------------------------------------------------------
219
220
221   //--------------------------------------------------------------------
222   template<class ImageType>
223   typename ImageType::Pointer
224   JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType, 
225              ImageType::ImageDimension-1>::Pointer > & slices, 
226              typename ImageType::Pointer input, 
227              int dim);
228   //--------------------------------------------------------------------
229
230
231   //--------------------------------------------------------------------
232   // Set of tools to manage 3D points and 2D points in slices  
233   template<class ImageType>
234   class PointsUtils
235   {
236     typedef typename ImageType::PointType PointType3D;
237     typedef typename ImageType::PixelType PixelType;
238     typedef typename ImageType::Pointer ImagePointer;
239     typedef typename ImageType::ConstPointer ImageConstPointer;
240     typedef itk::Image<PixelType, 2> SliceType;
241     typedef typename SliceType::PointType PointType2D;
242     
243     typedef std::map<int, PointType2D> MapPoint2DType;
244     typedef std::vector<PointType3D> VectorPoint3DType;
245   public:
246     static void Convert2DTo3D(const PointType2D & p2D, 
247                               ImagePointer image, 
248                               const int slice, 
249                               PointType3D & p3D);
250     static void Convert2DTo3DList(const MapPoint2DType & map, 
251                                   ImagePointer image, 
252                                   VectorPoint3DType & list);
253   };
254
255   //--------------------------------------------------------------------
256   template<class ImageType>
257   void 
258   WriteListOfLandmarks(std::vector<typename ImageType::PointType> points, 
259                        std::string filename);
260   //--------------------------------------------------------------------
261
262
263   //--------------------------------------------------------------------
264   template<class ImageType>
265   typename ImageType::Pointer
266   Dilate(typename ImageType::Pointer image, 
267               double radiusInMM,               
268               typename ImageType::PixelType BG, 
269               typename ImageType::PixelType FG, 
270               bool extendSupport);
271   template<class ImageType>
272   typename ImageType::Pointer
273   Dilate(typename ImageType::Pointer image, 
274               typename ImageType::SizeType radius, 
275               typename ImageType::PixelType BG, 
276               typename ImageType::PixelType FG, 
277               bool extendSupport);
278   template<class ImageType>
279   typename ImageType::Pointer  
280   Dilate(typename ImageType::Pointer image, 
281               typename ImageType::PointType radiusInMM, 
282               typename ImageType::PixelType BG, 
283               typename ImageType::PixelType FG, 
284               bool extendSupport);
285   //--------------------------------------------------------------------
286
287   //--------------------------------------------------------------------
288   template<class ValueType, class VectorType>
289   void ConvertOption(std::string optionName, uint given, 
290                      ValueType * values, VectorType & p, 
291                      uint dim, bool required);
292 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED)         \
293   ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
294   //--------------------------------------------------------------------
295
296   //--------------------------------------------------------------------
297   template<class ImageType>
298   void 
299   SliceBySliceSetBackgroundFromLineSeparation(typename ImageType::Pointer input, 
300                                               std::vector<typename ImageType::PointType> & lA, 
301                                               std::vector<typename ImageType::PointType> & lB, 
302                                               typename ImageType::PixelType BG, 
303                                               int mainDirection, 
304                                               double offsetToKeep);
305   
306
307 }
308
309 #include "clitkSegmentationUtils.txx"
310
311 #endif