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>
32 //--------------------------------------------------------------------
33 template<class ImageType>
34 void ComputeBBFromImageRegion(typename ImageType::Pointer image,
35 typename ImageType::RegionType region,
36 typename itk::BoundingBox<unsigned long,
37 ImageType::ImageDimension>::Pointer bb);
39 //--------------------------------------------------------------------
40 template<int Dimension>
41 void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo,
42 typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1,
43 typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2);
45 //--------------------------------------------------------------------
46 template<class ImageType>
47 void ComputeRegionFromBB(typename ImageType::Pointer image,
48 const typename itk::BoundingBox<unsigned long,
49 ImageType::ImageDimension>::Pointer bb,
50 typename ImageType::RegionType & region);
51 //--------------------------------------------------------------------
52 template<class TInternalImageType, class TMaskInternalImageType>
53 typename TInternalImageType::Pointer
54 SetBackground(const TInternalImageType * input,
55 const TMaskInternalImageType * mask,
56 typename TMaskInternalImageType::PixelType maskBG,
57 typename TInternalImageType::PixelType outValue,
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 template<class ImageType>
64 int GetNumberOfConnectedComponentLabels(typename ImageType::Pointer input,
65 typename ImageType::PixelType BG,
66 bool isFullyConnected);
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 template<class TImageType>
72 typename TImageType::Pointer
73 Labelize(const TImageType * input,
74 typename TImageType::PixelType BG,
75 bool isFullyConnected,
76 int minimalComponentSize);
77 template<class TImageType>
78 typename TImageType::Pointer
79 LabelizeAndCountNumberOfObjects(const TImageType * input,
80 typename TImageType::PixelType BG,
81 bool isFullyConnected,
82 int minimalComponentSize,
84 //--------------------------------------------------------------------
87 //--------------------------------------------------------------------
88 template<class ImageType>
89 typename ImageType::Pointer
90 RemoveLabels(typename ImageType::Pointer input,
91 typename ImageType::PixelType BG,
92 std::vector<typename ImageType::PixelType> & labelsToRemove);
93 //--------------------------------------------------------------------
96 //--------------------------------------------------------------------
97 template<class ImageType>
98 typename ImageType::Pointer
99 AutoCrop(typename ImageType::Pointer input,
100 typename ImageType::PixelType BG) {
101 typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
102 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
103 autoCropFilter->SetInput(input);
104 autoCropFilter->SetBackgroundValue(BG);
105 autoCropFilter->Update();
106 return autoCropFilter->GetOutput();
108 //--------------------------------------------------------------------
111 //--------------------------------------------------------------------
112 template<class TImageType>
113 typename TImageType::Pointer
114 KeepLabels(const TImageType * input,
115 typename TImageType::PixelType BG,
116 typename TImageType::PixelType FG,
117 typename TImageType::PixelType firstKeep,
118 typename TImageType::PixelType lastKeep,
120 //--------------------------------------------------------------------
123 //--------------------------------------------------------------------
124 template<class TImageType>
125 typename TImageType::Pointer
126 LabelizeAndSelectLabels(typename TImageType::Pointer input,
127 typename TImageType::PixelType BG,
128 typename TImageType::PixelType FG,
129 bool isFullyConnected,
130 int minimalComponentSize,
131 LabelizeParameters<typename TImageType::PixelType> * param);
133 //--------------------------------------------------------------------
134 template<class ImageType>
135 typename ImageType::Pointer
136 ResizeImageLike(typename ImageType::Pointer input,
137 typename ImageType::Pointer like,
138 typename ImageType::PixelType BG);
141 //--------------------------------------------------------------------
142 template<class MaskImageType>
143 typename MaskImageType::Pointer
144 SliceBySliceRelativePosition(const MaskImageType * input,
145 const MaskImageType * object,
148 std::string orientation,
149 bool uniqueConnectedComponent=false,
153 //--------------------------------------------------------------------
154 // In a binary image, search for the point belonging to the FG that
155 // is the most exterma in the direction 'direction' (or in the
156 // opposite if notFlag is given).
157 template<class ImageType>
159 FindExtremaPointInAGivenDirection(const ImageType * input,
160 typename ImageType::PixelType bg,
161 int direction, bool opposite,
162 typename ImageType::PointType & p);
164 //--------------------------------------------------------------------
166 //--------------------------------------------------------------------
167 // Same as above but check that the found point is not more than
168 // 'distanceMax' away from 'refPoint'
169 template<class ImageType>
171 FindExtremaPointInAGivenDirection(const ImageType * input,
172 typename ImageType::PixelType bg,
173 int direction, bool opposite,
174 typename ImageType::PointType refPoint,
176 typename ImageType::PointType & p);
178 //--------------------------------------------------------------------
180 //--------------------------------------------------------------------
181 template<class ImageType>
182 typename ImageType::Pointer
183 CropImageAlongOneAxis(typename ImageType::Pointer image,
184 int dim, double min, double max,
185 bool autoCrop = false,
186 typename ImageType::PixelType BG=0);
187 template<class ImageType>
188 typename ImageType::Pointer
189 CropImageAbove(typename ImageType::Pointer image,
191 bool autoCrop = false,
192 typename ImageType::PixelType BG=0);
193 template<class ImageType>
194 typename ImageType::Pointer
195 CropImageBelow(typename ImageType::Pointer image,
197 bool autoCrop = false,
198 typename ImageType::PixelType BG=0);
199 //--------------------------------------------------------------------
202 //--------------------------------------------------------------------
203 template<class ImageType>
205 ComputeCentroids(typename ImageType::Pointer image,
206 typename ImageType::PixelType BG,
207 std::vector<typename ImageType::PointType> & centroids);
208 //--------------------------------------------------------------------
211 //--------------------------------------------------------------------
212 template<class ImageType>
214 ExtractSlices(typename ImageType::Pointer image,
216 std::vector< typename itk::Image<typename ImageType::PixelType,
217 ImageType::ImageDimension-1>::Pointer > & slices);
218 //--------------------------------------------------------------------
221 //--------------------------------------------------------------------
222 template<class ImageType>
223 typename ImageType::Pointer
224 JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType,
225 ImageType::ImageDimension-1>::Pointer > & slices,
226 typename ImageType::Pointer input,
228 //--------------------------------------------------------------------
231 //--------------------------------------------------------------------
232 // Set of tools to manage 3D points and 2D points in slices
233 template<class ImageType>
236 typedef typename ImageType::PointType PointType3D;
237 typedef typename ImageType::PixelType PixelType;
238 typedef typename ImageType::Pointer ImagePointer;
239 typedef typename ImageType::ConstPointer ImageConstPointer;
240 typedef itk::Image<PixelType, 2> SliceType;
241 typedef typename SliceType::PointType PointType2D;
243 typedef std::map<int, PointType2D> MapPoint2DType;
244 typedef std::vector<PointType3D> VectorPoint3DType;
246 static void Convert2DTo3D(const PointType2D & p2D,
250 static void Convert2DTo3DList(const MapPoint2DType & map,
252 VectorPoint3DType & list);
255 //--------------------------------------------------------------------
256 template<class ImageType>
258 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
259 std::string filename);
260 //--------------------------------------------------------------------
263 //--------------------------------------------------------------------
264 template<class ImageType>
265 typename ImageType::Pointer
266 Dilate(typename ImageType::Pointer image,
268 typename ImageType::PixelType BG,
269 typename ImageType::PixelType FG,
271 template<class ImageType>
272 typename ImageType::Pointer
273 Dilate(typename ImageType::Pointer image,
274 typename ImageType::SizeType radius,
275 typename ImageType::PixelType BG,
276 typename ImageType::PixelType FG,
278 template<class ImageType>
279 typename ImageType::Pointer
280 Dilate(typename ImageType::Pointer image,
281 typename ImageType::PointType radiusInMM,
282 typename ImageType::PixelType BG,
283 typename ImageType::PixelType FG,
285 //--------------------------------------------------------------------
287 //--------------------------------------------------------------------
288 template<class ValueType, class VectorType>
289 void ConvertOption(std::string optionName, uint given,
290 ValueType * values, VectorType & p,
291 uint dim, bool required);
292 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \
293 ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
294 //--------------------------------------------------------------------
296 //--------------------------------------------------------------------
297 template<class ImageType>
299 SliceBySliceSetBackgroundFromLineSeparation(typename ImageType::Pointer input,
300 std::vector<typename ImageType::PointType> & lA,
301 std::vector<typename ImageType::PointType> & lB,
302 typename ImageType::PixelType BG,
304 double offsetToKeep);
309 #include "clitkSegmentationUtils.txx"