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