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"
26 #include "clitkExtractSliceFilter.h"
29 #include <itkBoundingBox.h>
30 #include <itkJoinSeriesImageFilter.h>
34 http://answerpot.com/showthread.php?357451-Itk::SmartPointer%20-%20problem%20making%20code%20const-correct
35 it is better to take raw pointer as argument instead of SmartPointer.
40 //--------------------------------------------------------------------
41 template<class ImageType>
42 void ComputeBBFromImageRegion(const ImageType * image,
43 typename ImageType::RegionType region,
44 typename itk::BoundingBox<unsigned long,
45 ImageType::ImageDimension>::Pointer bb);
47 //--------------------------------------------------------------------
48 template<int Dimension>
49 void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo,
50 typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1,
51 typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2);
53 //--------------------------------------------------------------------
54 template<class ImageType>
55 void ComputeRegionFromBB(const ImageType * image,
56 const typename itk::BoundingBox<unsigned long,
57 ImageType::ImageDimension>::Pointer bb,
58 typename ImageType::RegionType & region);
59 //--------------------------------------------------------------------
60 template<class TInternalImageType, class TMaskInternalImageType>
61 typename TInternalImageType::Pointer
62 SetBackground(const TInternalImageType * input,
63 const TMaskInternalImageType * mask,
64 typename TMaskInternalImageType::PixelType maskBG,
65 typename TInternalImageType::PixelType outValue,
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 template<class ImageType>
72 int GetNumberOfConnectedComponentLabels(const ImageType * input,
73 typename ImageType::PixelType BG,
74 bool isFullyConnected);
75 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
79 template<class TImageType>
80 typename TImageType::Pointer
81 Labelize(const TImageType * input, typename TImageType::PixelType BG,
82 bool isFullyConnected, int minimalComponentSize);
83 template<class TImageType>
84 typename TImageType::Pointer
85 LabelizeAndCountNumberOfObjects(const TImageType * input,
86 typename TImageType::PixelType BG,
87 bool isFullyConnected,
88 int minimalComponentSize,
90 //--------------------------------------------------------------------
93 //--------------------------------------------------------------------
94 template<class ImageType>
95 typename ImageType::Pointer
96 RemoveLabels(const ImageType * input,
97 typename ImageType::PixelType BG,
98 std::vector<typename ImageType::PixelType> & labelsToRemove);
99 //--------------------------------------------------------------------
102 //--------------------------------------------------------------------
103 template<class ImageType>
104 typename ImageType::Pointer
105 AutoCrop(const ImageType * input,
106 typename ImageType::PixelType BG) {
107 typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
108 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
109 autoCropFilter->SetInput(input);
110 autoCropFilter->SetBackgroundValue(BG);
111 autoCropFilter->Update();
112 return autoCropFilter->GetOutput();
114 //--------------------------------------------------------------------
117 //--------------------------------------------------------------------
118 template<class TImageType>
119 typename TImageType::Pointer
120 KeepLabels(const TImageType * input,
121 typename TImageType::PixelType BG,
122 typename TImageType::PixelType FG,
123 typename TImageType::PixelType firstKeep,
124 typename TImageType::PixelType lastKeep,
126 //--------------------------------------------------------------------
129 //--------------------------------------------------------------------
130 template<class TImageType>
131 typename TImageType::Pointer
132 LabelizeAndSelectLabels(const TImageType * input,
133 typename TImageType::PixelType BG,
134 typename TImageType::PixelType FG,
135 bool isFullyConnected,
136 int minimalComponentSize,
137 LabelizeParameters<typename TImageType::PixelType> * param);
139 //--------------------------------------------------------------------
140 template<class ImageType>
141 typename ImageType::Pointer
142 ResizeImageLike(const ImageType * input,
143 const itk::ImageBase<ImageType::ImageDimension> * like,
144 typename ImageType::PixelType BG);
147 //--------------------------------------------------------------------
148 template<class MaskImageType>
149 typename MaskImageType::Pointer
150 SliceBySliceRelativePosition(const MaskImageType * input,
151 const MaskImageType * object,
154 std::string orientation,
155 bool uniqueConnectedComponent=false,
157 bool autocropflag=true,
158 bool singleObjectCCL=true);
160 //--------------------------------------------------------------------
161 // In a binary image, search for the point belonging to the FG that
162 // is the most exterma in the direction 'direction' (or in the
163 // opposite if notFlag is given).
164 template<class ImageType>
166 FindExtremaPointInAGivenDirection(const ImageType * input,
167 typename ImageType::PixelType bg,
168 int direction, bool opposite,
169 typename ImageType::PointType & p);
171 //--------------------------------------------------------------------
173 //--------------------------------------------------------------------
174 // Same as above but check that the found point is not more than
175 // 'distanceMax' away from 'refPoint'
176 template<class ImageType>
178 FindExtremaPointInAGivenDirection(const ImageType * input,
179 typename ImageType::PixelType bg,
180 int direction, bool opposite,
181 typename ImageType::PointType refPoint,
183 typename ImageType::PointType & p);
185 //--------------------------------------------------------------------
187 //--------------------------------------------------------------------
188 template<class ImageType>
189 typename ImageType::Pointer
190 CropImageAlongOneAxis(const ImageType * image,
191 int dim, double min, double max,
192 bool autoCrop = false,
193 typename ImageType::PixelType BG=0);
194 template<class ImageType>
195 typename ImageType::Pointer
196 CropImageRemoveGreaterThan(const ImageType * image,
197 int dim, double min, bool autoCrop = false,
198 typename ImageType::PixelType BG=0);
199 template<class ImageType>
200 typename ImageType::Pointer
201 CropImageRemoveLowerThan(const ImageType * image,
202 int dim, double max,bool autoCrop = false,
203 typename ImageType::PixelType BG=0);
204 //--------------------------------------------------------------------
207 //--------------------------------------------------------------------
208 template<class ImageType, class LabelType>
209 typename itk::LabelMap< itk::ShapeLabelObject<LabelType, ImageType::ImageDimension> >::Pointer
210 ComputeLabelMap(const ImageType * image,
211 typename ImageType::PixelType BG,
212 bool computePerimeterFlag=false);
213 template<class ImageType>
215 ComputeCentroids(const ImageType * image,
216 typename ImageType::PixelType BG,
217 std::vector<typename ImageType::PointType> & centroids);
218 template<class ImageType>
220 ComputeCentroids2(const ImageType * image,
221 typename ImageType::PixelType BG,
222 std::vector<typename ImageType::PointType> & centroids);
223 //--------------------------------------------------------------------
226 //--------------------------------------------------------------------
227 template<class ImageType>
229 ExtractSlices(const ImageType * image, int direction,
230 std::vector<typename itk::Image<typename ImageType::PixelType,
231 ImageType::ImageDimension-1>::Pointer > & slices)
233 typedef ExtractSliceFilter<ImageType> ExtractSliceFilterType;
234 typedef typename ExtractSliceFilterType::SliceType SliceType;
235 typename ExtractSliceFilterType::Pointer
236 extractSliceFilter = ExtractSliceFilterType::New();
237 extractSliceFilter->SetInput(image);
238 extractSliceFilter->SetDirection(direction);
239 extractSliceFilter->Update();
240 extractSliceFilter->GetOutputSlices(slices);
242 //--------------------------------------------------------------------
245 //--------------------------------------------------------------------
246 template<class ImageType>
247 typename ImageType::Pointer
248 JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType,
249 ImageType::ImageDimension-1>::Pointer > & slices,
250 const ImageType * input,
252 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
253 typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
254 typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
255 joinFilter->SetOrigin(input->GetOrigin()[direction]);
256 joinFilter->SetSpacing(input->GetSpacing()[direction]);
257 for(unsigned int i=0; i<slices.size(); i++) {
258 joinFilter->PushBackInput(slices[i]);
260 joinFilter->Update();
261 return joinFilter->GetOutput();
263 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
267 // Set of tools to manage 3D points and 2D points in slices
268 template<class ImageType>
271 typedef typename ImageType::PointType PointType3D;
272 typedef typename ImageType::IndexType IndexType3D;
273 typedef typename ImageType::PixelType PixelType;
274 typedef typename ImageType::Pointer ImagePointer;
275 typedef typename ImageType::ConstPointer ImageConstPointer;
276 typedef itk::Image<PixelType, 2> SliceType;
277 typedef typename SliceType::PointType PointType2D;
278 typedef typename SliceType::IndexType IndexType2D;
280 typedef std::map<int, PointType2D> MapPoint2DType;
281 typedef std::vector<PointType3D> VectorPoint3DType;
282 typedef std::vector<PointType2D> VectorPoint2DType;
285 static void Convert2DTo3D(const PointType2D & p2D,
286 const ImageType * image,
289 static void Convert2DMapTo3DList(const MapPoint2DType & map,
290 const ImageType * image,
291 VectorPoint3DType & list);
292 static void Convert2DListTo3DList(const VectorPoint2DType & p,
294 const ImageType * image,
295 VectorPoint3DType & list);
298 //--------------------------------------------------------------------
299 template<class ImageType>
301 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
302 std::string filename);
303 //--------------------------------------------------------------------
306 //--------------------------------------------------------------------
307 template<class ImageType>
308 typename ImageType::Pointer
309 Dilate(const ImageType * image, double radiusInMM,
310 typename ImageType::PixelType BG,
311 typename ImageType::PixelType FG,
313 template<class ImageType>
314 typename ImageType::Pointer
315 Dilate(const ImageType * image, typename ImageType::SizeType radius,
316 typename ImageType::PixelType BG,
317 typename ImageType::PixelType FG,
319 template<class ImageType>
320 typename ImageType::Pointer
321 Dilate(const ImageType * image, typename ImageType::PointType radiusInMM,
322 typename ImageType::PixelType BG,
323 typename ImageType::PixelType FG,
325 //--------------------------------------------------------------------
328 //--------------------------------------------------------------------
329 template<class ImageType>
330 typename ImageType::Pointer
331 Opening(const ImageType * image, typename ImageType::SizeType radius,
332 typename ImageType::PixelType BG, typename ImageType::PixelType FG);
333 //--------------------------------------------------------------------
336 //--------------------------------------------------------------------
337 template<class ValueType, class VectorType>
338 void ConvertOption(std::string optionName, uint given,
339 ValueType * values, VectorType & p,
340 uint dim, bool required);
341 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \
342 ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
343 //--------------------------------------------------------------------
345 //--------------------------------------------------------------------
346 template<class ImageType>
348 SliceBySliceSetBackgroundFromLineSeparation(ImageType * input,
349 std::vector<typename ImageType::PointType> & lA,
350 std::vector<typename ImageType::PointType> & lB,
351 typename ImageType::PixelType BG,
353 double offsetToKeep);
354 //--------------------------------------------------------------------
357 //--------------------------------------------------------------------
358 template<class ImageType>
359 void AndNot(ImageType * input,
360 const ImageType * object,
361 typename ImageType::PixelType BG=0);
362 template<class ImageType>
363 void And(ImageType * input,
364 const ImageType * object,
365 typename ImageType::PixelType BG=0);
366 //--------------------------------------------------------------------
369 //--------------------------------------------------------------------
370 template<class ImageType>
371 typename ImageType::Pointer
372 Binarize(const ImageType * input,
373 typename ImageType::PixelType lower,
374 typename ImageType::PixelType upper,
375 typename ImageType::PixelType BG=0,
376 typename ImageType::PixelType FG=1);
377 //--------------------------------------------------------------------
380 //--------------------------------------------------------------------
381 template<class ImageType>
383 GetMinMaxPointPosition(const ImageType * input,
384 typename ImageType::PointType & min,
385 typename ImageType::PointType & max);
386 //--------------------------------------------------------------------
388 //--------------------------------------------------------------------
389 template<class ImageType>
390 typename ImageType::PointType
391 FindExtremaPointInAGivenLine(const ImageType * input,
392 int dimension, bool inverse,
393 typename ImageType::PointType p,
394 typename ImageType::PixelType BG,
396 //--------------------------------------------------------------------
399 //--------------------------------------------------------------------
400 template<class PointType>
402 IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
403 //--------------------------------------------------------------------
406 //--------------------------------------------------------------------
407 template<class ImageType>
409 SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input,
410 typename ImageType::PixelType BG,
412 int extremaDirection,
413 bool extremaOppositeFlag,
416 std::vector<typename ImageType::PointType> & A,
417 std::vector<typename ImageType::PointType> & B);
418 //--------------------------------------------------------------------
421 //--------------------------------------------------------------------
422 template<class ImageType>
423 typename ImageType::Pointer
424 SliceBySliceKeepMainCCL(const ImageType * input,
425 typename ImageType::PixelType BG,
426 typename ImageType::PixelType FG);
427 //--------------------------------------------------------------------
430 //--------------------------------------------------------------------
431 template<class ImageType>
432 typename ImageType::Pointer
433 Clone(const ImageType * input);
434 //--------------------------------------------------------------------
437 //--------------------------------------------------------------------
438 template<class ImageType>
439 typename ImageType::Pointer
440 SliceBySliceSetBackgroundFromSingleLine(const ImageType * input,
441 typename ImageType::PixelType BG,
442 typename ImageType::PointType & A,
443 typename ImageType::PointType & B,
444 int dim1, int dim2, bool removeLowerPartFlag);
445 //--------------------------------------------------------------------
448 //--------------------------------------------------------------------
449 template<class ImageType>
451 FillRegionWithValue(ImageType * input, typename ImageType::PixelType value, typename ImageType::RegionType & region);
452 //--------------------------------------------------------------------
456 #include "clitkSegmentationUtils.txx"