]> Creatis software - clitk.git/blob - itk/clitkSegmentationUtils.h
Missing include
[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 #include "clitkExtractSliceFilter.h"
27
28 // itk
29 #include <itkBoundingBox.h>
30 #include <itkJoinSeriesImageFilter.h>
31
32 /*
33   According to 
34   http://answerpot.com/showthread.php?357451-Itk::SmartPointer%20-%20problem%20making%20code%20const-correct
35   it is better to take raw pointer as argument instead of SmartPointer.
36 */
37
38 namespace clitk {
39
40   //--------------------------------------------------------------------
41   template<class ImageType>
42   void ComputeBBFromImageRegion(const ImageType * image, 
43                                 typename ImageType::RegionType region,
44                                 typename itk::BoundingBox<unsigned long, 
45                                                           ImageType::ImageDimension>::Pointer bb);
46   
47   //--------------------------------------------------------------------
48   template<int Dimension>
49   void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
50                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
51                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2);
52
53   //--------------------------------------------------------------------
54   template<class ImageType>
55   void ComputeRegionFromBB(const ImageType * image, 
56                            const typename itk::BoundingBox<unsigned long, 
57                                                            ImageType::ImageDimension>::Pointer bb, 
58                            typename ImageType::RegionType & region);
59   //--------------------------------------------------------------------
60   template<class TInternalImageType, class TMaskInternalImageType>
61   typename TInternalImageType::Pointer
62   SetBackground(const TInternalImageType * input,
63                 const TMaskInternalImageType * mask, 
64                 typename TMaskInternalImageType::PixelType maskBG, 
65                 typename TInternalImageType::PixelType outValue, 
66                 bool inPlace);
67   //--------------------------------------------------------------------
68
69
70   //--------------------------------------------------------------------
71   template<class ImageType>
72   int GetNumberOfConnectedComponentLabels(const ImageType * input, 
73                                           typename ImageType::PixelType BG, 
74                                           bool isFullyConnected);
75   //--------------------------------------------------------------------
76
77
78   //-------------------------------------------------------------------- 
79   template<class TImageType>
80   typename TImageType::Pointer
81   Labelize(const TImageType * input, typename TImageType::PixelType BG, 
82            bool isFullyConnected, int minimalComponentSize);
83   template<class TImageType>
84   typename TImageType::Pointer
85   LabelizeAndCountNumberOfObjects(const TImageType * input, 
86                                   typename TImageType::PixelType BG, 
87                                   bool isFullyConnected, 
88                                   int minimalComponentSize, 
89                                   int & nb);
90   //--------------------------------------------------------------------
91
92
93   //--------------------------------------------------------------------
94   template<class ImageType>
95   typename ImageType::Pointer
96   RemoveLabels(const ImageType * input, 
97                typename ImageType::PixelType BG, 
98                std::vector<typename ImageType::PixelType> & labelsToRemove);
99   //--------------------------------------------------------------------
100
101
102   //--------------------------------------------------------------------
103   template<class ImageType>
104   typename ImageType::Pointer
105   AutoCrop(const ImageType * input, 
106            typename ImageType::PixelType BG) {
107     typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
108     typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
109     autoCropFilter->SetInput(input);
110     autoCropFilter->SetBackgroundValue(BG);
111     autoCropFilter->Update();   
112     return autoCropFilter->GetOutput();
113   }
114   //--------------------------------------------------------------------
115
116
117   //--------------------------------------------------------------------
118   template<class TImageType>
119   typename TImageType::Pointer
120   KeepLabels(const TImageType * input,
121              typename TImageType::PixelType BG, 
122              typename TImageType::PixelType FG,  
123              typename TImageType::PixelType firstKeep, 
124              typename TImageType::PixelType lastKeep, 
125              bool useLastKeep);
126   //--------------------------------------------------------------------
127
128
129   //--------------------------------------------------------------------
130   template<class TImageType>
131   typename TImageType::Pointer
132   LabelizeAndSelectLabels(const TImageType * input,
133                           typename TImageType::PixelType BG, 
134                           typename TImageType::PixelType FG, 
135                           bool isFullyConnected,
136                           int minimalComponentSize,
137                           LabelizeParameters<typename TImageType::PixelType> * param);
138
139   //--------------------------------------------------------------------
140   template<class ImageType>
141   typename ImageType::Pointer
142   ResizeImageLike(const ImageType * input,
143                   const itk::ImageBase<ImageType::ImageDimension> * like, 
144                   typename ImageType::PixelType BG);
145
146
147   //--------------------------------------------------------------------
148   template<class MaskImageType>
149   typename MaskImageType::Pointer
150   SliceBySliceRelativePosition(const MaskImageType * input,
151                                const MaskImageType * object,
152                                int direction, 
153                                double threshold, 
154                                std::string orientation, 
155                                bool uniqueConnectedComponent=false, 
156                                double spacing=-1, 
157                                bool autocropflag=true, 
158                                bool singleObjectCCL=true);
159
160   //--------------------------------------------------------------------
161   // In a binary image, search for the point belonging to the FG that
162   // is the most exterma in the direction 'direction' (or in the
163   // opposite if notFlag is given). 
164   template<class ImageType>
165   bool
166   FindExtremaPointInAGivenDirection(const ImageType * input, 
167                                     typename ImageType::PixelType bg, 
168                                     int direction, bool opposite, 
169                                     typename ImageType::PointType & p);
170
171   //--------------------------------------------------------------------
172
173   //--------------------------------------------------------------------
174   // Same as above but check that the found point is not more than
175   // 'distanceMax' away from 'refPoint'
176   template<class ImageType>
177   bool
178   FindExtremaPointInAGivenDirection(const ImageType * input, 
179                                     typename ImageType::PixelType bg, 
180                                     int direction, bool opposite, 
181                                     typename ImageType::PointType refPoint,
182                                     double distanceMax, 
183                                     typename ImageType::PointType & p);
184
185   //--------------------------------------------------------------------
186
187   //--------------------------------------------------------------------
188   template<class ImageType>
189   typename ImageType::Pointer
190   CropImageAlongOneAxis(const ImageType * image, 
191                         int dim, double min, double max, 
192                         bool autoCrop = false,
193                         typename ImageType::PixelType BG=0);
194   template<class ImageType>
195   typename ImageType::Pointer
196   CropImageRemoveGreaterThan(const ImageType * image, 
197                              int dim, double min, bool autoCrop = false,
198                              typename ImageType::PixelType BG=0);
199   template<class ImageType>
200   typename ImageType::Pointer
201   CropImageRemoveLowerThan(const ImageType * image, 
202                            int dim, double max,bool autoCrop = false,
203                            typename ImageType::PixelType BG=0);
204   //--------------------------------------------------------------------
205
206
207   //--------------------------------------------------------------------
208   template<class ImageType, class LabelType>
209   typename itk::LabelMap< itk::ShapeLabelObject<LabelType, ImageType::ImageDimension> >::Pointer
210   ComputeLabelMap(const ImageType * image, 
211                   typename ImageType::PixelType BG,                   
212                   bool computePerimeterFlag=false);
213   template<class ImageType>
214   void
215   ComputeCentroids(const ImageType * image, 
216                    typename ImageType::PixelType BG, 
217                    std::vector<typename ImageType::PointType> & centroids);
218   template<class ImageType>
219   void
220   ComputeCentroids2(const ImageType * image, 
221                    typename ImageType::PixelType BG, 
222                    std::vector<typename ImageType::PointType> & centroids);
223   //--------------------------------------------------------------------
224
225
226   //--------------------------------------------------------------------
227   template<class ImageType>
228   void
229   ExtractSlices(const ImageType * image, int direction,
230                 std::vector<typename itk::Image<typename ImageType::PixelType,
231                                                 ImageType::ImageDimension-1>::Pointer > & slices)
232   {
233     typedef ExtractSliceFilter<ImageType> ExtractSliceFilterType;
234     typedef typename ExtractSliceFilterType::SliceType SliceType;
235     typename ExtractSliceFilterType::Pointer
236       extractSliceFilter = ExtractSliceFilterType::New();
237     extractSliceFilter->SetInput(image);
238     extractSliceFilter->SetDirection(direction);
239     extractSliceFilter->Update();
240     extractSliceFilter->GetOutputSlices(slices);
241   }
242   //--------------------------------------------------------------------
243
244
245   //--------------------------------------------------------------------
246   template<class ImageType>
247   typename ImageType::Pointer
248   JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType, 
249                                              ImageType::ImageDimension-1>::Pointer > & slices, 
250              const ImageType * input, 
251              int direction) {
252     typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
253     typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
254     typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
255     joinFilter->SetOrigin(input->GetOrigin()[direction]);
256     joinFilter->SetSpacing(input->GetSpacing()[direction]);
257     for(unsigned int i=0; i<slices.size(); i++) {
258       joinFilter->PushBackInput(slices[i]);
259     }
260     joinFilter->Update();
261     return joinFilter->GetOutput();
262   }
263   //--------------------------------------------------------------------
264
265
266   //--------------------------------------------------------------------
267   // Set of tools to manage 3D points and 2D points in slices  
268   template<class ImageType>
269   class PointsUtils
270   {
271     typedef typename ImageType::PointType PointType3D;
272     typedef typename ImageType::IndexType IndexType3D;
273     typedef typename ImageType::PixelType PixelType;
274     typedef typename ImageType::Pointer ImagePointer;
275     typedef typename ImageType::ConstPointer ImageConstPointer;
276     typedef itk::Image<PixelType, 2> SliceType;
277     typedef typename SliceType::PointType PointType2D;
278     typedef typename SliceType::IndexType IndexType2D;
279     
280     typedef std::map<int, PointType2D> MapPoint2DType;
281     typedef std::vector<PointType3D> VectorPoint3DType;
282     typedef std::vector<PointType2D> VectorPoint2DType;
283
284   public:
285     static void Convert2DTo3D(const PointType2D & p2D, 
286                               const ImageType * image, 
287                               const int slice, 
288                               PointType3D & p3D);
289     static void Convert2DMapTo3DList(const MapPoint2DType & map, 
290                                   const ImageType * image, 
291                                   VectorPoint3DType & list);
292     static void Convert2DListTo3DList(const VectorPoint2DType & p, 
293                                       int slice,
294                                       const ImageType * image, 
295                                       VectorPoint3DType & list);
296   };
297
298   //--------------------------------------------------------------------
299   template<class ImageType>
300   void 
301   WriteListOfLandmarks(std::vector<typename ImageType::PointType> points, 
302                        std::string filename);
303   //--------------------------------------------------------------------
304
305
306   //--------------------------------------------------------------------
307   template<class ImageType>
308   typename ImageType::Pointer
309   Dilate(const ImageType * image, double radiusInMM,               
310          typename ImageType::PixelType BG, 
311          typename ImageType::PixelType FG, 
312          bool extendSupport);
313   template<class ImageType>
314   typename ImageType::Pointer
315   Dilate(const ImageType * image, typename ImageType::SizeType radius, 
316          typename ImageType::PixelType BG, 
317          typename ImageType::PixelType FG, 
318          bool extendSupport);
319   template<class ImageType>
320   typename ImageType::Pointer  
321   Dilate(const ImageType * image, typename ImageType::PointType radiusInMM, 
322          typename ImageType::PixelType BG, 
323          typename ImageType::PixelType FG, 
324          bool extendSupport);
325   //--------------------------------------------------------------------
326
327
328   //--------------------------------------------------------------------
329   template<class ImageType>
330   typename ImageType::Pointer 
331   Opening(const ImageType * image, typename ImageType::SizeType radius,
332           typename ImageType::PixelType BG, typename ImageType::PixelType FG);
333   //--------------------------------------------------------------------
334
335
336   //--------------------------------------------------------------------
337   template<class ValueType, class VectorType>
338   void ConvertOption(std::string optionName, uint given, 
339                      ValueType * values, VectorType & p, 
340                      uint dim, bool required);
341 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED)              \
342   ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
343   //--------------------------------------------------------------------
344
345   //--------------------------------------------------------------------
346   template<class ImageType>
347   void 
348   SliceBySliceSetBackgroundFromLineSeparation(ImageType * input, 
349                                               std::vector<typename ImageType::PointType> & lA, 
350                                               std::vector<typename ImageType::PointType> & lB, 
351                                               typename ImageType::PixelType BG, 
352                                               int mainDirection, 
353                                               double offsetToKeep);
354   //--------------------------------------------------------------------
355
356
357   //--------------------------------------------------------------------
358   template<class ImageType>
359   void AndNot(ImageType * input, 
360               const ImageType * object, 
361               typename ImageType::PixelType BG=0);
362   //--------------------------------------------------------------------
363  
364
365   //--------------------------------------------------------------------
366   template<class ImageType>
367   typename ImageType::Pointer
368   Binarize(const ImageType * input, 
369            typename ImageType::PixelType lower, 
370            typename ImageType::PixelType upper, 
371            typename ImageType::PixelType BG=0,
372            typename ImageType::PixelType FG=1);
373   //--------------------------------------------------------------------
374  
375   
376   //--------------------------------------------------------------------
377   template<class ImageType>
378   void
379   GetMinMaxPointPosition(const ImageType * input, 
380                          typename ImageType::PointType & min,
381                          typename ImageType::PointType & max);
382   //--------------------------------------------------------------------
383
384   //--------------------------------------------------------------------
385   template<class ImageType>
386   typename ImageType::PointType
387   FindExtremaPointInAGivenLine(const ImageType * input, 
388                                int dimension, bool inverse, 
389                                typename ImageType::PointType p, 
390                                typename ImageType::PixelType BG, 
391                                double distanceMax);
392   //--------------------------------------------------------------------
393
394   
395   //--------------------------------------------------------------------
396   template<class PointType>
397   bool
398   IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
399   //--------------------------------------------------------------------
400
401
402   //--------------------------------------------------------------------
403   template<class ImageType>
404   void 
405   SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input, 
406                                                          typename ImageType::PixelType BG, 
407                                                          int sliceDimension, 
408                                                          int extremaDirection, 
409                                                          bool extremaOppositeFlag, 
410                                                          int lineDirection,
411                                                          double margin,
412                                                          std::vector<typename ImageType::PointType> & A, 
413                                                          std::vector<typename ImageType::PointType> & B);  
414   //--------------------------------------------------------------------
415
416
417   //--------------------------------------------------------------------
418   template<class ImageType>
419   typename ImageType::Pointer
420   SliceBySliceKeepMainCCL(const ImageType * input, 
421                           typename ImageType::PixelType BG,
422                           typename ImageType::PixelType FG);
423   //--------------------------------------------------------------------
424   
425
426   //--------------------------------------------------------------------
427   template<class ImageType>
428   typename ImageType::Pointer
429   Clone(const ImageType * input);
430   //--------------------------------------------------------------------
431   
432
433 }
434
435 #include "clitkSegmentationUtils.txx"
436
437 #endif