Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkCLUtil.h
Go to the documentation of this file.
1 #ifndef mitkCLUtil_h
2 #define mitkCLUtil_h
3 
4 #include <Eigen/Dense>
6 #include <itkImageRegionIterator.h>
7 
8 
9 #include <mitkImage.h>
10 #include <mitkImageCast.h>
11 #include <mitkITKImageImport.h>
12 
13 #include <itkConnectedComponentImageFilter.h>
14 namespace mitk
15 {
16 
18 {
19 public:
24  {
25  Axial,Coronal,Sagital,All
26  };
27 
33  static void CreateCheckerboardMask(mitk::Image::Pointer image, mitk::Image::Pointer & outimage);
34 
40  static void InterpolateCheckerboardPrediction(mitk::Image::Pointer checkerboard_prediction, mitk::Image::Pointer & checkerboard_mask, mitk::Image::Pointer & outimage);
41 
47  static void CountVoxel(mitk::Image::Pointer image, std::map<unsigned int, unsigned int> & map);
48 
55  static void CountVoxel(mitk::Image::Pointer image, unsigned int label, unsigned int & count);
56 
62  static void CountVoxel(mitk::Image::Pointer image, unsigned int & count);
63 
71  static void SumVoxelForLabel(mitk::Image::Pointer image, const mitk::Image::Pointer & source , unsigned int label, double & val );
72 
80  static void SqSumVoxelForLabel(mitk::Image::Pointer image, const mitk::Image::Pointer & source, unsigned int label, double & val );
81 
87  static void LogicalAndImages(const Image::Pointer &image1, const Image::Pointer &image2, Image::Pointer &outimage);
88 
89 
96  static void GaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer & smoothed ,double sigma);
97 
104  template<typename TMatrixElementType>
105  static mitk::Image::Pointer Transform(const Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> & matrix, const mitk::Image::Pointer & mask)
106  {
108  mitk::CastToItkImage(mask,itkMask);
109 
111  itk_img->SetRegions(itkMask->GetLargestPossibleRegion());
112  itk_img->SetOrigin(itkMask->GetOrigin());
113  itk_img->SetSpacing(itkMask->GetSpacing());
114  itk_img->SetDirection(itkMask->GetDirection());
115  itk_img->Allocate();
116 
117 
118  unsigned int n_numSamples = 0;
119  mitk::CLUtil::CountVoxel(mask,n_numSamples);
120 
121  if(n_numSamples != matrix.rows())
122  MITK_ERROR << "Number of samples in matrix and number of points under the masks is not the same!";
123 
124  auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(itkMask, itkMask->GetLargestPossibleRegion());
125  auto oit = itk::ImageRegionIterator<itk::Image<TMatrixElementType, 3> >(itk_img, itk_img->GetLargestPossibleRegion());
126 
127  unsigned int current_row = 0;
128  while(!mit.IsAtEnd())
129  {
130  if(mit.Value() > 0)
131  oit.Set(matrix(current_row++,0));
132  else
133  oit.Set(0.0);
134  ++mit;
135  ++oit;
136  }
137 
139  mitk::GrabItkImageMemory(itk_img,out_img);
140  return out_img;
141  }
148  template<typename TMatrixElementType>
149  static Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> Transform(const mitk::Image::Pointer & img, const mitk::Image::Pointer & mask)
150  {
152  mitk::CastToItkImage(mask,current_mask);
153 
154  unsigned int n_numSamples = 0;
155  mitk::CLUtil::CountVoxel(mask,n_numSamples);
156 
157  typename itk::Image<TMatrixElementType, 3>::Pointer current_img;
158  mitk::CastToItkImage(img,current_img);
159 
160  Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> out_matrix(n_numSamples,1);
161 
162  auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(current_mask, current_mask->GetLargestPossibleRegion());
163  auto iit = itk::ImageRegionConstIterator<itk::Image<TMatrixElementType, 3> >(current_img,current_img->GetLargestPossibleRegion());
164  unsigned int current_row = 0;
165  while (!mit.IsAtEnd()) {
166  if(mit.Value() > 0)
167  out_matrix(current_row++) = iit.Value();
168  ++mit;
169  ++iit;
170  }
171 
172  return out_matrix;
173  }
174 
182  static void DilateBinary(mitk::Image::Pointer & sourceImage, mitk::Image::Pointer& resultImage, int radius , MorphologicalDimensions d);
183 
191  static void ErodeBinary(mitk::Image::Pointer & sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
192 
200  static void ClosingBinary(mitk::Image::Pointer & sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
201 
202 
208  static void MergeLabels(mitk::Image::Pointer & img, const std::map<unsigned int, unsigned int> & map);
209 
217  static void ConnectedComponentsImage(mitk::Image::Pointer & image, mitk::Image::Pointer& mask, mitk::Image::Pointer &outimage, unsigned int& num_components);
218 
225  static void GrabLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & outimage, unsigned int label);
226 
227 
234  static void InsertLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & maskImage, unsigned int label);
235 
243  static void ErodeGrayscale(mitk::Image::Pointer & image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer & outimage );
244 
252  static void DilateGrayscale(mitk::Image::Pointer & image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer & outimage );
253 
259  static void FillHoleGrayscale(mitk::Image::Pointer & image, mitk::Image::Pointer & outimage);
260 
268  static void ProbabilityMap(const mitk::Image::Pointer& sourceImage, double mean, double std_dev, mitk::Image::Pointer& resultImage);
269 
270  template<class TImageType>
271  static void itkCountVoxel( TImageType * image, std::map<unsigned int, unsigned int> & map)
272  {
273  auto it = itk::ImageRegionIterator< TImageType >(image,image->GetLargestPossibleRegion());
274  while(!it.IsAtEnd())
275  {
276  if(map.find(it.Value()) == map.end())
277  map[it.Value()] = 0;
278  map[it.Value()]++;
279  ++it;
280  }
281  }
282 
283  template <class TImageType>
284  static void itkCountVoxel(TImageType* image, typename TImageType::PixelType label, unsigned int & count )
285  {
286  itk::ImageRegionConstIterator<TImageType> inputIter(image, image->GetLargestPossibleRegion());
287  while(!inputIter.IsAtEnd())
288  {
289  if(inputIter.Value() == label) ++count;
290  ++inputIter;
291  }
292  }
293 
294  template<typename TImageType>
295  static inline void itkCountVoxel(TImageType * mask, unsigned int & n_numSamples)
296  {
297  auto mit = itk::ImageRegionConstIterator<TImageType>(mask, mask->GetLargestPossibleRegion());
298  while (!mit.IsAtEnd())
299  {
300  if(mit.Value() > 0)
301  n_numSamples++;
302  ++mit;
303  }
304  }
305 
306  template <class TImageType1, class TImageType2>
307  static void itkSampleLabel(TImageType1* image, TImageType2* output, double acceptrate, unsigned int label)
308  {
309  std::srand (time(NULL));
310 
311  itk::ImageRegionConstIterator< TImageType1 > inputIter(image, image->GetLargestPossibleRegion());
312  itk::ImageRegionIterator< TImageType2 > outputIter(output, output->GetLargestPossibleRegion());
313 
314  while (!inputIter.IsAtEnd())
315  {
316  double r = (double)(rand()) / RAND_MAX;
317  if(inputIter.Get() == label && r < acceptrate)
318  outputIter.Set(label);
319 
320  ++inputIter;
321  ++outputIter;
322  }
323  }
324 
325  template <class TImageType>
326  static void itkSampleLabel(TImageType* image, mitk::Image::Pointer & output, unsigned int n_samples_drawn)
327  {
328  std::srand (time(NULL));
329 
330  typename TImageType::Pointer itk_out = TImageType::New();
331  itk_out->SetRegions(image->GetLargestPossibleRegion());
332  itk_out->SetDirection(image->GetDirection());
333  itk_out->SetOrigin(image->GetOrigin());
334  itk_out->SetSpacing(image->GetSpacing());
335  itk_out->Allocate();
336  itk_out->FillBuffer(0);
337 
338  itk::ImageRegionConstIterator< TImageType > inputIter(image, image->GetLargestPossibleRegion());
339  itk::ImageRegionIterator< TImageType > outputIter(itk_out, itk_out->GetLargestPossibleRegion());
340 
341  for(unsigned int i = 0 ; i < n_samples_drawn ;)
342  {
343  double r = (double)(rand()) / RAND_MAX;
344  if(inputIter.Value() != 0 && r < 0.01 && outputIter.Value() == 0)
345  {
346  outputIter.Set(inputIter.Value());
347  i++;
348  }
349  ++inputIter;
350  ++outputIter;
351 
352  if(inputIter.IsAtEnd())
353  {
354  inputIter.GoToBegin();
355  outputIter.GoToBegin();
356  }
357 
358  }
359 
360  mitk::CastToMitkImage(itk_out, output);
361  }
362 
363 private:
364 
365  template<class TImageType>
366  static void itkErodeGrayscale(TImageType * image, mitk::Image::Pointer & outimage , unsigned int radius, mitk::CLUtil::MorphologicalDimensions d);
367 
368  template<class TImageType>
369  static void itkDilateGrayscale(TImageType * image, mitk::Image::Pointer & outimage , unsigned int radius, mitk::CLUtil::MorphologicalDimensions d);
370 
371  template<class TImageType>
372  static void itkFillHoleGrayscale(TImageType * image, mitk::Image::Pointer & outimage);
373 
374  template< typename TImageType >
375  static void itkInsertLabel(TImageType * maskImage, mitk::Image::Pointer & outimage, unsigned int label)
376  {
377 
378  typename TImageType::Pointer itk_out;
379 
380  if(outimage.IsNull()) // create if necessary
381  {
382  MITK_INFO << "Initialize new image";
383  itk_out = TImageType::New();
384  itk_out->SetSpacing(maskImage->GetSpacing());
385  itk_out->SetDirection(maskImage->GetDirection());
386  itk_out->SetOrigin(maskImage->GetOrigin());
387  itk_out->SetRegions(maskImage->GetLargestPossibleRegion());
388  itk_out->Allocate();
389  itk_out->FillBuffer(0);
390  }else
391  {
392  mitk::CastToItkImage(outimage, itk_out);
393  }
394 
395  itk::ImageRegionIterator<TImageType> oit(itk_out,itk_out->GetLargestPossibleRegion());
396  itk::ImageRegionConstIterator<TImageType> mit(maskImage,maskImage->GetLargestPossibleRegion());
397 
398  while(!mit.IsAtEnd())
399  {
400  if(mit.Value() != 0)
401  {
402  oit.Set(label);
403  }
404  ++oit;
405  ++mit;
406  }
407 
408  mitk::CastToMitkImage(itk_out,outimage);
409  }
410 
411 
412  template< typename TImageType >
413  static void itkGrabLabel(TImageType * image, mitk::Image::Pointer & outimage, unsigned int label)
414  {
415  typedef itk::Image<unsigned short, 3> TOutType;
416  TOutType::Pointer itk_out = TOutType::New();
417  itk_out->SetRegions(image->GetLargestPossibleRegion());
418  itk_out->SetDirection(image->GetDirection());
419  itk_out->SetOrigin(image->GetOrigin());
420  itk_out->SetSpacing(image->GetSpacing());
421  itk_out->Allocate();
422 
423  itk::ImageRegionConstIterator<TImageType> iit(image, image->GetLargestPossibleRegion());
424  itk::ImageRegionIterator<TOutType> oit(itk_out,itk_out->GetLargestPossibleRegion());
425 
426  while(!iit.IsAtEnd())
427  {
428  if(iit.Value() == static_cast<typename TImageType::PixelType>(label))
429  oit.Set(1);
430  else
431  oit.Set(0);
432 
433  ++iit;
434  ++oit;
435  }
436 
437  mitk::CastToMitkImage(itk_out, outimage);
438  }
439 
440  template<class TImagetype>
441  static void itkMergeLabels(TImagetype * img, const std::map<unsigned int, unsigned int> & map)
442  {
443 
444  auto it = itk::ImageRegionIterator<TImagetype>(img,img->GetLargestPossibleRegion());
445 
446  while(!it.IsAtEnd())
447  {
448  if(map.find(it.Value())!=map.end())
449  it.Set( map.at(it.Value()) );
450  ++it;
451  }
452 
453  }
454 
455  template<typename TImageType>
456  static void itkConnectedComponentsImage(TImageType * image, mitk::Image::Pointer& mask, mitk::Image::Pointer &outimage, unsigned int& num_components)
457  {
458  typedef itk::Image<unsigned short, 3> MaskImageType;
459  MaskImageType::Pointer itk_mask;
460  if(mask.IsNull())
461  {
462  itk_mask = MaskImageType::New();
463  itk_mask->SetRegions(image->GetLargestPossibleRegion());
464  itk_mask->SetDirection(image->GetDirection());
465  itk_mask->SetOrigin(image->GetOrigin());
466  itk_mask->SetSpacing(image->GetSpacing());
467  itk_mask->Allocate();
468  itk_mask->FillBuffer(1);
469  }else{
470  mitk::CastToItkImage(mask,itk_mask);
471  }
472 
473  typedef itk::ConnectedComponentImageFilter<TImageType, MaskImageType, MaskImageType > FilterType;
474  typename FilterType::Pointer cc_filter = FilterType::New();
475  cc_filter->SetMaskImage(itk_mask.GetPointer());
476  cc_filter->SetInput(image);
477  cc_filter->SetBackgroundValue(0);
478  cc_filter->Update();
479 
480  num_components = cc_filter->GetObjectCount();
481  mitk::CastToMitkImage(cc_filter->GetOutput(), outimage);
482  }
483 
484  template< typename TImageType >
485  static void itkCreateCheckerboardMask(TImageType * image, mitk::Image::Pointer & outimage);
486 
487  template< typename TImageType >
488  static void itkInterpolateCheckerboardPrediction(TImageType * checkerboard_prediction, mitk::Image::Pointer & checkerboard_mask, mitk::Image::Pointer & outimage);
489 
490  template <class TImageType>
491  static void itkSumVoxelForLabel(TImageType* image, const mitk::Image::Pointer & source , typename TImageType::PixelType label, double & val );
492 
493  template <class TImageType>
494  static void itkSqSumVoxelForLabel(TImageType* image, const mitk::Image::Pointer & source, typename TImageType::PixelType label, double & val );
495 
496  template<typename TStructuringElement>
497  static void itkFitStructuringElement(TStructuringElement & se, MorphologicalDimensions d, int radius);
498 
499  template<typename TImageType>
500  static void itkDilateBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int radius , MorphologicalDimensions d);
501 
502  template<typename TImageType>
503  static void itkErodeBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
504 
505  template<typename TImageType>
506  static void itkClosingBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
507 
508  template<typename TPixel, unsigned int VDimension>
509  static void itkFillHolesBinary(itk::Image<TPixel, VDimension>* sourceImage, mitk::Image::Pointer& resultImage);
510 
511  template<typename TImageType>
512  static void itkLogicalAndImages(const TImageType * image1, const mitk::Image::Pointer & image2, mitk::Image::Pointer & outimage);
513 
514  template<class TImageType>
515  static void itkGaussianFilter(TImageType * image, mitk::Image::Pointer & smoothed ,double sigma);
516 
517  template<typename TImageType>
518  static void itkProbabilityMap(const TImageType * sourceImage, double mean, double std_dev, mitk::Image::Pointer& resultImage);
519 
520 
521 
522 };
523 
524 } //namespace MITK
525 
526 #endif
itk::SmartPointer< Self > Pointer
MorphologicalDimensions
The MorphologicalDimensions enum.
Definition: mitkCLUtil.h:23
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static void itkSampleLabel(TImageType1 *image, TImageType2 *output, double acceptrate, unsigned int label)
Definition: mitkCLUtil.h:307
DataCollection - Class to facilitate loading/accessing structured data.
static mitk::Image::Pointer Transform(const Eigen::Matrix< TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic > &matrix, const mitk::Image::Pointer &mask)
transform
Definition: mitkCLUtil.h:105
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
void GaussianFilter(itk::Image< TPixel, VImageDimension > *itkImage, double variance, mitk::Image::Pointer &output)
static Eigen::Matrix< TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic > Transform(const mitk::Image::Pointer &img, const mitk::Image::Pointer &mask)
TransformImageToMatrix.
Definition: mitkCLUtil.h:149
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:36
static void itkCountVoxel(TImageType *image, std::map< unsigned int, unsigned int > &map)
Definition: mitkCLUtil.h:271
static void itkCountVoxel(TImageType *image, typename TImageType::PixelType label, unsigned int &count)
Definition: mitkCLUtil.h:284
#define MITKCLUTILITIES_EXPORT
static Pointer New()
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.
unsigned short PixelType
static void CountVoxel(mitk::Image::Pointer image, std::map< unsigned int, unsigned int > &map)
CountVoxel.
Definition: mitkCLUtil.cpp:69
static void itkSampleLabel(TImageType *image, mitk::Image::Pointer &output, unsigned int n_samples_drawn)
Definition: mitkCLUtil.h:326
static void itkCountVoxel(TImageType *mask, unsigned int &n_numSamples)
Definition: mitkCLUtil.h:295
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.