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);
134 //--------------------------------------------------------------------
135 // In a binary image, search for the point belonging to the FG that
136 // is the most exterma in the direction 'direction' (or in the
137 // opposite if notFlag is given).
138 template<class ImageType>
140 FindExtremaPointInAGivenDirection(const ImageType * input,
141 typename ImageType::PixelType bg,
142 int direction, bool opposite,
143 typename ImageType::PointType & p);
145 //--------------------------------------------------------------------
147 //--------------------------------------------------------------------
148 // Same as above but check that the found point is not more than
149 // 'distanceMax' away from 'refPoint'
150 template<class ImageType>
152 FindExtremaPointInAGivenDirection(const ImageType * input,
153 typename ImageType::PixelType bg,
154 int direction, bool opposite,
155 typename ImageType::PointType refPoint,
157 typename ImageType::PointType & p);
159 //--------------------------------------------------------------------
161 //--------------------------------------------------------------------
162 template<class ImageType>
163 typename ImageType::Pointer
164 CropImageAlongOneAxis(const ImageType * image,
165 int dim, double min, double max,
166 bool autoCrop = false,
167 typename ImageType::PixelType BG=0);
168 template<class ImageType>
169 typename ImageType::Pointer
170 CropImageRemoveGreaterThan(const ImageType * image,
171 int dim, double min, bool autoCrop = false,
172 typename ImageType::PixelType BG=0);
173 template<class ImageType>
174 typename ImageType::Pointer
175 CropImageRemoveLowerThan(const ImageType * image,
176 int dim, double max,bool autoCrop = false,
177 typename ImageType::PixelType BG=0);
178 //--------------------------------------------------------------------
181 //--------------------------------------------------------------------
182 template<class ImageType, class LabelType>
183 typename itk::LabelMap< itk::ShapeLabelObject<LabelType, ImageType::ImageDimension> >::Pointer
184 ComputeLabelMap(const ImageType * image,
185 typename ImageType::PixelType BG,
186 bool computePerimeterFlag=false);
187 template<class ImageType>
189 ComputeCentroids(const ImageType * image,
190 typename ImageType::PixelType BG,
191 std::vector<typename ImageType::PointType> & centroids);
192 template<class ImageType>
194 ComputeCentroids2(const ImageType * image,
195 typename ImageType::PixelType BG,
196 std::vector<typename ImageType::PointType> & centroids);
197 //--------------------------------------------------------------------
200 //--------------------------------------------------------------------
201 template<class ImageType>
203 ExtractSlices(const ImageType * image, int direction,
204 std::vector<typename itk::Image<typename ImageType::PixelType,
205 ImageType::ImageDimension-1>::Pointer > & slices)
207 typedef ExtractSliceFilter<ImageType> ExtractSliceFilterType;
208 typedef typename ExtractSliceFilterType::SliceType SliceType;
209 typename ExtractSliceFilterType::Pointer
210 extractSliceFilter = ExtractSliceFilterType::New();
211 extractSliceFilter->SetInput(image);
212 extractSliceFilter->SetDirection(direction);
213 extractSliceFilter->Update();
214 extractSliceFilter->GetOutputSlices(slices);
216 //--------------------------------------------------------------------
219 //--------------------------------------------------------------------
220 template<class ImageType>
221 typename ImageType::Pointer
222 JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType,
223 ImageType::ImageDimension-1>::Pointer > & slices,
224 const itk::ImageBase<ImageType::ImageDimension> * input, //const ImageType * input,
226 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
227 typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
228 typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
229 joinFilter->SetOrigin(input->GetOrigin()[direction]);
230 joinFilter->SetSpacing(input->GetSpacing()[direction]);
231 for(unsigned int i=0; i<slices.size(); i++) {
232 joinFilter->PushBackInput(slices[i]);
234 joinFilter->Update();
235 return joinFilter->GetOutput();
237 //--------------------------------------------------------------------
240 //--------------------------------------------------------------------
241 // Set of tools to manage 3D points and 2D points in slices
242 template<class ImageType>
245 typedef typename ImageType::PointType PointType3D;
246 typedef typename ImageType::IndexType IndexType3D;
247 typedef typename ImageType::PixelType PixelType;
248 typedef typename ImageType::Pointer ImagePointer;
249 typedef typename ImageType::ConstPointer ImageConstPointer;
250 typedef itk::Image<PixelType, 2> SliceType;
251 typedef typename SliceType::PointType PointType2D;
252 typedef typename SliceType::IndexType IndexType2D;
254 typedef std::map<int, PointType2D> MapPoint2DType;
255 typedef std::vector<PointType3D> VectorPoint3DType;
256 typedef std::vector<PointType2D> VectorPoint2DType;
259 static void Convert2DTo3D(const PointType2D & p2D,
260 const ImageType * image,
263 static void Convert2DMapTo3DList(const MapPoint2DType & map,
264 const ImageType * image,
265 VectorPoint3DType & list);
266 static void Convert2DListTo3DList(const VectorPoint2DType & p,
268 const ImageType * image,
269 VectorPoint3DType & list);
272 //--------------------------------------------------------------------
273 template<class ImageType>
275 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
276 std::string filename);
277 //--------------------------------------------------------------------
280 //--------------------------------------------------------------------
281 template<class ImageType>
282 typename ImageType::Pointer
283 Dilate(const ImageType * image, double radiusInMM,
284 typename ImageType::PixelType BG,
285 typename ImageType::PixelType FG,
287 template<class ImageType>
288 typename ImageType::Pointer
289 Dilate(const ImageType * image, typename ImageType::SizeType radius,
290 typename ImageType::PixelType BG,
291 typename ImageType::PixelType FG,
293 template<class ImageType>
294 typename ImageType::Pointer
295 Dilate(const ImageType * image, typename ImageType::PointType radiusInMM,
296 typename ImageType::PixelType BG,
297 typename ImageType::PixelType FG,
299 //--------------------------------------------------------------------
302 //--------------------------------------------------------------------
303 template<class ImageType>
304 typename ImageType::Pointer
305 Opening(const ImageType * image, typename ImageType::SizeType radius,
306 typename ImageType::PixelType BG, typename ImageType::PixelType FG);
307 //--------------------------------------------------------------------
310 //--------------------------------------------------------------------
311 template<class ValueType, class VectorType>
312 void ConvertOption(std::string optionName, uint given,
313 ValueType * values, VectorType & p,
314 uint dim, bool required);
315 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \
316 ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
317 //--------------------------------------------------------------------
319 //--------------------------------------------------------------------
320 template<class ImageType>
322 SliceBySliceSetBackgroundFromLineSeparation(ImageType * input,
323 std::vector<typename ImageType::PointType> & lA,
324 std::vector<typename ImageType::PointType> & lB,
325 typename ImageType::PixelType BG,
327 double offsetToKeep);
328 //--------------------------------------------------------------------
331 //--------------------------------------------------------------------
332 template<class ImageType>
333 void AndNot(ImageType * input,
334 const ImageType * object,
335 typename ImageType::PixelType BG=0);
336 template<class ImageType>
337 void And(ImageType * input,
338 const ImageType * object,
339 typename ImageType::PixelType BG=0);
340 template<class ImageType>
341 void Or(ImageType * input,
342 const ImageType * object,
343 typename ImageType::PixelType BG=0);
344 //--------------------------------------------------------------------
347 //--------------------------------------------------------------------
348 template<class ImageType>
349 typename ImageType::Pointer
350 Binarize(const ImageType * input,
351 typename ImageType::PixelType lower,
352 typename ImageType::PixelType upper,
353 typename ImageType::PixelType BG=0,
354 typename ImageType::PixelType FG=1);
355 //--------------------------------------------------------------------
358 //--------------------------------------------------------------------
359 template<class ImageType>
361 GetMinMaxPointPosition(const ImageType * input,
362 typename ImageType::PointType & min,
363 typename ImageType::PointType & max);
364 //--------------------------------------------------------------------
366 //--------------------------------------------------------------------
367 template<class ImageType>
368 typename ImageType::PointType
369 FindExtremaPointInAGivenLine(const ImageType * input,
370 int dimension, bool inverse,
371 typename ImageType::PointType p,
372 typename ImageType::PixelType BG,
374 //--------------------------------------------------------------------
377 //--------------------------------------------------------------------
378 template<class PointType>
380 IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
381 //--------------------------------------------------------------------
384 //--------------------------------------------------------------------
385 template<class ImageType>
387 SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input,
388 typename ImageType::PixelType BG,
390 int extremaDirection,
391 bool extremaOppositeFlag,
394 std::vector<typename ImageType::PointType> & A,
395 std::vector<typename ImageType::PointType> & B);
396 template<class ImageType>
398 SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1,
399 const ImageType * S2,
400 typename ImageType::PixelType BG,
402 std::vector<typename ImageType::PointType> & A,
403 std::vector<typename ImageType::PointType> & B);
404 //--------------------------------------------------------------------
407 //--------------------------------------------------------------------
408 template<class ImageType>
409 typename ImageType::Pointer
410 SliceBySliceKeepMainCCL(const ImageType * input,
411 typename ImageType::PixelType BG,
412 typename ImageType::PixelType FG);
413 //--------------------------------------------------------------------
416 //--------------------------------------------------------------------
417 template<class ImageType>
418 typename ImageType::Pointer
419 Clone(const ImageType * input);
420 //--------------------------------------------------------------------
423 //--------------------------------------------------------------------
424 template<class ImageType>
425 typename ImageType::Pointer
426 SliceBySliceSetBackgroundFromSingleLine(const ImageType * input,
427 typename ImageType::PixelType BG,
428 typename ImageType::PointType & A,
429 typename ImageType::PointType & B,
430 int dim1, int dim2, bool removeLowerPartFlag);
431 //--------------------------------------------------------------------
434 //--------------------------------------------------------------------
435 template<class ImageType>
436 typename ImageType::Pointer
437 SliceBySliceSetBackgroundFromPoints(const ImageType * input,
438 typename ImageType::PixelType BG,
440 std::vector<typename ImageType::PointType> & A,
441 bool removeGreaterThanXFlag,
442 bool removeGreaterThanYFlag);
443 //--------------------------------------------------------------------
446 //--------------------------------------------------------------------
447 template<class ImageType>
449 FillRegionWithValue(ImageType * input, typename ImageType::PixelType value,
450 typename ImageType::RegionType & region);
451 //--------------------------------------------------------------------
454 //--------------------------------------------------------------------
455 template<class ImageType>
457 GetMinMaxBoundary(ImageType * input, typename ImageType::PointType & min,
458 typename ImageType::PointType & max);
459 //--------------------------------------------------------------------
462 //--------------------------------------------------------------------
463 template<class ImageType>
464 typename itk::Image<float, ImageType::ImageDimension>::Pointer//void
465 DistanceMap(const ImageType * input, typename ImageType::PixelType BG);//,
466 //--------------------------------------------------------------------
469 //--------------------------------------------------------------------
470 template<class ImageType>
471 typename ImageType::PointType
472 ComputeClosestPoint(const ImageType * input,
473 const itk::Image<float, ImageType::ImageDimension> * dmap,
474 typename ImageType::PixelType & BG);
475 //--------------------------------------------------------------------
479 } // end clitk namespace
481 #include "clitkSegmentationUtils.txx"