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