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 TInternalImageType, class TMaskInternalImageType>
42 typename TInternalImageType::Pointer
43 SetBackground(const TInternalImageType * input,
44 const TMaskInternalImageType * mask,
45 typename TMaskInternalImageType::PixelType maskBG,
46 typename TInternalImageType::PixelType outValue,
48 //--------------------------------------------------------------------
51 //--------------------------------------------------------------------
52 template<class ImageType>
53 int GetNumberOfConnectedComponentLabels(const ImageType * input,
54 typename ImageType::PixelType BG,
55 bool isFullyConnected);
56 //--------------------------------------------------------------------
59 //--------------------------------------------------------------------
60 template<class TImageType>
61 typename TImageType::Pointer
62 Labelize(const TImageType * input, typename TImageType::PixelType BG,
63 bool isFullyConnected, int minimalComponentSize);
64 template<class TImageType>
65 typename TImageType::Pointer
66 LabelizeAndCountNumberOfObjects(const TImageType * input,
67 typename TImageType::PixelType BG,
68 bool isFullyConnected,
69 int minimalComponentSize,
71 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
75 template<class ImageType>
76 typename ImageType::Pointer
77 RemoveLabels(const ImageType * input,
78 typename ImageType::PixelType BG,
79 std::vector<typename ImageType::PixelType> & labelsToRemove);
80 //--------------------------------------------------------------------
83 //--------------------------------------------------------------------
84 template<class ImageType>
85 typename ImageType::Pointer
86 AutoCrop(const ImageType * input,
87 typename ImageType::PixelType BG) {
88 typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
89 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
90 autoCropFilter->SetInput(input);
91 autoCropFilter->SetBackgroundValue(BG);
92 autoCropFilter->Update();
93 return autoCropFilter->GetOutput();
95 //--------------------------------------------------------------------
98 //--------------------------------------------------------------------
99 template<class TImageType>
100 typename TImageType::Pointer
101 KeepLabels(const TImageType * input,
102 typename TImageType::PixelType BG,
103 typename TImageType::PixelType FG,
104 typename TImageType::PixelType firstKeep,
105 typename TImageType::PixelType lastKeep,
107 //--------------------------------------------------------------------
110 //--------------------------------------------------------------------
111 template<class TImageType>
112 typename TImageType::Pointer
113 LabelizeAndSelectLabels(const TImageType * input,
114 typename TImageType::PixelType BG,
115 typename TImageType::PixelType FG,
116 bool isFullyConnected,
117 int minimalComponentSize,
118 LabelizeParameters<typename TImageType::PixelType> * param);
121 //--------------------------------------------------------------------
122 template<class MaskImageType>
123 typename MaskImageType::Pointer
124 SliceBySliceRelativePosition(const MaskImageType * input,
125 const MaskImageType * object,
128 std::string orientation,
129 bool uniqueConnectedComponent=false,
131 bool autocropflag=true,
132 bool singleObjectCCL=true);
133 template<class MaskImageType>
134 typename MaskImageType::Pointer
135 SliceBySliceRelativePosition(const MaskImageType * input,
136 const MaskImageType * object,
141 bool uniqueConnectedComponent=false,
143 bool autocropflag=true,
144 bool singleObjectCCL=true);
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>
152 FindExtremaPointInAGivenDirection(const ImageType * input,
153 typename ImageType::PixelType bg,
154 int direction, bool opposite,
155 typename ImageType::PointType & p);
157 //--------------------------------------------------------------------
159 //--------------------------------------------------------------------
160 // Same as above but check that the found point is not more than
161 // 'distanceMax' away from 'refPoint'
162 template<class ImageType>
164 FindExtremaPointInAGivenDirection(const ImageType * input,
165 typename ImageType::PixelType bg,
166 int direction, bool opposite,
167 typename ImageType::PointType refPoint,
169 typename ImageType::PointType & p);
171 //--------------------------------------------------------------------
173 //--------------------------------------------------------------------
174 template<class ImageType>
175 typename ImageType::Pointer
176 CropImageAlongOneAxis(const ImageType * 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 CropImageRemoveGreaterThan(const ImageType * image,
183 int dim, double min, bool autoCrop = false,
184 typename ImageType::PixelType BG=0);
185 template<class ImageType>
186 typename ImageType::Pointer
187 CropImageRemoveLowerThan(const ImageType * image,
188 int dim, double max,bool autoCrop = false,
189 typename ImageType::PixelType BG=0);
190 //--------------------------------------------------------------------
193 //--------------------------------------------------------------------
194 template<class ImageType, class LabelType>
195 typename itk::LabelMap< itk::ShapeLabelObject<LabelType, ImageType::ImageDimension> >::Pointer
196 ComputeLabelMap(const ImageType * image,
197 typename ImageType::PixelType BG,
198 bool computePerimeterFlag=false);
199 template<class ImageType>
201 ComputeCentroids(const ImageType * image,
202 typename ImageType::PixelType BG,
203 std::vector<typename ImageType::PointType> & centroids);
204 template<class ImageType>
206 ComputeCentroids2(const ImageType * image,
207 typename ImageType::PixelType BG,
208 std::vector<typename ImageType::PointType> & centroids);
209 //--------------------------------------------------------------------
212 //--------------------------------------------------------------------
213 template<class ImageType>
215 ExtractSlices(const ImageType * image, int direction,
216 std::vector<typename itk::Image<typename ImageType::PixelType,
217 ImageType::ImageDimension-1>::Pointer > & slices)
219 typedef ExtractSliceFilter<ImageType> ExtractSliceFilterType;
220 typedef typename ExtractSliceFilterType::SliceType SliceType;
221 typename ExtractSliceFilterType::Pointer
222 extractSliceFilter = ExtractSliceFilterType::New();
223 extractSliceFilter->SetInput(image);
224 extractSliceFilter->SetDirection(direction);
225 extractSliceFilter->Update();
226 extractSliceFilter->GetOutputSlices(slices);
228 //--------------------------------------------------------------------
231 //--------------------------------------------------------------------
232 template<class ImageType>
233 typename ImageType::Pointer
234 JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType,
235 ImageType::ImageDimension-1>::Pointer > & slices,
236 const itk::ImageBase<ImageType::ImageDimension> * input, //const ImageType * input,
238 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
239 typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
240 typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
241 joinFilter->SetOrigin(input->GetOrigin()[direction]);
242 joinFilter->SetSpacing(input->GetSpacing()[direction]);
243 for(unsigned int i=0; i<slices.size(); i++) {
244 joinFilter->PushBackInput(slices[i]);
246 joinFilter->Update();
247 return joinFilter->GetOutput();
249 //--------------------------------------------------------------------
252 //--------------------------------------------------------------------
253 // Set of tools to manage 3D points and 2D points in slices
254 template<class ImageType>
257 typedef typename ImageType::PointType PointType3D;
258 typedef typename ImageType::IndexType IndexType3D;
259 typedef typename ImageType::PixelType PixelType;
260 typedef typename ImageType::Pointer ImagePointer;
261 typedef typename ImageType::ConstPointer ImageConstPointer;
262 typedef itk::Image<PixelType, 2> SliceType;
263 typedef typename SliceType::PointType PointType2D;
264 typedef typename SliceType::IndexType IndexType2D;
266 typedef std::map<int, PointType2D> MapPoint2DType;
267 typedef std::vector<PointType3D> VectorPoint3DType;
268 typedef std::vector<PointType2D> VectorPoint2DType;
271 static void Convert2DTo3D(const PointType2D & p2D,
272 const ImageType * image,
275 static void Convert2DMapTo3DList(const MapPoint2DType & map,
276 const ImageType * image,
277 VectorPoint3DType & list);
278 static void Convert2DListTo3DList(const VectorPoint2DType & p,
280 const ImageType * image,
281 VectorPoint3DType & list);
284 //--------------------------------------------------------------------
285 template<class ImageType>
287 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
288 std::string filename);
289 //--------------------------------------------------------------------
292 //--------------------------------------------------------------------
293 template<class ImageType>
294 typename ImageType::Pointer
295 Dilate(const ImageType * image, double radiusInMM,
296 typename ImageType::PixelType BG,
297 typename ImageType::PixelType FG,
299 template<class ImageType>
300 typename ImageType::Pointer
301 Dilate(const ImageType * image, typename ImageType::SizeType radius,
302 typename ImageType::PixelType BG,
303 typename ImageType::PixelType FG,
305 template<class ImageType>
306 typename ImageType::Pointer
307 Dilate(const ImageType * image, typename ImageType::PointType radiusInMM,
308 typename ImageType::PixelType BG,
309 typename ImageType::PixelType FG,
311 //--------------------------------------------------------------------
314 //--------------------------------------------------------------------
315 template<class ImageType>
316 typename ImageType::Pointer
317 Opening(const ImageType * image, typename ImageType::SizeType radius,
318 typename ImageType::PixelType BG, typename ImageType::PixelType FG);
319 //--------------------------------------------------------------------
322 //--------------------------------------------------------------------
323 template<class ValueType, class VectorType>
324 void ConvertOption(std::string optionName, uint given,
325 ValueType * values, VectorType & p,
326 uint dim, bool required);
327 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \
328 ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
329 //--------------------------------------------------------------------
331 //--------------------------------------------------------------------
332 template<class ImageType>
334 SliceBySliceSetBackgroundFromLineSeparation(ImageType * input,
335 std::vector<typename ImageType::PointType> & lA,
336 std::vector<typename ImageType::PointType> & lB,
337 typename ImageType::PixelType BG,
339 double offsetToKeep);
340 //--------------------------------------------------------------------
343 //--------------------------------------------------------------------
344 template<class ImageType>
345 void AndNot(ImageType * input,
346 const ImageType * object,
347 typename ImageType::PixelType BG=0);
348 template<class ImageType>
349 void And(ImageType * input,
350 const ImageType * object,
351 typename ImageType::PixelType BG=0);
352 template<class ImageType>
353 void Or(ImageType * input,
354 const ImageType * object,
355 typename ImageType::PixelType BG=0);
356 //--------------------------------------------------------------------
359 //--------------------------------------------------------------------
360 template<class ImageType>
361 typename ImageType::Pointer
362 Binarize(const ImageType * input,
363 typename ImageType::PixelType lower,
364 typename ImageType::PixelType upper,
365 typename ImageType::PixelType BG=0,
366 typename ImageType::PixelType FG=1);
367 //--------------------------------------------------------------------
370 //--------------------------------------------------------------------
371 template<class ImageType>
373 GetMinMaxPointPosition(const ImageType * input,
374 typename ImageType::PointType & min,
375 typename ImageType::PointType & max);
376 //--------------------------------------------------------------------
378 //--------------------------------------------------------------------
379 template<class ImageType>
380 typename ImageType::PointType
381 FindExtremaPointInAGivenLine(const ImageType * input,
382 int dimension, bool inverse,
383 typename ImageType::PointType p,
384 typename ImageType::PixelType BG,
386 //--------------------------------------------------------------------
389 //--------------------------------------------------------------------
390 template<class PointType>
392 IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
393 //--------------------------------------------------------------------
396 //--------------------------------------------------------------------
397 template<class ImageType>
399 SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input,
400 typename ImageType::PixelType BG,
402 int extremaDirection,
403 bool extremaOppositeFlag,
406 std::vector<typename ImageType::PointType> & A,
407 std::vector<typename ImageType::PointType> & B);
408 template<class ImageType>
410 SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1,
411 const ImageType * S2,
412 typename ImageType::PixelType BG,
414 std::vector<typename ImageType::PointType> & A,
415 std::vector<typename ImageType::PointType> & B);
416 //--------------------------------------------------------------------
419 //--------------------------------------------------------------------
420 template<class ImageType>
421 typename ImageType::Pointer
422 SliceBySliceKeepMainCCL(const ImageType * input,
423 typename ImageType::PixelType BG,
424 typename ImageType::PixelType FG);
425 //--------------------------------------------------------------------
428 //--------------------------------------------------------------------
429 template<class ImageType>
430 typename ImageType::Pointer
431 Clone(const ImageType * input);
432 //--------------------------------------------------------------------
435 //--------------------------------------------------------------------
436 template<class ImageType>
437 typename ImageType::Pointer
438 SliceBySliceSetBackgroundFromSingleLine(const ImageType * input,
439 typename ImageType::PixelType BG,
440 typename ImageType::PointType & A,
441 typename ImageType::PointType & B,
442 int dim1, int dim2, bool removeLowerPartFlag);
443 //--------------------------------------------------------------------
446 //--------------------------------------------------------------------
447 template<class ImageType>
448 typename ImageType::Pointer
449 SliceBySliceSetBackgroundFromPoints(const ImageType * input,
450 typename ImageType::PixelType BG,
452 std::vector<typename ImageType::PointType> & A,
453 bool removeGreaterThanXFlag,
454 bool removeGreaterThanYFlag);
455 //--------------------------------------------------------------------
458 //--------------------------------------------------------------------
459 template<class ImageType>
461 FillRegionWithValue(ImageType * input, typename ImageType::PixelType value,
462 typename ImageType::RegionType & region);
463 //--------------------------------------------------------------------
466 //--------------------------------------------------------------------
467 template<class ImageType>
469 GetMinMaxBoundary(ImageType * input, typename ImageType::PointType & min,
470 typename ImageType::PointType & max);
471 //--------------------------------------------------------------------
474 //--------------------------------------------------------------------
475 template<class ImageType>
476 typename itk::Image<float, ImageType::ImageDimension>::Pointer//void
477 DistanceMap(const ImageType * input, typename ImageType::PixelType BG);//,
478 //--------------------------------------------------------------------
481 //--------------------------------------------------------------------
482 template<class ImageType>
483 typename ImageType::PointType
484 ComputeClosestPoint(const ImageType * input,
485 const itk::Image<float, ImageType::ImageDimension> * dmap,
486 typename ImageType::PixelType & BG);
487 //--------------------------------------------------------------------
491 } // end clitk namespace
493 #include "clitkSegmentationUtils.txx"