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