]> Creatis software - clitk.git/blob - itk/clitkSegmentationUtils.h
77270d1a542b4d2c1bb7bc3346bcb91967b49f2c
[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 namespace clitk {
31
32   //--------------------------------------------------------------------
33   template<class ImageType>
34   void ComputeBBFromImageRegion(typename ImageType::Pointer image, 
35                                 typename ImageType::RegionType region,
36                                 typename itk::BoundingBox<unsigned long, 
37                                                           ImageType::ImageDimension>::Pointer bb);
38   
39   //--------------------------------------------------------------------
40   template<int Dimension>
41   void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
42                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
43                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2);
44
45   //--------------------------------------------------------------------
46   template<class ImageType>
47   void ComputeRegionFromBB(typename ImageType::Pointer image, 
48                            const typename itk::BoundingBox<unsigned long, 
49                            ImageType::ImageDimension>::Pointer bb, 
50                            typename ImageType::RegionType & region);
51   //--------------------------------------------------------------------
52   template<class TInternalImageType, class TMaskInternalImageType>
53   typename TInternalImageType::Pointer
54   SetBackground(const TInternalImageType * input,
55                 const TMaskInternalImageType * mask, 
56                 typename TMaskInternalImageType::PixelType maskBG, 
57                 typename TInternalImageType::PixelType outValue, 
58                 bool inPlace);
59   //--------------------------------------------------------------------
60
61
62   //--------------------------------------------------------------------
63   template<class ImageType>
64   int GetNumberOfConnectedComponentLabels(typename ImageType::Pointer input, 
65                                           typename ImageType::PixelType BG, 
66                                           bool isFullyConnected);
67   //--------------------------------------------------------------------
68
69
70   //-------------------------------------------------------------------- 
71   template<class TImageType>
72   typename TImageType::Pointer
73   Labelize(const TImageType * input, 
74            typename TImageType::PixelType BG, 
75            bool isFullyConnected, 
76            int minimalComponentSize);
77   //--------------------------------------------------------------------
78
79
80   //--------------------------------------------------------------------
81   template<class ImageType>
82   typename ImageType::Pointer
83   RemoveLabels(typename ImageType::Pointer input, 
84                typename ImageType::PixelType BG, 
85                std::vector<typename ImageType::PixelType> & labelsToRemove);
86   //--------------------------------------------------------------------
87
88
89   //--------------------------------------------------------------------
90   template<class ImageType>
91   typename ImageType::Pointer
92   AutoCrop(typename ImageType::Pointer input, 
93            typename ImageType::PixelType BG) {
94     typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
95     typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
96     autoCropFilter->SetInput(input);
97     autoCropFilter->SetBackgroundValue(BG);
98     autoCropFilter->Update();   
99     return autoCropFilter->GetOutput();
100   }
101   //--------------------------------------------------------------------
102
103
104   //--------------------------------------------------------------------
105   template<class TImageType>
106   typename TImageType::Pointer
107   KeepLabels(const TImageType * input,
108              typename TImageType::PixelType BG, 
109              typename TImageType::PixelType FG,  
110              typename TImageType::PixelType firstKeep, 
111              typename TImageType::PixelType lastKeep, 
112              bool useLastKeep);
113   //--------------------------------------------------------------------
114
115
116   //--------------------------------------------------------------------
117   template<class TImageType>
118   typename TImageType::Pointer
119   LabelizeAndSelectLabels(typename TImageType::Pointer input,
120                           typename TImageType::PixelType BG, 
121                           typename TImageType::PixelType FG, 
122                           bool isFullyConnected,
123                           int minimalComponentSize,
124                           LabelizeParameters<typename TImageType::PixelType> * param);
125
126   //--------------------------------------------------------------------
127   template<class ImageType>
128   typename ImageType::Pointer
129   ResizeImageLike(typename ImageType::Pointer input,
130                   typename ImageType::Pointer like, 
131                   typename ImageType::PixelType BG);
132
133
134   //--------------------------------------------------------------------
135   template<class MaskImageType>
136   typename MaskImageType::Pointer
137   SliceBySliceRelativePosition(const MaskImageType * input,
138                                const MaskImageType * object,
139                                int direction, 
140                                double threshold, 
141                                std::string orientation, 
142                                bool uniqueConnectedComponent=false, 
143                                double spacing=-1, 
144                                bool notflag=false);
145
146   //--------------------------------------------------------------------
147   // In a binary image, search for the point belonging to the FG that
148   // is the most exterma in the direction 'direction' (or in the
149   // opposite if notFlag is given). 
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 & p);
156
157   //--------------------------------------------------------------------
158
159   //--------------------------------------------------------------------
160   // Same as above but check that the found point is not more than
161   // 'distanceMax' away from 'refPoint'
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 refPoint,
168                                     double distanceMax, 
169                                     typename ImageType::PointType & p);
170
171   //--------------------------------------------------------------------
172
173   //--------------------------------------------------------------------
174   template<class ImageType>
175   typename ImageType::Pointer
176   CropImageAlongOneAxis(typename ImageType::Pointer image, 
177                         int dim, double min, double max, 
178                         bool autoCrop = false,
179                         typename ImageType::PixelType BG=0);
180   template<class ImageType>
181   typename ImageType::Pointer
182   CropImageAbove(typename ImageType::Pointer image, 
183                  int dim, double min, 
184                  bool autoCrop = false,
185                  typename ImageType::PixelType BG=0);
186   template<class ImageType>
187   typename ImageType::Pointer
188   CropImageBelow(typename ImageType::Pointer image, 
189                  int dim, double max,
190                  bool autoCrop = false,
191                  typename ImageType::PixelType BG=0);
192   //--------------------------------------------------------------------
193
194
195   //--------------------------------------------------------------------
196   template<class ImageType>
197   void
198   ComputeCentroids(typename ImageType::Pointer image, 
199                    typename ImageType::PixelType BG, 
200                    std::vector<typename ImageType::PointType> & centroids);
201   //--------------------------------------------------------------------
202
203
204   //--------------------------------------------------------------------
205   template<class ImageType>
206   void
207   ExtractSlices(typename ImageType::Pointer image, 
208                 int dim, 
209                 std::vector< typename itk::Image<typename ImageType::PixelType, 
210                 ImageType::ImageDimension-1>::Pointer > & slices);
211   //--------------------------------------------------------------------
212
213
214   //--------------------------------------------------------------------
215   template<class ImageType>
216   typename ImageType::Pointer
217   JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType, 
218              ImageType::ImageDimension-1>::Pointer > & slices, 
219              typename ImageType::Pointer input, 
220              int dim);
221   //--------------------------------------------------------------------
222
223
224   //--------------------------------------------------------------------
225   // Set of tools to manage 3D points and 2D points in slices  
226   template<class ImageType>
227   class PointsUtils
228   {
229     typedef typename ImageType::PointType PointType3D;
230     typedef typename ImageType::PixelType PixelType;
231     typedef typename ImageType::Pointer ImagePointer;
232     typedef typename ImageType::ConstPointer ImageConstPointer;
233     typedef itk::Image<PixelType, 2> SliceType;
234     typedef typename SliceType::PointType PointType2D;
235     
236     typedef std::map<int, PointType2D> MapPoint2DType;
237     typedef std::vector<PointType3D> VectorPoint3DType;
238   public:
239     static void Convert2DTo3D(const PointType2D & p2D, 
240                               ImagePointer image, 
241                               const int slice, 
242                               PointType3D & p3D);
243     static void Convert2DTo3DList(const MapPoint2DType & map, 
244                                   ImagePointer image, 
245                                   VectorPoint3DType & list);
246   };
247
248   //--------------------------------------------------------------------
249   template<class ImageType>
250   void 
251   WriteListOfLandmarks(std::vector<typename ImageType::PointType> points, 
252                        std::string filename);
253   //--------------------------------------------------------------------
254
255
256   //--------------------------------------------------------------------
257   template<class ImageType>
258   typename ImageType::Pointer
259   Dilate(typename ImageType::Pointer image, 
260               double radiusInMM,               
261               typename ImageType::PixelType BG, 
262               typename ImageType::PixelType FG, 
263               bool extendSupport);
264   template<class ImageType>
265   typename ImageType::Pointer
266   Dilate(typename ImageType::Pointer image, 
267               typename ImageType::SizeType radius, 
268               typename ImageType::PixelType BG, 
269               typename ImageType::PixelType FG, 
270               bool extendSupport);
271   template<class ImageType>
272   typename ImageType::Pointer  
273   Dilate(typename ImageType::Pointer image, 
274               typename ImageType::PointType radiusInMM, 
275               typename ImageType::PixelType BG, 
276               typename ImageType::PixelType FG, 
277               bool extendSupport);
278   //--------------------------------------------------------------------
279
280   //--------------------------------------------------------------------
281   template<class ValueType, class VectorType>
282   void ConvertOption(std::string optionName, uint given, 
283                      ValueType * values, VectorType & p, 
284                      uint dim, bool required);
285 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED)         \
286   ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
287
288 }
289
290 #include "clitkSegmentationUtils.txx"
291
292 #endif