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>
31 #include <itkChangeInformationImageFilter.h>
35 http://answerpot.com/showthread.php?357451-Itk::SmartPointer%20-%20problem%20making%20code%20const-correct
36 it is better to take raw pointer as argument instead of SmartPointer.
41 //--------------------------------------------------------------------
42 template<class TInternalImageType, class TMaskInternalImageType>
43 typename TInternalImageType::Pointer
44 SetBackground(const TInternalImageType * input,
45 const TMaskInternalImageType * mask,
46 typename TMaskInternalImageType::PixelType maskBG,
47 typename TInternalImageType::PixelType outValue,
49 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 template<class ImageType>
54 int GetNumberOfConnectedComponentLabels(const ImageType * input,
55 typename ImageType::PixelType BG,
56 bool isFullyConnected);
57 //--------------------------------------------------------------------
60 //--------------------------------------------------------------------
61 template<class TImageType>
62 typename TImageType::Pointer
63 Labelize(const TImageType * input, typename TImageType::PixelType BG,
64 bool isFullyConnected, int minimalComponentSize);
65 template<class TImageType>
66 typename TImageType::Pointer
67 LabelizeAndCountNumberOfObjects(const TImageType * input,
68 typename TImageType::PixelType BG,
69 bool isFullyConnected,
70 int minimalComponentSize,
72 //--------------------------------------------------------------------
75 //--------------------------------------------------------------------
76 template<class ImageType>
77 typename ImageType::Pointer
78 RemoveLabels(const ImageType * input,
79 typename ImageType::PixelType BG,
80 std::vector<typename ImageType::PixelType> & labelsToRemove);
81 //--------------------------------------------------------------------
84 //--------------------------------------------------------------------
85 template<class ImageType>
86 typename ImageType::Pointer
87 AutoCrop(const ImageType * input,
88 typename ImageType::PixelType BG) {
89 typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
90 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
91 autoCropFilter->SetInput(input);
92 autoCropFilter->SetBackgroundValue(BG);
93 autoCropFilter->Update();
94 return autoCropFilter->GetOutput();
96 //--------------------------------------------------------------------
99 //--------------------------------------------------------------------
100 template<class TImageType>
101 typename TImageType::Pointer
102 KeepLabels(const TImageType * input,
103 typename TImageType::PixelType BG,
104 typename TImageType::PixelType FG,
105 typename TImageType::PixelType firstKeep,
106 typename TImageType::PixelType lastKeep,
108 //--------------------------------------------------------------------
111 //--------------------------------------------------------------------
112 template<class TImageType>
113 typename TImageType::Pointer
114 LabelizeAndSelectLabels(const TImageType * input,
115 typename TImageType::PixelType BG,
116 typename TImageType::PixelType FG,
117 bool isFullyConnected,
118 int minimalComponentSize,
119 LabelizeParameters<typename TImageType::PixelType> * param);
122 //--------------------------------------------------------------------
123 template<class MaskImageType>
124 typename MaskImageType::Pointer
125 SliceBySliceRelativePosition(const MaskImageType * input,
126 const MaskImageType * object,
129 std::string orientation,
130 bool uniqueConnectedComponent=false,
132 bool autocropflag=true,
133 bool singleObjectCCL=true);
134 template<class MaskImageType>
135 typename MaskImageType::Pointer
136 SliceBySliceRelativePosition(const MaskImageType * input,
137 const MaskImageType * object,
142 bool uniqueConnectedComponent=false,
144 bool autocropflag=true,
145 bool singleObjectCCL=true);
147 //--------------------------------------------------------------------
148 // In a binary image, search for the point belonging to the FG that
149 // is the most exterma in the direction 'direction' (or in the
150 // opposite if notFlag is given).
151 template<class ImageType>
153 FindExtremaPointInAGivenDirection(const ImageType * input,
154 typename ImageType::PixelType bg,
155 int direction, bool opposite,
156 typename ImageType::PointType & p);
158 //--------------------------------------------------------------------
160 //--------------------------------------------------------------------
161 // Same as above but check that the found point is not more than
162 // 'distanceMax' away from 'refPoint'
163 template<class ImageType>
165 FindExtremaPointInAGivenDirection(const ImageType * input,
166 typename ImageType::PixelType bg,
167 int direction, bool opposite,
168 typename ImageType::PointType refPoint,
170 typename ImageType::PointType & p);
172 //--------------------------------------------------------------------
174 //--------------------------------------------------------------------
175 template<class ImageType>
176 typename ImageType::Pointer
177 CropImageAlongOneAxis(const ImageType * image,
178 int dim, double min, double max,
179 bool autoCrop = false,
180 typename ImageType::PixelType BG=0);
181 template<class ImageType>
182 typename ImageType::Pointer
183 CropImageRemoveGreaterThan(const ImageType * image,
184 int dim, double min, bool autoCrop = false,
185 typename ImageType::PixelType BG=0);
186 template<class ImageType>
187 typename ImageType::Pointer
188 CropImageRemoveLowerThan(const ImageType * image,
189 int dim, double max,bool autoCrop = false,
190 typename ImageType::PixelType BG=0);
191 //--------------------------------------------------------------------
194 //--------------------------------------------------------------------
195 template<class ImageType, class LabelType>
196 typename itk::LabelMap< itk::ShapeLabelObject<LabelType, ImageType::ImageDimension> >::Pointer
197 ComputeLabelMap(const ImageType * image,
198 typename ImageType::PixelType BG,
199 bool computePerimeterFlag=false);
200 template<class ImageType>
202 ComputeCentroids(const ImageType * image,
203 typename ImageType::PixelType BG,
204 std::vector<typename ImageType::PointType> & centroids);
205 template<class ImageType>
207 ComputeCentroids2(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 direction,
217 std::vector<typename itk::Image<typename ImageType::PixelType,
218 ImageType::ImageDimension-1>::Pointer > & slices)
220 typedef ExtractSliceFilter<ImageType> ExtractSliceFilterType;
221 typedef typename ExtractSliceFilterType::SliceType SliceType;
222 typename ExtractSliceFilterType::Pointer
223 extractSliceFilter = ExtractSliceFilterType::New();
224 extractSliceFilter->SetInput(image);
225 extractSliceFilter->SetDirection(direction);
226 extractSliceFilter->Update();
227 extractSliceFilter->GetOutputSlices(slices);
229 //--------------------------------------------------------------------
232 //--------------------------------------------------------------------
233 template<class ImageType>
234 typename ImageType::Pointer
235 JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType,
236 ImageType::ImageDimension-1>::Pointer > & slices,
237 const itk::ImageBase<ImageType::ImageDimension> * input, //const ImageType * input,
239 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
240 typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
241 typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
242 joinFilter->SetOrigin(input->GetOrigin()[direction]);
243 joinFilter->SetSpacing(input->GetSpacing()[direction]);
244 for(unsigned int i=0; i<slices.size(); i++) {
245 joinFilter->PushBackInput(slices[i]);
247 joinFilter->Update();
248 return joinFilter->GetOutput();
250 //--------------------------------------------------------------------
253 //--------------------------------------------------------------------
254 // Set of tools to manage 3D points and 2D points in slices
255 template<class ImageType>
258 typedef typename ImageType::PointType PointType3D;
259 typedef typename ImageType::IndexType IndexType3D;
260 typedef typename ImageType::PixelType PixelType;
261 typedef typename ImageType::Pointer ImagePointer;
262 typedef typename ImageType::ConstPointer ImageConstPointer;
263 typedef itk::Image<PixelType, 2> SliceType;
264 typedef typename SliceType::PointType PointType2D;
265 typedef typename SliceType::IndexType IndexType2D;
267 typedef std::map<int, PointType2D> MapPoint2DType;
268 typedef std::vector<PointType3D> VectorPoint3DType;
269 typedef std::vector<PointType2D> VectorPoint2DType;
272 static void Convert2DTo3D(const PointType2D & p2D,
273 const ImageType * image,
276 static void Convert2DMapTo3DList(const MapPoint2DType & map,
277 const ImageType * image,
278 VectorPoint3DType & list);
279 static void Convert2DListTo3DList(const VectorPoint2DType & p,
281 const ImageType * image,
282 VectorPoint3DType & list);
285 //--------------------------------------------------------------------
286 template<class ImageType>
288 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
289 std::string filename);
290 //--------------------------------------------------------------------
293 //--------------------------------------------------------------------
294 template<class ImageType>
295 typename ImageType::Pointer
296 Dilate(const ImageType * image, double radiusInMM,
297 typename ImageType::PixelType BG,
298 typename ImageType::PixelType FG,
300 template<class ImageType>
301 typename ImageType::Pointer
302 Dilate(const ImageType * image, typename ImageType::SizeType radius,
303 typename ImageType::PixelType BG,
304 typename ImageType::PixelType FG,
306 template<class ImageType>
307 typename ImageType::Pointer
308 Dilate(const ImageType * image, typename ImageType::PointType radiusInMM,
309 typename ImageType::PixelType BG,
310 typename ImageType::PixelType FG,
312 //--------------------------------------------------------------------
315 //--------------------------------------------------------------------
316 template<class ImageType>
317 typename ImageType::Pointer
318 Opening(const ImageType * image, typename ImageType::SizeType radius,
319 typename ImageType::PixelType BG, typename ImageType::PixelType FG);
320 //--------------------------------------------------------------------
323 //--------------------------------------------------------------------
324 template<class ValueType, class VectorType>
325 void ConvertOption(std::string optionName, uint given,
326 ValueType * values, VectorType & p,
327 uint dim, bool required);
328 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \
329 ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
330 //--------------------------------------------------------------------
332 //--------------------------------------------------------------------
333 template<class ImageType>
335 SliceBySliceSetBackgroundFromLineSeparation(ImageType * input,
336 std::vector<typename ImageType::PointType> & lA,
337 std::vector<typename ImageType::PointType> & lB,
338 typename ImageType::PixelType BG,
340 double offsetToKeep);
341 //--------------------------------------------------------------------
344 //--------------------------------------------------------------------
345 template<class ImageType>
346 void AndNot(ImageType * input,
347 const ImageType * object,
348 typename ImageType::PixelType BG=0);
349 template<class ImageType>
350 void And(ImageType * input,
351 const ImageType * object,
352 typename ImageType::PixelType BG=0);
353 template<class ImageType>
354 void Or(ImageType * input,
355 const ImageType * object,
356 typename ImageType::PixelType BG=0);
357 //--------------------------------------------------------------------
360 //--------------------------------------------------------------------
361 template<class ImageType>
362 typename ImageType::Pointer
363 Binarize(const ImageType * input,
364 typename ImageType::PixelType lower,
365 typename ImageType::PixelType upper,
366 typename ImageType::PixelType BG=0,
367 typename ImageType::PixelType FG=1);
368 //--------------------------------------------------------------------
371 //--------------------------------------------------------------------
372 template<class ImageType>
374 GetMinMaxPointPosition(const ImageType * input,
375 typename ImageType::PointType & min,
376 typename ImageType::PointType & max);
377 //--------------------------------------------------------------------
379 //--------------------------------------------------------------------
380 template<class ImageType>
381 typename ImageType::PointType
382 FindExtremaPointInAGivenLine(const ImageType * input,
383 int dimension, bool inverse,
384 typename ImageType::PointType p,
385 typename ImageType::PixelType BG,
387 //--------------------------------------------------------------------
390 //--------------------------------------------------------------------
391 template<class PointType>
393 IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
394 //--------------------------------------------------------------------
397 //--------------------------------------------------------------------
398 template<class ImageType>
400 SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input,
401 typename ImageType::PixelType BG,
403 int extremaDirection,
404 bool extremaOppositeFlag,
407 std::vector<typename ImageType::PointType> & A,
408 std::vector<typename ImageType::PointType> & B);
409 template<class ImageType>
411 SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1,
412 const ImageType * S2,
413 typename ImageType::PixelType BG,
415 std::vector<typename ImageType::PointType> & A,
416 std::vector<typename ImageType::PointType> & B);
417 //--------------------------------------------------------------------
420 //--------------------------------------------------------------------
421 template<class ImageType>
422 typename ImageType::Pointer
423 SliceBySliceKeepMainCCL(const ImageType * input,
424 typename ImageType::PixelType BG,
425 typename ImageType::PixelType FG);
426 //--------------------------------------------------------------------
429 //--------------------------------------------------------------------
430 template<class ImageType>
431 typename ImageType::Pointer
432 Clone(const ImageType * input);
433 //--------------------------------------------------------------------
436 //--------------------------------------------------------------------
437 template<class ImageType>
438 typename ImageType::Pointer
439 SliceBySliceSetBackgroundFromSingleLine(const ImageType * input,
440 typename ImageType::PixelType BG,
441 typename ImageType::PointType & A,
442 typename ImageType::PointType & B,
443 int dim1, int dim2, bool removeLowerPartFlag);
444 //--------------------------------------------------------------------
447 //--------------------------------------------------------------------
448 template<class ImageType>
449 typename ImageType::Pointer
450 SliceBySliceSetBackgroundFromPoints(const ImageType * input,
451 typename ImageType::PixelType BG,
453 std::vector<typename ImageType::PointType> & A,
454 bool removeGreaterThanXFlag,
455 bool removeGreaterThanYFlag);
456 //--------------------------------------------------------------------
459 //--------------------------------------------------------------------
460 template<class ImageType>
462 FillRegionWithValue(ImageType * input, typename ImageType::PixelType value,
463 typename ImageType::RegionType & region);
464 //--------------------------------------------------------------------
467 //--------------------------------------------------------------------
468 template<class ImageType>
470 GetMinMaxBoundary(ImageType * input, typename ImageType::PointType & min,
471 typename ImageType::PointType & max);
472 //--------------------------------------------------------------------
475 //--------------------------------------------------------------------
476 template<class ImageType>
477 typename itk::Image<float, ImageType::ImageDimension>::Pointer//void
478 DistanceMap(const ImageType * input, typename ImageType::PixelType BG);//,
479 //--------------------------------------------------------------------
482 //--------------------------------------------------------------------
483 template<class ImageType>
484 typename ImageType::PointType
485 ComputeClosestPoint(const ImageType * input,
486 const itk::Image<float, ImageType::ImageDimension> * dmap,
487 typename ImageType::PixelType & BG);
488 //--------------------------------------------------------------------
490 //--------------------------------------------------------------------
491 template<class ImageType>
492 typename ImageType::Pointer
493 RemoveNegativeIndexFromRegion(ImageType * input);
494 //--------------------------------------------------------------------
497 } // end clitk namespace
499 #include "clitkSegmentationUtils.txx"