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 //--------------------------------------------------------------------
80 //--------------------------------------------------------------------
81 template<class ImageType>
82 typename ImageType::Pointer
83 RemoveLabels(typename ImageType::Pointer input,
84 typename ImageType::PixelType BG,
85 std::vector<typename ImageType::PixelType> & labelsToRemove);
86 //--------------------------------------------------------------------
89 //--------------------------------------------------------------------
90 template<class ImageType>
91 typename ImageType::Pointer
92 AutoCrop(typename ImageType::Pointer input,
93 typename ImageType::PixelType BG) {
94 typedef clitk::AutoCropFilter<ImageType> AutoCropFilterType;
95 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
96 autoCropFilter->SetInput(input);
97 autoCropFilter->SetBackgroundValue(BG);
98 autoCropFilter->Update();
99 return autoCropFilter->GetOutput();
101 //--------------------------------------------------------------------
104 //--------------------------------------------------------------------
105 template<class TImageType>
106 typename TImageType::Pointer
107 KeepLabels(const TImageType * input,
108 typename TImageType::PixelType BG,
109 typename TImageType::PixelType FG,
110 typename TImageType::PixelType firstKeep,
111 typename TImageType::PixelType lastKeep,
113 //--------------------------------------------------------------------
116 //--------------------------------------------------------------------
117 template<class TImageType>
118 typename TImageType::Pointer
119 LabelizeAndSelectLabels(typename TImageType::Pointer input,
120 typename TImageType::PixelType BG,
121 typename TImageType::PixelType FG,
122 bool isFullyConnected,
123 int minimalComponentSize,
124 LabelizeParameters<typename TImageType::PixelType> * param);
126 //--------------------------------------------------------------------
127 template<class ImageType>
128 typename ImageType::Pointer
129 ResizeImageLike(typename ImageType::Pointer input,
130 typename ImageType::Pointer like,
131 typename ImageType::PixelType BG);
134 //--------------------------------------------------------------------
135 template<class MaskImageType>
136 typename MaskImageType::Pointer
137 SliceBySliceRelativePosition(const MaskImageType * input,
138 const MaskImageType * object,
141 std::string orientation,
142 bool uniqueConnectedComponent=false,
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(typename ImageType::Pointer 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 CropImageAbove(typename ImageType::Pointer image,
184 bool autoCrop = false,
185 typename ImageType::PixelType BG=0);
186 template<class ImageType>
187 typename ImageType::Pointer
188 CropImageBelow(typename ImageType::Pointer image,
190 bool autoCrop = false,
191 typename ImageType::PixelType BG=0);
192 //--------------------------------------------------------------------
195 //--------------------------------------------------------------------
196 template<class ImageType>
198 ComputeCentroids(typename ImageType::Pointer image,
199 typename ImageType::PixelType BG,
200 std::vector<typename ImageType::PointType> & centroids);
201 //--------------------------------------------------------------------
204 //--------------------------------------------------------------------
205 template<class ImageType>
207 ExtractSlices(typename ImageType::Pointer image,
209 std::vector< typename itk::Image<typename ImageType::PixelType,
210 ImageType::ImageDimension-1>::Pointer > & slices);
211 //--------------------------------------------------------------------
214 //--------------------------------------------------------------------
215 template<class ImageType>
216 typename ImageType::Pointer
217 JoinSlices(std::vector<typename itk::Image<typename ImageType::PixelType,
218 ImageType::ImageDimension-1>::Pointer > & slices,
219 typename ImageType::Pointer input,
221 //--------------------------------------------------------------------
224 //--------------------------------------------------------------------
225 // Set of tools to manage 3D points and 2D points in slices
226 template<class ImageType>
229 typedef typename ImageType::PointType PointType3D;
230 typedef typename ImageType::PixelType PixelType;
231 typedef typename ImageType::Pointer ImagePointer;
232 typedef typename ImageType::ConstPointer ImageConstPointer;
233 typedef itk::Image<PixelType, 2> SliceType;
234 typedef typename SliceType::PointType PointType2D;
236 typedef std::map<int, PointType2D> MapPoint2DType;
237 typedef std::vector<PointType3D> VectorPoint3DType;
239 static void Convert2DTo3D(const PointType2D & p2D,
243 static void Convert2DTo3DList(const MapPoint2DType & map,
245 VectorPoint3DType & list);
248 //--------------------------------------------------------------------
249 template<class ImageType>
251 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
252 std::string filename);
253 //--------------------------------------------------------------------
256 //--------------------------------------------------------------------
257 template<class ImageType>
258 typename ImageType::Pointer
259 Dilate(typename ImageType::Pointer image,
261 typename ImageType::PixelType BG,
262 typename ImageType::PixelType FG,
264 template<class ImageType>
265 typename ImageType::Pointer
266 Dilate(typename ImageType::Pointer image,
267 typename ImageType::SizeType radius,
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::PointType radiusInMM,
275 typename ImageType::PixelType BG,
276 typename ImageType::PixelType FG,
278 //--------------------------------------------------------------------
280 //--------------------------------------------------------------------
281 template<class ValueType, class VectorType>
282 void ConvertOption(std::string optionName, uint given,
283 ValueType * values, VectorType & p,
284 uint dim, bool required);
285 #define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \
286 ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED);
290 #include "clitkSegmentationUtils.txx"