1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #ifndef CLITKSEGMENTATIONUTILS_H
20 #define CLITKSEGMENTATIONUTILS_H
23 #include "clitkCommon.h"
24 #include "clitkAutoCropFilter.h"
25 #include "clitkLabelizeParameters.h"
28 #include <itkBoundingBox.h>
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.
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);
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);
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,
65 //--------------------------------------------------------------------
68 //--------------------------------------------------------------------
69 template<class ImageType>
70 int GetNumberOfConnectedComponentLabels(const ImageType * input,
71 typename ImageType::PixelType BG,
72 bool isFullyConnected);
73 //--------------------------------------------------------------------
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,
88 //--------------------------------------------------------------------
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 //--------------------------------------------------------------------
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();
112 //--------------------------------------------------------------------
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,
124 //--------------------------------------------------------------------
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);
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);
145 //--------------------------------------------------------------------
146 template<class MaskImageType>
147 typename MaskImageType::Pointer
148 SliceBySliceRelativePosition(const MaskImageType * input,
149 const MaskImageType * object,
152 std::string orientation,
153 bool uniqueConnectedComponent=false,
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>
163 FindExtremaPointInAGivenDirection(const ImageType * input,
164 typename ImageType::PixelType bg,
165 int direction, bool opposite,
166 typename ImageType::PointType & p);
168 //--------------------------------------------------------------------
170 //--------------------------------------------------------------------
171 // Same as above but check that the found point is not more than
172 // 'distanceMax' away from 'refPoint'
173 template<class ImageType>
175 FindExtremaPointInAGivenDirection(const ImageType * input,
176 typename ImageType::PixelType bg,
177 int direction, bool opposite,
178 typename ImageType::PointType refPoint,
180 typename ImageType::PointType & p);
182 //--------------------------------------------------------------------
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 //--------------------------------------------------------------------
204 //--------------------------------------------------------------------
205 template<class ImageType>
207 ComputeCentroids(const ImageType * image,
208 typename ImageType::PixelType BG,
209 std::vector<typename ImageType::PointType> & centroids);
210 //--------------------------------------------------------------------
213 //--------------------------------------------------------------------
214 template<class ImageType>
216 ExtractSlices(const ImageType * image, int dim,
217 std::vector< typename itk::Image<typename ImageType::PixelType,
218 ImageType::ImageDimension-1>::Pointer > & slices);
219 //--------------------------------------------------------------------
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 //--------------------------------------------------------------------
231 //--------------------------------------------------------------------
232 // Set of tools to manage 3D points and 2D points in slices
233 template<class ImageType>
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;
245 typedef std::map<int, PointType2D> MapPoint2DType;
246 typedef std::vector<PointType3D> VectorPoint3DType;
248 static void Convert2DTo3D(const PointType2D & p2D,
249 const ImageType * image,
252 static void Convert2DTo3DList(const MapPoint2DType & map,
253 const ImageType * image,
254 VectorPoint3DType & list);
257 //--------------------------------------------------------------------
258 template<class ImageType>
260 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
261 std::string filename);
262 //--------------------------------------------------------------------
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,
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,
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,
284 //--------------------------------------------------------------------
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 //--------------------------------------------------------------------
295 //--------------------------------------------------------------------
296 template<class ImageType>
298 SliceBySliceSetBackgroundFromLineSeparation(ImageType * input,
299 std::vector<typename ImageType::PointType> & lA,
300 std::vector<typename ImageType::PointType> & lB,
301 typename ImageType::PixelType BG,
303 double offsetToKeep);
304 //--------------------------------------------------------------------
307 //--------------------------------------------------------------------
308 template<class ImageType>
309 void AndNot(ImageType * input,
310 const ImageType * object,
311 typename ImageType::PixelType BG=0);
312 //--------------------------------------------------------------------
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 //--------------------------------------------------------------------
326 //--------------------------------------------------------------------
327 template<class ImageType>
329 GetMinMaxPointPosition(const ImageType * input,
330 typename ImageType::PointType & min,
331 typename ImageType::PointType & max);
332 //--------------------------------------------------------------------
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,
342 //--------------------------------------------------------------------
345 //--------------------------------------------------------------------
346 template<class PointType>
348 IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
349 //--------------------------------------------------------------------
353 #include "clitkSegmentationUtils.txx"