Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkCLUtil.cpp
Go to the documentation of this file.
1 #ifndef _mitkCLUtil_HXX
2 #define _mitkCLUtil_HXX
3 
4 #include <mitkCLUtil.h>
5 
6 #include <mitkImageAccessByItk.h>
7 
8 
9 
10 #include <Eigen/Dense>
11 #include <itkImage.h>
12 
13 // itk includes
14 #include <itkCheckerBoardImageFilter.h>
15 #include <itkShapedNeighborhoodIterator.h>
16 // Morphologic Operations
17 #include <itkBinaryBallStructuringElement.h>
18 #include <itkBinaryDilateImageFilter.h>
19 #include <itkBinaryErodeImageFilter.h>
20 #include <itkBinaryFillholeImageFilter.h>
21 #include <itkBinaryMorphologicalClosingImageFilter.h>
22 #include <itkGrayscaleErodeImageFilter.h>
23 #include <itkGrayscaleDilateImageFilter.h>
24 #include <itkGrayscaleFillholeImageFilter.h>
25 
26 // Image Filter
27 #include <itkDiscreteGaussianImageFilter.h>
28 
29 void mitk::CLUtil::ProbabilityMap(const mitk::Image::Pointer & image , double mean, double stddev, mitk::Image::Pointer & outimage)
30 {
31  AccessFixedDimensionByItk_3(image, mitk::CLUtil::itkProbabilityMap, 3, mean, stddev, outimage);
32 }
33 
35 {
36  AccessFixedDimensionByItk_3(image, mitk::CLUtil::itkErodeGrayscale, 3, outimage, radius, d);
37 }
38 
40 {
41  AccessFixedDimensionByItk_3(image, mitk::CLUtil::itkDilateGrayscale, 3, outimage, radius, d);
42 }
43 
45 {
46  AccessFixedDimensionByItk_1(image, mitk::CLUtil::itkFillHoleGrayscale, 3, outimage);
47 }
48 
49 void mitk::CLUtil::InsertLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & maskImage, unsigned int label)
50 {
51  AccessByItk_2(image, mitk::CLUtil::itkInsertLabel, maskImage, label);
52 }
53 
54 void mitk::CLUtil::GrabLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & outimage, unsigned int label)
55 {
56  AccessFixedDimensionByItk_2(image, mitk::CLUtil::itkGrabLabel, 3, outimage, label);
57 }
58 
60 {
61  AccessFixedDimensionByItk_3(image, mitk::CLUtil::itkConnectedComponentsImage,3, mask, outimage, num_components);
62 }
63 
64 void mitk::CLUtil::MergeLabels(mitk::Image::Pointer & img, const std::map<unsigned int, unsigned int> & map)
65 {
66  AccessByItk_1(img, mitk::CLUtil::itkMergeLabels, map);
67 }
68 
69 void mitk::CLUtil::CountVoxel(mitk::Image::Pointer image, std::map<unsigned int, unsigned int> & map)
70 {
72 }
73 
74 void mitk::CLUtil::CountVoxel(mitk::Image::Pointer image, unsigned int label, unsigned int & count)
75 {
76  AccessByItk_2(image, mitk::CLUtil::itkCountVoxel, label, count);
77 }
78 
79 void mitk::CLUtil::CountVoxel(mitk::Image::Pointer image, unsigned int & count)
80 {
82 }
83 
85 {
86  AccessFixedDimensionByItk_1(image, mitk::CLUtil::itkCreateCheckerboardMask,3, outimage);
87 }
88 
90 {
91  AccessFixedDimensionByItk_2(image1,itkLogicalAndImages, 3, image2, outimage);
92 
93 }
94 
96 {
97  AccessFixedDimensionByItk_2(checkerboard_prediction, mitk::CLUtil::itkInterpolateCheckerboardPrediction,3, checkerboard_mask, outimage);
98 }
99 
101 {
102  AccessFixedDimensionByItk_2(image, mitk::CLUtil::itkGaussianFilter,3, smoothed, sigma);
103 }
104 
105 
107 {
108  AccessFixedDimensionByItk_3(sourceImage, mitk::CLUtil::itkDilateBinary, 3, resultImage, factor, d);
109 }
110 
111 
113 {
114  AccessFixedDimensionByItk_3(sourceImage, mitk::CLUtil::itkErodeBinary, 3, resultImage, factor, d);
115 }
116 
117 
119 {
120  AccessFixedDimensionByItk_3(sourceImage, mitk::CLUtil::itkClosingBinary, 3, resultImage, factor, d);
121 }
122 
123 template<typename TImageType>
124 void mitk::CLUtil::itkProbabilityMap(const TImageType * sourceImage, double mean, double std_dev, mitk::Image::Pointer& resultImage)
125 {
127  itk_img->SetRegions(sourceImage->GetLargestPossibleRegion());
128  itk_img->SetOrigin(sourceImage->GetOrigin());
129  itk_img->SetSpacing(sourceImage->GetSpacing());
130  itk_img->SetDirection(sourceImage->GetDirection());
131  itk_img->Allocate();
132 
133 
134  itk::ImageRegionConstIterator<TImageType> it(sourceImage,sourceImage->GetLargestPossibleRegion());
135  itk::ImageRegionIterator<itk::Image<double, 3> > outit(itk_img,itk_img->GetLargestPossibleRegion());
136 
137  while(!it.IsAtEnd())
138  {
139  double x = it.Value();
140 
141  double prob = (1.0/(std_dev*std::sqrt(2.0*M_PI))) * std::exp(-(((x-mean)*(x-mean))/(2.0*std_dev*std_dev)));
142  outit.Set(prob);
143  ++it;
144  ++outit;
145  }
146 
147  mitk::CastToMitkImage(itk_img, resultImage);
148 }
149 
150 template< typename TImageType >
151 void mitk::CLUtil::itkInterpolateCheckerboardPrediction(TImageType * checkerboard_prediction, Image::Pointer &checkerboard_mask, mitk::Image::Pointer & outimage)
152 {
153  typename TImageType::Pointer itk_checkerboard_mask;
154  mitk::CastToItkImage(checkerboard_mask,itk_checkerboard_mask);
155 
156  typename TImageType::Pointer itk_outimage = TImageType::New();
157  itk_outimage->SetRegions(checkerboard_prediction->GetLargestPossibleRegion());
158  itk_outimage->SetDirection(checkerboard_prediction->GetDirection());
159  itk_outimage->SetOrigin(checkerboard_prediction->GetOrigin());
160  itk_outimage->SetSpacing(checkerboard_prediction->GetSpacing());
161  itk_outimage->Allocate();
162  itk_outimage->FillBuffer(0);
163 
164  //typedef typename itk::ShapedNeighborhoodIterator<TImageType>::SizeType SizeType;
165  typedef itk::Size<3> SizeType;
166  SizeType size;
167  size.Fill(1);
168  itk::ShapedNeighborhoodIterator<TImageType> iit(size,checkerboard_prediction,checkerboard_prediction->GetLargestPossibleRegion());
169  itk::ShapedNeighborhoodIterator<TImageType> mit(size,itk_checkerboard_mask,itk_checkerboard_mask->GetLargestPossibleRegion());
170  itk::ImageRegionIterator<TImageType> oit(itk_outimage,itk_outimage->GetLargestPossibleRegion());
171 
172  typedef typename itk::ShapedNeighborhoodIterator<TImageType>::OffsetType OffsetType;
173  OffsetType offset;
174  offset.Fill(0);
175  offset[0] = 1; // {1,0,0}
176  iit.ActivateOffset(offset);
177  mit.ActivateOffset(offset);
178  offset[0] = -1; // {-1,0,0}
179  iit.ActivateOffset(offset);
180  mit.ActivateOffset(offset);
181  offset[0] = 0; offset[1] = 1; //{0,1,0}
182  iit.ActivateOffset(offset);
183  mit.ActivateOffset(offset);
184  offset[1] = -1; //{0,-1,0}
185  iit.ActivateOffset(offset);
186  mit.ActivateOffset(offset);
187 
188  // iit.ActivateOffset({{0,0,1}});
189  // iit.ActivateOffset({{0,0,-1}});
190  // mit.ActivateOffset({{0,0,1}});
191  // mit.ActivateOffset({{0,0,-1}});
192 
193  while(!iit.IsAtEnd())
194  {
195  if(mit.GetCenterPixel() == 0)
196  {
197  typename TImageType::PixelType mean = 0;
198  for (auto i = iit.Begin(); ! i.IsAtEnd(); i++)
199  { mean += i.Get(); }
200 
201 
202  //std::sort(list.begin(),list.end(),[](const typename TImageType::PixelType x,const typename TImageType::PixelType y){return x<=y;});
203 
204  oit.Set((mean+0.5)/6.0);
205  }
206  else
207  {
208  oit.Set(iit.GetCenterPixel());
209  }
210  ++iit;
211  ++mit;
212  ++oit;
213  }
214 
215  mitk::CastToMitkImage(itk_outimage,outimage);
216 }
217 
218 template< typename TImageType >
219 void mitk::CLUtil::itkCreateCheckerboardMask(TImageType * image, mitk::Image::Pointer & outimage)
220 {
221  typename TImageType::Pointer zeroimg = TImageType::New();
222  zeroimg->SetRegions(image->GetLargestPossibleRegion());
223  zeroimg->SetDirection(image->GetDirection());
224  zeroimg->SetOrigin(image->GetOrigin());
225  zeroimg->SetSpacing(image->GetSpacing());
226 
227  zeroimg->Allocate();
228  zeroimg->FillBuffer(0);
229 
230  typedef itk::CheckerBoardImageFilter<TImageType> FilterType;
231  typename FilterType::Pointer filter = FilterType::New();
232  filter->SetInput1(image);
233  filter->SetInput2(zeroimg);
234  typename FilterType::PatternArrayType pattern;
235  pattern.SetElement(0,(image->GetLargestPossibleRegion().GetSize()[0]));
236  pattern.SetElement(1,(image->GetLargestPossibleRegion().GetSize()[1]));
237  pattern.SetElement(2,(image->GetLargestPossibleRegion().GetSize()[2]));
238  filter->SetCheckerPattern(pattern);
239 
240  filter->Update();
241  mitk::CastToMitkImage(filter->GetOutput(), outimage);
242 }
243 
244 
245 template <class TImageType>
246 void mitk::CLUtil::itkSumVoxelForLabel(TImageType* image, const mitk::Image::Pointer & source , typename TImageType::PixelType label, double & val )
247 {
249  mitk::CastToItkImage(source,itk_source);
250 
251  itk::ImageRegionConstIterator<TImageType> inputIter(image, image->GetLargestPossibleRegion());
252  itk::ImageRegionConstIterator< itk::Image<double,3> > sourceIter(itk_source, itk_source->GetLargestPossibleRegion());
253  while(!inputIter.IsAtEnd())
254  {
255  if(inputIter.Value() == label) val += sourceIter.Value();
256  ++inputIter;
257  ++sourceIter;
258  }
259 }
260 
261 template <class TImageType>
262 void mitk::CLUtil::itkSqSumVoxelForLabel(TImageType* image, const mitk::Image::Pointer & source, typename TImageType::PixelType label, double & val )
263 {
265  mitk::CastToItkImage(source,itk_source);
266 
267  itk::ImageRegionConstIterator<TImageType> inputIter(image, image->GetLargestPossibleRegion());
268  itk::ImageRegionConstIterator< itk::Image<double,3> > sourceIter(itk_source, itk_source->GetLargestPossibleRegion());
269  while(!inputIter.IsAtEnd())
270  {
271  if(inputIter.Value() == label) val += sourceIter.Value() * sourceIter.Value();
272  ++inputIter;
273  ++sourceIter;
274  }
275 }
276 
277 template<typename TStructuringElement>
278 void mitk::CLUtil::itkFitStructuringElement(TStructuringElement & se, MorphologicalDimensions d, int factor)
279 {
280  typename TStructuringElement::SizeType size;
281  size.Fill(factor);
282  switch(d)
283  {
284  case(All):
285  case(Axial):
286  size.SetElement(2,0);
287  break;
288  case(Sagital):
289  size.SetElement(0,0);
290  break;
291  case(Coronal):
292  size.SetElement(1,0);
293  break;
294  }
295  se.SetRadius(size);
296  se.CreateStructuringElement();
297 }
298 
299 template<typename TImageType>
300 void mitk::CLUtil::itkClosingBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int factor, MorphologicalDimensions d)
301 {
302  typedef itk::BinaryBallStructuringElement<typename TImageType::PixelType, 3> BallType;
303  typedef itk::BinaryMorphologicalClosingImageFilter<TImageType, TImageType, BallType> FilterType;
304 
305  BallType strElem;
306  itkFitStructuringElement(strElem,d,factor);
307 
308  typename FilterType::Pointer erodeFilter = FilterType::New();
309  erodeFilter->SetKernel(strElem);
310  erodeFilter->SetInput(sourceImage);
311  erodeFilter->SetForegroundValue(1);
312  erodeFilter->Update();
313 
314  mitk::CastToMitkImage(erodeFilter->GetOutput(), resultImage);
315 
316 }
317 
318 template<typename TImageType>
319 void mitk::CLUtil::itkDilateBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int factor, MorphologicalDimensions d)
320 {
321  typedef itk::BinaryBallStructuringElement<typename TImageType::PixelType, 3> BallType;
322  typedef typename itk::BinaryDilateImageFilter<TImageType, TImageType, BallType> BallDilateFilterType;
323 
324  BallType strElem;
325  itkFitStructuringElement(strElem,d,factor);
326 
328  erodeFilter->SetKernel(strElem);
329  erodeFilter->SetInput(sourceImage);
330  erodeFilter->SetDilateValue(1);
331  erodeFilter->Update();
332 
333  mitk::CastToMitkImage(erodeFilter->GetOutput(), resultImage);
334 
335 }
336 
337 template<typename TImageType>
338 void mitk::CLUtil::itkErodeBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int factor, MorphologicalDimensions d)
339 {
340  typedef itk::BinaryBallStructuringElement<typename TImageType::PixelType, 3> BallType;
341  typedef typename itk::BinaryErodeImageFilter<TImageType, TImageType, BallType> BallErodeFilterType;
342 
343  BallType strElem;
344  itkFitStructuringElement(strElem,d,factor);
345 
346 
348  erodeFilter->SetKernel(strElem);
349  erodeFilter->SetInput(sourceImage);
350  erodeFilter->SetErodeValue(1);
351 // erodeFilter->UpdateLargestPossibleRegion();
352  erodeFilter->Update();
353 
354  mitk::CastToMitkImage(erodeFilter->GetOutput(), resultImage);
355 
356 }
357 
363 template<typename TPixel, unsigned int VDimension>
364 void mitk::CLUtil::itkFillHolesBinary(itk::Image<TPixel, VDimension>* sourceImage, mitk::Image::Pointer& resultImage)
365 {
366  typedef itk::Image<TPixel, VDimension> ImageType;
367  typedef typename itk::BinaryFillholeImageFilter<ImageType> FillHoleFilterType;
368 
369  typename FillHoleFilterType::Pointer fillHoleFilter = FillHoleFilterType::New();
370  fillHoleFilter->SetInput(sourceImage);
371  fillHoleFilter->SetForegroundValue(1);
372  fillHoleFilter->Update();
373 
374  mitk::CastToMitkImage(fillHoleFilter->GetOutput(), resultImage);
375 }
376 
382 template<typename TImageType>
383 void mitk::CLUtil::itkLogicalAndImages(const TImageType * image1, const mitk::Image::Pointer & image2, mitk::Image::Pointer & outimage)
384 {
385 
386  typename TImageType::Pointer itk_outimage = TImageType::New();
387  itk_outimage->SetRegions(image1->GetLargestPossibleRegion());
388  itk_outimage->SetDirection(image1->GetDirection());
389  itk_outimage->SetOrigin(image1->GetOrigin());
390  itk_outimage->SetSpacing(image1->GetSpacing());
391 
392  itk_outimage->Allocate();
393  itk_outimage->FillBuffer(0);
394 
395  typename TImageType::Pointer itk_image2;
396  mitk::CastToItkImage(image2,itk_image2);
397 
398  itk::ImageRegionConstIterator<TImageType> it1(image1, image1->GetLargestPossibleRegion());
399  itk::ImageRegionConstIterator<TImageType> it2(itk_image2, itk_image2->GetLargestPossibleRegion());
400  itk::ImageRegionIterator<TImageType> oit(itk_outimage,itk_outimage->GetLargestPossibleRegion());
401 
402  while(!it1.IsAtEnd())
403  {
404  if(it1.Value() == 0 || it2.Value() == 0)
405  {
406  oit.Set(0);
407  }else
408  oit.Set(it1.Value());
409  ++it1;
410  ++it2;
411  ++oit;
412  }
413 
414  mitk::CastToMitkImage(itk_outimage, outimage);
415 }
416 
423 template<class TImageType>
424 void mitk::CLUtil::itkGaussianFilter(TImageType * image, mitk::Image::Pointer & smoothed ,double sigma)
425 {
426  typedef itk::DiscreteGaussianImageFilter<TImageType,TImageType> FilterType;
427  typename FilterType::Pointer filter = FilterType::New();
428  filter->SetInput(image);
429  filter->SetVariance(sigma);
430  filter->Update();
431 
432  mitk::CastToMitkImage(filter->GetOutput(),smoothed);
433 }
434 
435 template<class TImageType>
436 void mitk::CLUtil::itkErodeGrayscale(TImageType * image, mitk::Image::Pointer & outimage , unsigned int radius, mitk::CLUtil::MorphologicalDimensions d)
437 {
438  typedef itk::BinaryBallStructuringElement<typename TImageType::PixelType, 3> StructureElementType;
439  typedef itk::GrayscaleErodeImageFilter<TImageType,TImageType,StructureElementType> FilterType;
440 
441  StructureElementType ball;
442  itkFitStructuringElement(ball,d, radius);
443 
444  typename FilterType::Pointer filter = FilterType::New();
445  filter->SetKernel(ball);
446  filter->SetInput(image);
447  filter->Update();
448 
449  mitk::CastToMitkImage(filter->GetOutput(),outimage);
450 }
451 
452 template<class TImageType>
453 void mitk::CLUtil::itkDilateGrayscale(TImageType * image, mitk::Image::Pointer & outimage , unsigned int radius, mitk::CLUtil::MorphologicalDimensions d)
454 {
455  typedef itk::BinaryBallStructuringElement<typename TImageType::PixelType, 3> StructureElementType;
456  typedef itk::GrayscaleDilateImageFilter<TImageType,TImageType,StructureElementType> FilterType;
457 
458  StructureElementType ball;
459  itkFitStructuringElement(ball,d, radius);
460 
461  typename FilterType::Pointer filter = FilterType::New();
462  filter->SetKernel(ball);
463  filter->SetInput(image);
464  filter->Update();
465 
466  mitk::CastToMitkImage(filter->GetOutput(),outimage);
467 }
468 
469 template<class TImageType>
470 void mitk::CLUtil::itkFillHoleGrayscale(TImageType * image, mitk::Image::Pointer & outimage)
471 {
472  typedef itk::GrayscaleFillholeImageFilter<TImageType,TImageType> FilterType;
473 
474  typename FilterType::Pointer filter = FilterType::New();
475  filter->SetInput(image);
476  filter->Update();
477 
478  mitk::CastToMitkImage(filter->GetOutput(),outimage);
479 }
480 
481 
482 #endif
static void ConnectedComponentsImage(mitk::Image::Pointer &image, mitk::Image::Pointer &mask, mitk::Image::Pointer &outimage, unsigned int &num_components)
ConnectedComponentsImage.
Definition: mitkCLUtil.cpp:59
static void LogicalAndImages(const Image::Pointer &image1, const Image::Pointer &image2, Image::Pointer &outimage)
LogicalAndImages.
Definition: mitkCLUtil.cpp:89
itk::SmartPointer< Self > Pointer
MorphologicalDimensions
The MorphologicalDimensions enum.
Definition: mitkCLUtil.h:23
static void GrabLabel(mitk::Image::Pointer &image, mitk::Image::Pointer &outimage, unsigned int label)
GrabLabel.
Definition: mitkCLUtil.cpp:54
static void ErodeBinary(mitk::Image::Pointer &sourceImage, mitk::Image::Pointer &resultImage, int radius, MorphologicalDimensions d)
ErodeBinary.
Definition: mitkCLUtil.cpp:112
itk::SmartPointer< Self > Pointer
Definition: mitkImage.h:88
static void GaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer &smoothed, double sigma)
GaussianFilter.
Definition: mitkCLUtil.cpp:100
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
#define AccessFixedDimensionByItk_3(mitkImage, itkImageTypeFunction, dimension, arg1, arg2, arg3)
static void DilateGrayscale(mitk::Image::Pointer &image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer &outimage)
DilateGrayscale.
Definition: mitkCLUtil.cpp:39
static Vector3D offset
static void FillHoleGrayscale(mitk::Image::Pointer &image, mitk::Image::Pointer &outimage)
FillHoleGrayscale.
Definition: mitkCLUtil.cpp:44
map::core::discrete::Elements< 3 >::InternalImageType ImageType
static void DilateBinary(mitk::Image::Pointer &sourceImage, mitk::Image::Pointer &resultImage, int radius, MorphologicalDimensions d)
DilateBinary.
Definition: mitkCLUtil.cpp:106
static void ErodeGrayscale(mitk::Image::Pointer &image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer &outimage)
ErodeGrayscale.
Definition: mitkCLUtil.cpp:34
static void InterpolateCheckerboardPrediction(mitk::Image::Pointer checkerboard_prediction, mitk::Image::Pointer &checkerboard_mask, mitk::Image::Pointer &outimage)
InterpolateCreateCheckerboardPrediction.
Definition: mitkCLUtil.cpp:95
#define AccessFixedDimensionByItk_2(mitkImage, itkImageTypeFunction, dimension, arg1, arg2)
static void InsertLabel(mitk::Image::Pointer &image, mitk::Image::Pointer &maskImage, unsigned int label)
itkInsertLabel
Definition: mitkCLUtil.cpp:49
static void ProbabilityMap(const mitk::Image::Pointer &sourceImage, double mean, double std_dev, mitk::Image::Pointer &resultImage)
ProbabilityMap.
Definition: mitkCLUtil.cpp:29
static void itkCountVoxel(TImageType *image, std::map< unsigned int, unsigned int > &map)
Definition: mitkCLUtil.h:271
#define AccessFixedDimensionByItk_1(mitkImage, itkImageTypeFunction, dimension, arg1)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:78
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static void ClosingBinary(mitk::Image::Pointer &sourceImage, mitk::Image::Pointer &resultImage, int radius, MorphologicalDimensions d)
ClosingBinary.
Definition: mitkCLUtil.cpp:118
static void MergeLabels(mitk::Image::Pointer &img, const std::map< unsigned int, unsigned int > &map)
MergeLabels.
Definition: mitkCLUtil.cpp:64
unsigned short PixelType
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
static void CreateCheckerboardMask(mitk::Image::Pointer image, mitk::Image::Pointer &outimage)
CreateCheckerBoardPredictionMask.
Definition: mitkCLUtil.cpp:84
static void CountVoxel(mitk::Image::Pointer image, std::map< unsigned int, unsigned int > &map)
CountVoxel.
Definition: mitkCLUtil.cpp:69
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.