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>
29 #include <itkJoinSeriesImageFilter.h>
33 http://answerpot.com/showthread.php?357451-Itk::SmartPointer%20-%20problem%20making%20code%20const-correct
34 it is better to take raw pointer as argument instead of SmartPointer.
39 //--------------------------------------------------------------------
40 template<class ImageType>
41 void ComputeBBFromImageRegion(const ImageType * image,
42 typename ImageType::RegionType region,
43 typename itk::BoundingBox<unsigned long,
44 ImageType::ImageDimension>::Pointer bb);
46 //--------------------------------------------------------------------
47 template<int Dimension>
48 void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo,
49 typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1,
50 typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2);
52 //--------------------------------------------------------------------
53 template<class ImageType>
54 void ComputeRegionFromBB(const ImageType * image,
55 const typename itk::BoundingBox<unsigned long,
56 ImageType::ImageDimension>::Pointer bb,
57 typename ImageType::RegionType & region);
58 //--------------------------------------------------------------------
59 template<class TInternalImageType, class TMaskInternalImageType>
60 typename TInternalImageType::Pointer
61 SetBackground(const TInternalImageType * input,
62 const TMaskInternalImageType * mask,
63 typename TMaskInternalImageType::PixelType maskBG,
64 typename TInternalImageType::PixelType outValue,
66 //--------------------------------------------------------------------
69 //--------------------------------------------------------------------
70 template<class ImageType>
71 int GetNumberOfConnectedComponentLabels(const ImageType * input,
72 typename ImageType::PixelType BG,
73 bool isFullyConnected);
74 //--------------------------------------------------------------------
77 //--------------------------------------------------------------------
78 template<class TImageType>
79 typename TImageType::Pointer
80 Labelize(const TImageType * input, typename TImageType::PixelType BG,
81 bool isFullyConnected, int minimalComponentSize);
82 template<class TImageType>
83 typename TImageType::Pointer
84 LabelizeAndCountNumberOfObjects(const TImageType * input,
85 typename TImageType::PixelType BG,
86 bool isFullyConnected,
87 int minimalComponentSize,
89 //--------------------------------------------------------------------
92 //--------------------------------------------------------------------
93 template<class ImageType>
94 typename ImageType::Pointer
95 RemoveLabels(const ImageType * input,
96 typename ImageType::PixelType BG,
97 std::vector<typename ImageType::PixelType> & labelsToRemove);
98 //--------------------------------------------------------------------
101 //--------------------------------------------------------------------
102 template<class ImageType>
103 typename ImageType::Pointer
104 AutoCrop(const ImageType * input,
105 typename ImageType::PixelType BG) {
106 typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
107 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
108 autoCropFilter->SetInput(input);
109 autoCropFilter->SetBackgroundValue(BG);
110 autoCropFilter->Update();
111 return autoCropFilter->GetOutput();
113 //--------------------------------------------------------------------
116 //--------------------------------------------------------------------
117 template<class TImageType>
118 typename TImageType::Pointer
119 KeepLabels(const TImageType * input,
120 typename TImageType::PixelType BG,
121 typename TImageType::PixelType FG,
122 typename TImageType::PixelType firstKeep,
123 typename TImageType::PixelType lastKeep,
125 //--------------------------------------------------------------------
128 //--------------------------------------------------------------------
129 template<class TImageType>
130 typename TImageType::Pointer
131 LabelizeAndSelectLabels(const TImageType * input,
132 typename TImageType::PixelType BG,
133 typename TImageType::PixelType FG,
134 bool isFullyConnected,
135 int minimalComponentSize,
136 LabelizeParameters<typename TImageType::PixelType> * param);
138 //--------------------------------------------------------------------
139 template<class ImageType>
140 typename ImageType::Pointer
141 ResizeImageLike(const ImageType * input,
142 const itk::ImageBase<ImageType::ImageDimension> * like,
143 typename ImageType::PixelType BG);
146 //--------------------------------------------------------------------
147 template<class MaskImageType>
148 typename MaskImageType::Pointer
149 SliceBySliceRelativePosition(const MaskImageType * input,
150 const MaskImageType * object,
153 std::string orientation,
154 bool uniqueConnectedComponent=false,
156 bool autocropflag=true,
157 bool singleObjectCCL=true);
159 //--------------------------------------------------------------------
160 // In a binary image, search for the point belonging to the FG that
161 // is the most exterma in the direction 'direction' (or in the
162 // opposite if notFlag is given).
163 template<class ImageType>
165 FindExtremaPointInAGivenDirection(const ImageType * input,
166 typename ImageType::PixelType bg,
167 int direction, bool opposite,
168 typename ImageType::PointType & p);
170 //--------------------------------------------------------------------
172 //--------------------------------------------------------------------
173 // Same as above but check that the found point is not more than
174 // 'distanceMax' away from 'refPoint'
175 template<class ImageType>
177 FindExtremaPointInAGivenDirection(const ImageType * input,
178 typename ImageType::PixelType bg,
179 int direction, bool opposite,
180 typename ImageType::PointType refPoint,
182 typename ImageType::PointType & p);
184 //--------------------------------------------------------------------
186 //--------------------------------------------------------------------
187 template<class ImageType>
188 typename ImageType::Pointer
189 CropImageAlongOneAxis(const ImageType * image,
190 int dim, double min, double max,
191 bool autoCrop = false,
192 typename ImageType::PixelType BG=0);
193 template<class ImageType>
194 typename ImageType::Pointer
195 CropImageAbove(const ImageType * image,
196 int dim, double min, bool autoCrop = false,
197 typename ImageType::PixelType BG=0);
198 template<class ImageType>
199 typename ImageType::Pointer
200 CropImageBelow(const ImageType * image,
201 int dim, double max,bool autoCrop = false,
202 typename ImageType::PixelType BG=0);
203 //--------------------------------------------------------------------
206 //--------------------------------------------------------------------
207 template<class ImageType>
209 ComputeCentroids(const ImageType * image,
210 typename ImageType::PixelType BG,
211 std::vector<typename ImageType::PointType> & centroids);
212 template<class ImageType>
214 ComputeCentroids2(const ImageType * image,
215 typename ImageType::PixelType BG,
216 std::vector<typename ImageType::PointType> & centroids);
217 //--------------------------------------------------------------------
220 //--------------------------------------------------------------------
221 template<class ImageType>
223 ExtractSlices(const ImageType * image, int dim,
224 std::vector< typename itk::Image<typename ImageType::PixelType,
225 ImageType::ImageDimension-1>::Pointer > & slices);
226 //--------------------------------------------------------------------
229 //--------------------------------------------------------------------
230 template<class ImageType>
231 typename ImageType::Pointer
232 JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType,
233 ImageType::ImageDimension-1>::Pointer > & slices,
234 const ImageType * input,
236 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
237 typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
238 typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
239 joinFilter->SetOrigin(input->GetOrigin()[direction]);
240 joinFilter->SetSpacing(input->GetSpacing()[direction]);
241 for(unsigned int i=0; i<slices.size(); i++) {
242 joinFilter->PushBackInput(slices[i]);
244 joinFilter->Update();
245 return joinFilter->GetOutput();
247 //--------------------------------------------------------------------
250 //--------------------------------------------------------------------
251 // Set of tools to manage 3D points and 2D points in slices
252 template<class ImageType>
255 typedef typename ImageType::PointType PointType3D;
256 typedef typename ImageType::IndexType IndexType3D;
257 typedef typename ImageType::PixelType PixelType;
258 typedef typename ImageType::Pointer ImagePointer;
259 typedef typename ImageType::ConstPointer ImageConstPointer;
260 typedef itk::Image<PixelType, 2> SliceType;
261 typedef typename SliceType::PointType PointType2D;
262 typedef typename SliceType::IndexType IndexType2D;
264 typedef std::map<int, PointType2D> MapPoint2DType;
265 typedef std::vector<PointType3D> VectorPoint3DType;
266 typedef std::vector<PointType2D> VectorPoint2DType;
269 static void Convert2DTo3D(const PointType2D & p2D,
270 const ImageType * image,
273 static void Convert2DMapTo3DList(const MapPoint2DType & map,
274 const ImageType * image,
275 VectorPoint3DType & list);
276 static void Convert2DListTo3DList(const VectorPoint2DType & p,
278 const ImageType * image,
279 VectorPoint3DType & list);
282 //--------------------------------------------------------------------
283 template<class ImageType>
285 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
286 std::string filename);
287 //--------------------------------------------------------------------
290 //--------------------------------------------------------------------
291 template<class ImageType>
292 typename ImageType::Pointer
293 Dilate(const ImageType * image, double radiusInMM,
294 typename ImageType::PixelType BG,
295 typename ImageType::PixelType FG,
297 template<class ImageType>
298 typename ImageType::Pointer
299 Dilate(const ImageType * image, typename ImageType::SizeType radius,
300 typename ImageType::PixelType BG,
301 typename ImageType::PixelType FG,
303 template<class ImageType>
304 typename ImageType::Pointer
305 Dilate(const ImageType * image, typename ImageType::PointType radiusInMM,
306 typename ImageType::PixelType BG,
307 typename ImageType::PixelType FG,
309 //--------------------------------------------------------------------
311 //--------------------------------------------------------------------
312 template<class ValueType, class VectorType>
313 void ConvertOption(std::string optionName, uint given,
314 ValueType * values, VectorType & p,
315 uint dim, bool required);
316 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \
317 ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
318 //--------------------------------------------------------------------
320 //--------------------------------------------------------------------
321 template<class ImageType>
323 SliceBySliceSetBackgroundFromLineSeparation(ImageType * input,
324 std::vector<typename ImageType::PointType> & lA,
325 std::vector<typename ImageType::PointType> & lB,
326 typename ImageType::PixelType BG,
328 double offsetToKeep);
329 //--------------------------------------------------------------------
332 //--------------------------------------------------------------------
333 template<class ImageType>
334 void AndNot(ImageType * input,
335 const ImageType * object,
336 typename ImageType::PixelType BG=0);
337 //--------------------------------------------------------------------
340 //--------------------------------------------------------------------
341 template<class ImageType>
342 typename ImageType::Pointer
343 Binarize(const ImageType * input,
344 typename ImageType::PixelType lower,
345 typename ImageType::PixelType upper,
346 typename ImageType::PixelType BG=0,
347 typename ImageType::PixelType FG=1);
348 //--------------------------------------------------------------------
351 //--------------------------------------------------------------------
352 template<class ImageType>
354 GetMinMaxPointPosition(const ImageType * input,
355 typename ImageType::PointType & min,
356 typename ImageType::PointType & max);
357 //--------------------------------------------------------------------
359 //--------------------------------------------------------------------
360 template<class ImageType>
361 typename ImageType::PointType
362 FindExtremaPointInAGivenLine(const ImageType * input,
363 int dimension, bool inverse,
364 typename ImageType::PointType p,
365 typename ImageType::PixelType BG,
367 //--------------------------------------------------------------------
370 //--------------------------------------------------------------------
371 template<class PointType>
373 IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like);
374 //--------------------------------------------------------------------
377 //--------------------------------------------------------------------
378 template<class ImageType>
380 SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input,
381 typename ImageType::PixelType BG,
383 int extremaDirection,
384 bool extremaOppositeFlag,
387 std::vector<typename ImageType::PointType> & A,
388 std::vector<typename ImageType::PointType> & B);
389 //--------------------------------------------------------------------
394 #include "clitkSegmentationUtils.txx"