]> Creatis software - clitk.git/blob - itk/clitkSegmentationUtils.h
replace SmartPointer with raw pointer for function (recommended)
[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 notflag=false);
156
157   //--------------------------------------------------------------------
158   // In a binary image, search for the point belonging to the FG that
159   // is the most exterma in the direction 'direction' (or in the
160   // opposite if notFlag is given). 
161   template<class ImageType>
162   bool
163   FindExtremaPointInAGivenDirection(const ImageType * input, 
164                                     typename ImageType::PixelType bg, 
165                                     int direction, bool opposite, 
166                                     typename ImageType::PointType & p);
167
168   //--------------------------------------------------------------------
169
170   //--------------------------------------------------------------------
171   // Same as above but check that the found point is not more than
172   // 'distanceMax' away from 'refPoint'
173   template<class ImageType>
174   bool
175   FindExtremaPointInAGivenDirection(const ImageType * input, 
176                                     typename ImageType::PixelType bg, 
177                                     int direction, bool opposite, 
178                                     typename ImageType::PointType refPoint,
179                                     double distanceMax, 
180                                     typename ImageType::PointType & p);
181
182   //--------------------------------------------------------------------
183
184   //--------------------------------------------------------------------
185   template<class ImageType>
186   typename ImageType::Pointer
187   CropImageAlongOneAxis(const ImageType * image, 
188                         int dim, double min, double max, 
189                         bool autoCrop = false,
190                         typename ImageType::PixelType BG=0);
191   template<class ImageType>
192   typename ImageType::Pointer
193   CropImageAbove(const ImageType * image, 
194                  int dim, double min, bool autoCrop = false,
195                  typename ImageType::PixelType BG=0);
196   template<class ImageType>
197   typename ImageType::Pointer
198   CropImageBelow(const ImageType * image, 
199                  int dim, double max,bool autoCrop = false,
200                  typename ImageType::PixelType BG=0);
201   //--------------------------------------------------------------------
202
203
204   //--------------------------------------------------------------------
205   template<class ImageType>
206   void
207   ComputeCentroids(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 dim, 
217                 std::vector< typename itk::Image<typename ImageType::PixelType, 
218                                                  ImageType::ImageDimension-1>::Pointer > & slices);
219   //--------------------------------------------------------------------
220
221
222   //--------------------------------------------------------------------
223   template<class ImageType>
224   typename ImageType::Pointer
225   JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType, 
226                                              ImageType::ImageDimension-1>::Pointer > & slices, 
227              const ImageType * input, int dim);
228   //--------------------------------------------------------------------
229
230
231   //--------------------------------------------------------------------
232   // Set of tools to manage 3D points and 2D points in slices  
233   template<class ImageType>
234   class PointsUtils
235   {
236     typedef typename ImageType::PointType PointType3D;
237     typedef typename ImageType::IndexType IndexType3D;
238     typedef typename ImageType::PixelType PixelType;
239     typedef typename ImageType::Pointer ImagePointer;
240     typedef typename ImageType::ConstPointer ImageConstPointer;
241     typedef itk::Image<PixelType, 2> SliceType;
242     typedef typename SliceType::PointType PointType2D;
243     typedef typename SliceType::IndexType IndexType2D;
244     
245     typedef std::map<int, PointType2D> MapPoint2DType;
246     typedef std::vector<PointType3D> VectorPoint3DType;
247   public:
248     static void Convert2DTo3D(const PointType2D & p2D, 
249                               const ImageType * image, 
250                               const int slice, 
251                               PointType3D & p3D);
252     static void Convert2DTo3DList(const MapPoint2DType & map, 
253                                   const ImageType * image, 
254                                   VectorPoint3DType & list);
255   };
256
257   //--------------------------------------------------------------------
258   template<class ImageType>
259   void 
260   WriteListOfLandmarks(std::vector<typename ImageType::PointType> points, 
261                        std::string filename);
262   //--------------------------------------------------------------------
263
264
265   //--------------------------------------------------------------------
266   template<class ImageType>
267   typename ImageType::Pointer
268   Dilate(const ImageType * image, double radiusInMM,               
269          typename ImageType::PixelType BG, 
270          typename ImageType::PixelType FG, 
271          bool extendSupport);
272   template<class ImageType>
273   typename ImageType::Pointer
274   Dilate(const ImageType * image, typename ImageType::SizeType radius, 
275          typename ImageType::PixelType BG, 
276          typename ImageType::PixelType FG, 
277          bool extendSupport);
278   template<class ImageType>
279   typename ImageType::Pointer  
280   Dilate(const ImageType * image, typename ImageType::PointType radiusInMM, 
281          typename ImageType::PixelType BG, 
282          typename ImageType::PixelType FG, 
283          bool extendSupport);
284   //--------------------------------------------------------------------
285
286   //--------------------------------------------------------------------
287   template<class ValueType, class VectorType>
288   void ConvertOption(std::string optionName, uint given, 
289                      ValueType * values, VectorType & p, 
290                      uint dim, bool required);
291 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED)              \
292   ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
293   //--------------------------------------------------------------------
294
295   //--------------------------------------------------------------------
296   template<class ImageType>
297   void 
298   SliceBySliceSetBackgroundFromLineSeparation(ImageType * input, 
299                                               std::vector<typename ImageType::PointType> & lA, 
300                                               std::vector<typename ImageType::PointType> & lB, 
301                                               typename ImageType::PixelType BG, 
302                                               int mainDirection, 
303                                               double offsetToKeep);
304   //--------------------------------------------------------------------
305
306
307   //--------------------------------------------------------------------
308   template<class ImageType>
309   void AndNot(ImageType * input, 
310               const ImageType * object, 
311               typename ImageType::PixelType BG=0);
312   //--------------------------------------------------------------------
313  
314
315   //--------------------------------------------------------------------
316   template<class ImageType>
317   typename ImageType::Pointer
318   Binarize(const ImageType * input, 
319            typename ImageType::PixelType lower, 
320            typename ImageType::PixelType upper, 
321            typename ImageType::PixelType BG=0,
322            typename ImageType::PixelType FG=1);
323   //--------------------------------------------------------------------
324  
325   
326   //--------------------------------------------------------------------
327   template<class ImageType>
328   void
329   GetMinMaxPointPosition(const ImageType * input, 
330                          typename ImageType::PointType & min,
331                          typename ImageType::PointType & max);
332   //--------------------------------------------------------------------
333
334   //--------------------------------------------------------------------
335   template<class ImageType>
336   typename ImageType::PointType
337   FindExtremaPointInAGivenLine(const ImageType * input, 
338                                int dimension, bool inverse, 
339                                typename ImageType::PointType p, 
340                                typename ImageType::PixelType BG, 
341                                double distanceMax);
342   //--------------------------------------------------------------------
343
344   
345   //--------------------------------------------------------------------
346   template<class PointType>
347   bool
348   IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
349   //--------------------------------------------------------------------
350
351 }
352
353 #include "clitkSegmentationUtils.txx"
354
355 #endif