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