]> Creatis software - clitk.git/blob - itk/clitkSegmentationUtils.h
Remove ResizeImageLike (put in CropLikeImageFilter) and BoundingBox (put in clitkBoun...
[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
134   //--------------------------------------------------------------------
135   // In a binary image, search for the point belonging to the FG that
136   // is the most exterma in the direction 'direction' (or in the
137   // opposite if notFlag is given). 
138   template<class ImageType>
139   bool
140   FindExtremaPointInAGivenDirection(const ImageType * input, 
141                                     typename ImageType::PixelType bg, 
142                                     int direction, bool opposite, 
143                                     typename ImageType::PointType & p);
144
145   //--------------------------------------------------------------------
146
147   //--------------------------------------------------------------------
148   // Same as above but check that the found point is not more than
149   // 'distanceMax' away from 'refPoint'
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 refPoint,
156                                     double distanceMax, 
157                                     typename ImageType::PointType & p);
158
159   //--------------------------------------------------------------------
160
161   //--------------------------------------------------------------------
162   template<class ImageType>
163   typename ImageType::Pointer
164   CropImageAlongOneAxis(const ImageType * image, 
165                         int dim, double min, double max, 
166                         bool autoCrop = false,
167                         typename ImageType::PixelType BG=0);
168   template<class ImageType>
169   typename ImageType::Pointer
170   CropImageRemoveGreaterThan(const ImageType * image, 
171                              int dim, double min, bool autoCrop = false,
172                              typename ImageType::PixelType BG=0);
173   template<class ImageType>
174   typename ImageType::Pointer
175   CropImageRemoveLowerThan(const ImageType * image, 
176                            int dim, double max,bool autoCrop = false,
177                            typename ImageType::PixelType BG=0);
178   //--------------------------------------------------------------------
179
180
181   //--------------------------------------------------------------------
182   template<class ImageType, class LabelType>
183   typename itk::LabelMap< itk::ShapeLabelObject<LabelType, ImageType::ImageDimension> >::Pointer
184   ComputeLabelMap(const ImageType * image, 
185                   typename ImageType::PixelType BG,                   
186                   bool computePerimeterFlag=false);
187   template<class ImageType>
188   void
189   ComputeCentroids(const ImageType * image, 
190                    typename ImageType::PixelType BG, 
191                    std::vector<typename ImageType::PointType> & centroids);
192   template<class ImageType>
193   void
194   ComputeCentroids2(const ImageType * image, 
195                    typename ImageType::PixelType BG, 
196                    std::vector<typename ImageType::PointType> & centroids);
197   //--------------------------------------------------------------------
198
199
200   //--------------------------------------------------------------------
201   template<class ImageType>
202   void
203   ExtractSlices(const ImageType * image, int direction,
204                 std::vector<typename itk::Image<typename ImageType::PixelType,
205                                                 ImageType::ImageDimension-1>::Pointer > & slices)
206   {
207     typedef ExtractSliceFilter<ImageType> ExtractSliceFilterType;
208     typedef typename ExtractSliceFilterType::SliceType SliceType;
209     typename ExtractSliceFilterType::Pointer
210       extractSliceFilter = ExtractSliceFilterType::New();
211     extractSliceFilter->SetInput(image);
212     extractSliceFilter->SetDirection(direction);
213     extractSliceFilter->Update();
214     extractSliceFilter->GetOutputSlices(slices);
215   }
216   //--------------------------------------------------------------------
217
218
219   //--------------------------------------------------------------------
220   template<class ImageType>
221   typename ImageType::Pointer
222   JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType, 
223                                              ImageType::ImageDimension-1>::Pointer > & slices, 
224              const itk::ImageBase<ImageType::ImageDimension> * input, //const ImageType * input, 
225              int direction) {
226     typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
227     typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
228     typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
229     joinFilter->SetOrigin(input->GetOrigin()[direction]);
230     joinFilter->SetSpacing(input->GetSpacing()[direction]);
231     for(unsigned int i=0; i<slices.size(); i++) {
232       joinFilter->PushBackInput(slices[i]);
233     }
234     joinFilter->Update();
235     return joinFilter->GetOutput();
236   }
237   //--------------------------------------------------------------------
238
239
240   //--------------------------------------------------------------------
241   // Set of tools to manage 3D points and 2D points in slices  
242   template<class ImageType>
243   class PointsUtils
244   {
245     typedef typename ImageType::PointType PointType3D;
246     typedef typename ImageType::IndexType IndexType3D;
247     typedef typename ImageType::PixelType PixelType;
248     typedef typename ImageType::Pointer ImagePointer;
249     typedef typename ImageType::ConstPointer ImageConstPointer;
250     typedef itk::Image<PixelType, 2> SliceType;
251     typedef typename SliceType::PointType PointType2D;
252     typedef typename SliceType::IndexType IndexType2D;
253     
254     typedef std::map<int, PointType2D> MapPoint2DType;
255     typedef std::vector<PointType3D> VectorPoint3DType;
256     typedef std::vector<PointType2D> VectorPoint2DType;
257
258   public:
259     static void Convert2DTo3D(const PointType2D & p2D, 
260                               const ImageType * image, 
261                               const int slice, 
262                               PointType3D & p3D);
263     static void Convert2DMapTo3DList(const MapPoint2DType & map, 
264                                   const ImageType * image, 
265                                   VectorPoint3DType & list);
266     static void Convert2DListTo3DList(const VectorPoint2DType & p, 
267                                       int slice,
268                                       const ImageType * image, 
269                                       VectorPoint3DType & list);
270   };
271
272   //--------------------------------------------------------------------
273   template<class ImageType>
274   void 
275   WriteListOfLandmarks(std::vector<typename ImageType::PointType> points, 
276                        std::string filename);
277   //--------------------------------------------------------------------
278
279
280   //--------------------------------------------------------------------
281   template<class ImageType>
282   typename ImageType::Pointer
283   Dilate(const ImageType * image, double radiusInMM,               
284          typename ImageType::PixelType BG, 
285          typename ImageType::PixelType FG, 
286          bool extendSupport);
287   template<class ImageType>
288   typename ImageType::Pointer
289   Dilate(const ImageType * image, typename ImageType::SizeType radius, 
290          typename ImageType::PixelType BG, 
291          typename ImageType::PixelType FG, 
292          bool extendSupport);
293   template<class ImageType>
294   typename ImageType::Pointer  
295   Dilate(const ImageType * image, typename ImageType::PointType radiusInMM, 
296          typename ImageType::PixelType BG, 
297          typename ImageType::PixelType FG, 
298          bool extendSupport);
299   //--------------------------------------------------------------------
300
301
302   //--------------------------------------------------------------------
303   template<class ImageType>
304   typename ImageType::Pointer 
305   Opening(const ImageType * image, typename ImageType::SizeType radius,
306           typename ImageType::PixelType BG, typename ImageType::PixelType FG);
307   //--------------------------------------------------------------------
308
309
310   //--------------------------------------------------------------------
311   template<class ValueType, class VectorType>
312   void ConvertOption(std::string optionName, uint given, 
313                      ValueType * values, VectorType & p, 
314                      uint dim, bool required);
315 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED)              \
316   ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
317   //--------------------------------------------------------------------
318
319   //--------------------------------------------------------------------
320   template<class ImageType>
321   void 
322   SliceBySliceSetBackgroundFromLineSeparation(ImageType * input, 
323                                               std::vector<typename ImageType::PointType> & lA, 
324                                               std::vector<typename ImageType::PointType> & lB, 
325                                               typename ImageType::PixelType BG, 
326                                               int mainDirection, 
327                                               double offsetToKeep);
328   //--------------------------------------------------------------------
329
330
331   //--------------------------------------------------------------------
332   template<class ImageType>
333   void AndNot(ImageType * input, 
334               const ImageType * object, 
335               typename ImageType::PixelType BG=0);
336   template<class ImageType>
337   void And(ImageType * input, 
338            const ImageType * object, 
339            typename ImageType::PixelType BG=0);
340   template<class ImageType>
341   void Or(ImageType * input, 
342           const ImageType * object, 
343           typename ImageType::PixelType BG=0);
344   //--------------------------------------------------------------------
345  
346
347   //--------------------------------------------------------------------
348   template<class ImageType>
349   typename ImageType::Pointer
350   Binarize(const ImageType * input, 
351            typename ImageType::PixelType lower, 
352            typename ImageType::PixelType upper, 
353            typename ImageType::PixelType BG=0,
354            typename ImageType::PixelType FG=1);
355   //--------------------------------------------------------------------
356  
357   
358   //--------------------------------------------------------------------
359   template<class ImageType>
360   void
361   GetMinMaxPointPosition(const ImageType * input, 
362                          typename ImageType::PointType & min,
363                          typename ImageType::PointType & max);
364   //--------------------------------------------------------------------
365
366   //--------------------------------------------------------------------
367   template<class ImageType>
368   typename ImageType::PointType
369   FindExtremaPointInAGivenLine(const ImageType * input, 
370                                int dimension, bool inverse, 
371                                typename ImageType::PointType p, 
372                                typename ImageType::PixelType BG, 
373                                double distanceMax);
374   //--------------------------------------------------------------------
375
376   
377   //--------------------------------------------------------------------
378   template<class PointType>
379   bool
380   IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
381   //--------------------------------------------------------------------
382
383
384   //--------------------------------------------------------------------
385   template<class ImageType>
386   void 
387   SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input, 
388                                                          typename ImageType::PixelType BG, 
389                                                          int sliceDimension, 
390                                                          int extremaDirection, 
391                                                          bool extremaOppositeFlag, 
392                                                          int lineDirection,
393                                                          double margin,
394                                                          std::vector<typename ImageType::PointType> & A, 
395                                                          std::vector<typename ImageType::PointType> & B);  
396   template<class ImageType>
397   void 
398   SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1, 
399                                                                           const ImageType * S2, 
400                                                                           typename ImageType::PixelType BG, 
401                                                                           int sliceDimension, 
402                                                                           std::vector<typename ImageType::PointType> & A, 
403                                                                           std::vector<typename ImageType::PointType> & B);  
404   //--------------------------------------------------------------------
405
406
407   //--------------------------------------------------------------------
408   template<class ImageType>
409   typename ImageType::Pointer
410   SliceBySliceKeepMainCCL(const ImageType * input, 
411                           typename ImageType::PixelType BG,
412                           typename ImageType::PixelType FG);
413   //--------------------------------------------------------------------
414   
415
416   //--------------------------------------------------------------------
417   template<class ImageType>
418   typename ImageType::Pointer
419   Clone(const ImageType * input);
420   //--------------------------------------------------------------------
421   
422
423   //--------------------------------------------------------------------
424   template<class ImageType>
425   typename ImageType::Pointer
426   SliceBySliceSetBackgroundFromSingleLine(const ImageType * input, 
427                                           typename ImageType::PixelType BG, 
428                                           typename ImageType::PointType & A, 
429                                           typename ImageType::PointType & B, 
430                                           int dim1, int dim2, bool removeLowerPartFlag);
431   //--------------------------------------------------------------------
432   
433
434   //--------------------------------------------------------------------
435   template<class ImageType>
436   typename ImageType::Pointer
437   SliceBySliceSetBackgroundFromPoints(const ImageType * input, 
438                                       typename ImageType::PixelType BG, 
439                                       int sliceDim,
440                                       std::vector<typename ImageType::PointType> & A, 
441                                       bool removeGreaterThanXFlag,
442                                       bool removeGreaterThanYFlag);
443   //--------------------------------------------------------------------
444
445
446   //--------------------------------------------------------------------
447   template<class ImageType>
448   void
449   FillRegionWithValue(ImageType * input, typename ImageType::PixelType value, 
450                       typename ImageType::RegionType & region);
451   //--------------------------------------------------------------------
452
453
454   //--------------------------------------------------------------------
455   template<class ImageType>
456   void
457   GetMinMaxBoundary(ImageType * input, typename ImageType::PointType & min, 
458                     typename ImageType::PointType & max);
459   //--------------------------------------------------------------------
460
461
462   //--------------------------------------------------------------------
463   template<class ImageType>
464   typename itk::Image<float, ImageType::ImageDimension>::Pointer//void
465   DistanceMap(const ImageType * input, typename ImageType::PixelType BG);//, 
466   //--------------------------------------------------------------------
467
468
469   //--------------------------------------------------------------------
470   template<class ImageType>
471   typename ImageType::PointType
472   ComputeClosestPoint(const ImageType * input, 
473                       const itk::Image<float, ImageType::ImageDimension> * dmap, 
474                       typename ImageType::PixelType & BG);
475   //--------------------------------------------------------------------
476   
477
478
479 } // end clitk namespace
480
481 #include "clitkSegmentationUtils.txx"
482
483 #endif