Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
mitkCLUtil.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkCLUtil_h
14 #define mitkCLUtil_h
15 
16 #include <Eigen/Dense>
17 #include <MitkCLUtilitiesExports.h>
18 #include <itkImageRegionIterator.h>
19 
20 
21 #include <mitkImage.h>
22 #include <mitkImageCast.h>
23 #include <mitkITKImageImport.h>
24 
25 #include <itkConnectedComponentImageFilter.h>
26 namespace mitk
27 {
28 
30 {
31 public:
36  {
37  Axial,Coronal,Sagital,All
38  };
39 
45  static void CreateCheckerboardMask(mitk::Image::Pointer image, mitk::Image::Pointer & outimage);
46 
52  static void InterpolateCheckerboardPrediction(mitk::Image::Pointer checkerboard_prediction, mitk::Image::Pointer & checkerboard_mask, mitk::Image::Pointer & outimage);
53 
59  static void CountVoxel(mitk::Image::Pointer image, std::map<unsigned int, unsigned int> & map);
60 
67  static void CountVoxel(mitk::Image::Pointer image, unsigned int label, unsigned int & count);
68 
74  static void CountVoxel(mitk::Image::Pointer image, unsigned int & count);
75 
83  static void SumVoxelForLabel(mitk::Image::Pointer image, const mitk::Image::Pointer & source , unsigned int label, double & val );
84 
92  static void SqSumVoxelForLabel(mitk::Image::Pointer image, const mitk::Image::Pointer & source, unsigned int label, double & val );
93 
99  static void LogicalAndImages(const Image::Pointer &image1, const Image::Pointer &image2, Image::Pointer &outimage);
100 
101 
108  static void GaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer & smoothed ,double sigma);
109 
117  static void DifferenceOfGaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer & smoothed, double sigma1, double sigma2);
118 
126  static void LaplacianOfGaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer & smoothed, double sigma1);
127 
135  static void HessianOfGaussianFilter(mitk::Image::Pointer image, std::vector<mitk::Image::Pointer> &out, double sigma);
136 
144  static void LocalHistogram(mitk::Image::Pointer image, std::vector<mitk::Image::Pointer> &out, int Bins, int NeighbourhoodSize);
145 
152  template<typename TMatrixElementType>
153  static mitk::Image::Pointer Transform(const Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> & matrix, const mitk::Image::Pointer & mask)
154  {
155  itk::Image<unsigned int, 3>::Pointer itkMask;
156  mitk::CastToItkImage(mask,itkMask);
157 
158  typename itk::Image<TMatrixElementType, 3>::Pointer itk_img = itk::Image<TMatrixElementType, 3>::New();
159  itk_img->SetRegions(itkMask->GetLargestPossibleRegion());
160  itk_img->SetOrigin(itkMask->GetOrigin());
161  itk_img->SetSpacing(itkMask->GetSpacing());
162  itk_img->SetDirection(itkMask->GetDirection());
163  itk_img->Allocate();
164 
165 
166  unsigned int n_numSamples = 0;
167  mitk::CLUtil::CountVoxel(mask,n_numSamples);
168 
169  if(n_numSamples != matrix.rows())
170  MITK_ERROR << "Number of samples in matrix and number of points under the masks is not the same!";
171 
172  auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(itkMask, itkMask->GetLargestPossibleRegion());
173  auto oit = itk::ImageRegionIterator<itk::Image<TMatrixElementType, 3> >(itk_img, itk_img->GetLargestPossibleRegion());
174 
175  unsigned int current_row = 0;
176  while(!mit.IsAtEnd())
177  {
178  if(mit.Value() > 0)
179  oit.Set(matrix(current_row++,0));
180  else
181  oit.Set(0.0);
182  ++mit;
183  ++oit;
184  }
185 
187  mitk::GrabItkImageMemory(itk_img,out_img);
188  return out_img;
189  }
196  template<typename TMatrixElementType>
197  static Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> Transform(const mitk::Image::Pointer & img, const mitk::Image::Pointer & mask)
198  {
199  itk::Image<unsigned int, 3>::Pointer current_mask;
200  mitk::CastToItkImage(mask,current_mask);
201 
202  unsigned int n_numSamples = 0;
203  mitk::CLUtil::CountVoxel(mask,n_numSamples);
204 
205  typename itk::Image<TMatrixElementType, 3>::Pointer current_img;
206  mitk::CastToItkImage(img,current_img);
207 
208  Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> out_matrix(n_numSamples,1);
209 
210  auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(current_mask, current_mask->GetLargestPossibleRegion());
211  auto iit = itk::ImageRegionConstIterator<itk::Image<TMatrixElementType, 3> >(current_img,current_img->GetLargestPossibleRegion());
212  unsigned int current_row = 0;
213  while (!mit.IsAtEnd()) {
214  if(mit.Value() > 0)
215  out_matrix(current_row++) = iit.Value();
216  ++mit;
217  ++iit;
218  }
219 
220  return out_matrix;
221  }
222 
230  static void DilateBinary(mitk::Image::Pointer & sourceImage, mitk::Image::Pointer& resultImage, int radius , MorphologicalDimensions d);
231 
239  static void ErodeBinary(mitk::Image::Pointer & sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
240 
248  static void ClosingBinary(mitk::Image::Pointer & sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
249 
250 
256  static void MergeLabels(mitk::Image::Pointer & img, const std::map<unsigned int, unsigned int> & map);
257 
265  static void ConnectedComponentsImage(mitk::Image::Pointer & image, mitk::Image::Pointer& mask, mitk::Image::Pointer &outimage, unsigned int& num_components);
266 
273  static void GrabLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & outimage, unsigned int label);
274 
275 
282  static void InsertLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & maskImage, unsigned int label);
283 
291  static void ErodeGrayscale(mitk::Image::Pointer & image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer & outimage );
292 
300  static void DilateGrayscale(mitk::Image::Pointer & image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer & outimage );
301 
307  static void FillHoleGrayscale(mitk::Image::Pointer & image, mitk::Image::Pointer & outimage);
308 
316  static void ProbabilityMap(const mitk::Image::Pointer& sourceImage, double mean, double std_dev, mitk::Image::Pointer& resultImage);
317 
318  template<class TImageType>
319  static void itkCountVoxel( TImageType * image, std::map<unsigned int, unsigned int> & map)
320  {
321  auto it = itk::ImageRegionIterator< TImageType >(image,image->GetLargestPossibleRegion());
322  while(!it.IsAtEnd())
323  {
324  if(map.find(it.Value()) == map.end())
325  map[it.Value()] = 0;
326  map[it.Value()]++;
327  ++it;
328  }
329  }
330 
331  template <class TImageType>
332  static void itkCountVoxel(TImageType* image, typename TImageType::PixelType label, unsigned int & count )
333  {
334  itk::ImageRegionConstIterator<TImageType> inputIter(image, image->GetLargestPossibleRegion());
335  while(!inputIter.IsAtEnd())
336  {
337  if(inputIter.Value() == label) ++count;
338  ++inputIter;
339  }
340  }
341 
342  template<typename TImageType>
343  static inline void itkCountVoxel(TImageType * mask, unsigned int & n_numSamples)
344  {
345  auto mit = itk::ImageRegionConstIterator<TImageType>(mask, mask->GetLargestPossibleRegion());
346  while (!mit.IsAtEnd())
347  {
348  if(mit.Value() > 0)
349  n_numSamples++;
350  ++mit;
351  }
352  }
353 
354  template <class TImageType1, class TImageType2>
355  static void itkSampleLabel(TImageType1* image, TImageType2* output, double acceptrate, unsigned int label)
356  {
357  std::srand (time(nullptr));
358 
359  itk::ImageRegionConstIterator< TImageType1 > inputIter(image, image->GetLargestPossibleRegion());
360  itk::ImageRegionIterator< TImageType2 > outputIter(output, output->GetLargestPossibleRegion());
361 
362  while (!inputIter.IsAtEnd())
363  {
364  double r = (double)(rand()) / RAND_MAX;
365  if(inputIter.Get() == label && r < acceptrate)
366  outputIter.Set(label);
367 
368  ++inputIter;
369  ++outputIter;
370  }
371  }
372 
373  template <class TImageType>
374  static void itkSampleLabel(TImageType* image, mitk::Image::Pointer & output, unsigned int n_samples_drawn)
375  {
376  std::srand (time(nullptr));
377 
378  typename TImageType::Pointer itk_out = TImageType::New();
379  itk_out->SetRegions(image->GetLargestPossibleRegion());
380  itk_out->SetDirection(image->GetDirection());
381  itk_out->SetOrigin(image->GetOrigin());
382  itk_out->SetSpacing(image->GetSpacing());
383  itk_out->Allocate();
384  itk_out->FillBuffer(0);
385 
386  itk::ImageRegionConstIterator< TImageType > inputIter(image, image->GetLargestPossibleRegion());
387  itk::ImageRegionIterator< TImageType > outputIter(itk_out, itk_out->GetLargestPossibleRegion());
388 
389  for(unsigned int i = 0 ; i < n_samples_drawn ;)
390  {
391  double r = (double)(rand()) / RAND_MAX;
392  if(inputIter.Value() != 0 && r < 0.01 && outputIter.Value() == 0)
393  {
394  outputIter.Set(inputIter.Value());
395  i++;
396  }
397  ++inputIter;
398  ++outputIter;
399 
400  if(inputIter.IsAtEnd())
401  {
402  inputIter.GoToBegin();
403  outputIter.GoToBegin();
404  }
405 
406  }
407 
408  mitk::CastToMitkImage(itk_out, output);
409  }
410 
411 private:
412 
413  template<class TImageType>
414  static void itkErodeGrayscale(TImageType * image, mitk::Image::Pointer & outimage , unsigned int radius, mitk::CLUtil::MorphologicalDimensions d);
415 
416  template<class TImageType>
417  static void itkDilateGrayscale(TImageType * image, mitk::Image::Pointer & outimage , unsigned int radius, mitk::CLUtil::MorphologicalDimensions d);
418 
419  template<class TImageType>
420  static void itkFillHoleGrayscale(TImageType * image, mitk::Image::Pointer & outimage);
421 
422  template< typename TImageType >
423  static void itkInsertLabel(TImageType * maskImage, mitk::Image::Pointer & outimage, unsigned int label)
424  {
425 
426  typename TImageType::Pointer itk_out;
427 
428  if(outimage.IsNull()) // create if necessary
429  {
430  MITK_INFO << "Initialize new image";
431  itk_out = TImageType::New();
432  itk_out->SetSpacing(maskImage->GetSpacing());
433  itk_out->SetDirection(maskImage->GetDirection());
434  itk_out->SetOrigin(maskImage->GetOrigin());
435  itk_out->SetRegions(maskImage->GetLargestPossibleRegion());
436  itk_out->Allocate();
437  itk_out->FillBuffer(0);
438  }else
439  {
440  mitk::CastToItkImage(outimage, itk_out);
441  }
442 
443  itk::ImageRegionIterator<TImageType> oit(itk_out,itk_out->GetLargestPossibleRegion());
444  itk::ImageRegionConstIterator<TImageType> mit(maskImage,maskImage->GetLargestPossibleRegion());
445 
446  while(!mit.IsAtEnd())
447  {
448  if(mit.Value() != 0)
449  {
450  oit.Set(label);
451  }
452  ++oit;
453  ++mit;
454  }
455 
456  mitk::CastToMitkImage(itk_out,outimage);
457  }
458 
459 
460  template< typename TImageType >
461  static void itkGrabLabel(TImageType * image, mitk::Image::Pointer & outimage, unsigned int label)
462  {
463  typedef itk::Image<unsigned short, 3> TOutType;
464  TOutType::Pointer itk_out = TOutType::New();
465  itk_out->SetRegions(image->GetLargestPossibleRegion());
466  itk_out->SetDirection(image->GetDirection());
467  itk_out->SetOrigin(image->GetOrigin());
468  itk_out->SetSpacing(image->GetSpacing());
469  itk_out->Allocate();
470 
471  itk::ImageRegionConstIterator<TImageType> iit(image, image->GetLargestPossibleRegion());
472  itk::ImageRegionIterator<TOutType> oit(itk_out,itk_out->GetLargestPossibleRegion());
473 
474  while(!iit.IsAtEnd())
475  {
476  if(iit.Value() == static_cast<typename TImageType::PixelType>(label))
477  oit.Set(1);
478  else
479  oit.Set(0);
480 
481  ++iit;
482  ++oit;
483  }
484 
485  mitk::CastToMitkImage(itk_out, outimage);
486  }
487 
488  template<class TImagetype>
489  static void itkMergeLabels(TImagetype * img, const std::map<unsigned int, unsigned int> & map)
490  {
491 
492  auto it = itk::ImageRegionIterator<TImagetype>(img,img->GetLargestPossibleRegion());
493 
494  while(!it.IsAtEnd())
495  {
496  if(map.find(it.Value())!=map.end())
497  it.Set( map.at(it.Value()) );
498  ++it;
499  }
500 
501  }
502 
503  template<typename TImageType>
504  static void itkConnectedComponentsImage(TImageType * image, mitk::Image::Pointer& mask, mitk::Image::Pointer &outimage, unsigned int& num_components)
505  {
506  typedef itk::Image<unsigned short, 3> MaskImageType;
507  MaskImageType::Pointer itk_mask;
508  if(mask.IsNull())
509  {
510  itk_mask = MaskImageType::New();
511  itk_mask->SetRegions(image->GetLargestPossibleRegion());
512  itk_mask->SetDirection(image->GetDirection());
513  itk_mask->SetOrigin(image->GetOrigin());
514  itk_mask->SetSpacing(image->GetSpacing());
515  itk_mask->Allocate();
516  itk_mask->FillBuffer(1);
517  }else{
518  mitk::CastToItkImage(mask,itk_mask);
519  }
520 
521  typedef itk::ConnectedComponentImageFilter<TImageType, MaskImageType, MaskImageType > FilterType;
522  typename FilterType::Pointer cc_filter = FilterType::New();
523  cc_filter->SetMaskImage(itk_mask.GetPointer());
524  cc_filter->SetInput(image);
525  cc_filter->SetBackgroundValue(0);
526  cc_filter->Update();
527 
528  num_components = cc_filter->GetObjectCount();
529  mitk::CastToMitkImage(cc_filter->GetOutput(), outimage);
530  }
531 
532  template< typename TImageType >
533  static void itkCreateCheckerboardMask(TImageType * image, mitk::Image::Pointer & outimage);
534 
535  template< typename TImageType >
536  static void itkInterpolateCheckerboardPrediction(TImageType * checkerboard_prediction, mitk::Image::Pointer & checkerboard_mask, mitk::Image::Pointer & outimage);
537 
538  template <class TImageType>
539  static void itkSumVoxelForLabel(TImageType* image, const mitk::Image::Pointer & source , typename TImageType::PixelType label, double & val );
540 
541  template <class TImageType>
542  static void itkSqSumVoxelForLabel(TImageType* image, const mitk::Image::Pointer & source, typename TImageType::PixelType label, double & val );
543 
544  template<typename TStructuringElement>
545  static void itkFitStructuringElement(TStructuringElement & se, MorphologicalDimensions d, int radius);
546 
547  template<typename TImageType>
548  static void itkDilateBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int radius , MorphologicalDimensions d);
549 
550  template<typename TImageType>
551  static void itkErodeBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
552 
553  template<typename TImageType>
554  static void itkClosingBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage, int radius, MorphologicalDimensions d);
555 
556  template<typename TPixel, unsigned int VDimension>
557  static void itkFillHolesBinary(itk::Image<TPixel, VDimension>* sourceImage, mitk::Image::Pointer& resultImage);
558 
559  template<typename TImageType>
560  static void itkLogicalAndImages(const TImageType * image1, const mitk::Image::Pointer & image2, mitk::Image::Pointer & outimage);
561 
562  template<class TImageType>
563  static void itkGaussianFilter(TImageType * image, mitk::Image::Pointer & smoothed ,double sigma);
564 
565  template<class TImageType>
566  static void itkDifferenceOfGaussianFilter(TImageType * image, mitk::Image::Pointer & smoothed, double sigma1, double sigma2);
567 
568  template<typename TImageType>
569  static void itkProbabilityMap(const TImageType * sourceImage, double mean, double std_dev, mitk::Image::Pointer& resultImage);
570 
571  template<typename TPixel, unsigned int VImageDimension>
572  static void itkHessianOfGaussianFilter(itk::Image<TPixel, VImageDimension>* itkImage, double variance, std::vector<mitk::Image::Pointer> &out);
573 
574  template<typename TPixel, unsigned int VImageDimension>
575  static void itkLaplacianOfGaussianFilter(itk::Image<TPixel, VImageDimension>* itkImage, double variance, mitk::Image::Pointer &output);
576 
577  template<typename TPixel, unsigned int VImageDimension>
578  static void itkLocalHistograms(itk::Image<TPixel, VImageDimension>* itkImage, std::vector<mitk::Image::Pointer> &out, int size, int bins);
579 };
580 
581 } //namespace MITK
582 
583 #endif
void LaplacianOfGaussianFilter(itk::Image< TPixel, VImageDimension > *itkImage, double variance, mitk::Image::Pointer &output)
MorphologicalDimensions
The MorphologicalDimensions enum.
Definition: mitkCLUtil.h:35
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
static void itkSampleLabel(TImageType1 *image, TImageType2 *output, double acceptrate, unsigned int label)
Definition: mitkCLUtil.h:355
DataCollection - Class to facilitate loading/accessing structured data.
void HessianOfGaussianFilter(itk::Image< TPixel, VImageDimension > *itkImage, double variance, std::vector< mitk::Image::Pointer > &out)
static mitk::Image::Pointer Transform(const Eigen::Matrix< TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic > &matrix, const mitk::Image::Pointer &mask)
transform
Definition: mitkCLUtil.h:153
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:197
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:32
static void itkCountVoxel(TImageType *image, std::map< unsigned int, unsigned int > &map)
Definition: mitkCLUtil.h:319
static void itkCountVoxel(TImageType *image, typename TImageType::PixelType label, unsigned int &count)
Definition: mitkCLUtil.h:332
#define MITKCLUTILITIES_EXPORT
mitk::Image::Pointer image
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:74
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
mitk::Image::Pointer mask
static void CountVoxel(mitk::Image::Pointer image, std::map< unsigned int, unsigned int > &map)
CountVoxel.
Definition: mitkCLUtil.cpp:88
static void itkSampleLabel(TImageType *image, mitk::Image::Pointer &output, unsigned int n_samples_drawn)
Definition: mitkCLUtil.h:374
static void itkCountVoxel(TImageType *mask, unsigned int &n_numSamples)
Definition: mitkCLUtil.h:343