Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkMaskCleaningOperation.cpp
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 
14 
15 #include <mitkImage.h>
16 #include <mitkImageAccessByItk.h>
17 #include <mitkImageCast.h>
18 
19 #include <itkImage.h>
20 #include <itkBinaryFunctorImageFilter.h>
21 #include <itkImageRegionConstIterator.h>
22 
23 #include "itkCastImageFilter.h"
24 
25 namespace mitk
26 {
27  namespace Functor
28  {
29  template< class TInput, class TOutput>
30  class RangeBasedMasking
31  {
32  public:
33  RangeBasedMasking() {};
34  ~RangeBasedMasking() {};
35  bool operator!=(const RangeBasedMasking &) const
36  {
37  return false;
38  }
39  bool operator==(const RangeBasedMasking & other) const
40  {
41  return !(*this != other);
42  }
43  inline TOutput operator()(const TInput & A, const TOutput & B) const
44  {
45  unsigned short erg = B;
46  if (lowerLimitOn && (A < lowerLimit))
47  {
48  erg = 0;
49  }
50  if (upperLimitOn && (upperLimit < A))
51  {
52  erg = 0;
53  }
54  return erg;
55  }
56 
57  bool lowerLimitOn = false;
58  bool upperLimitOn = false;
59  double lowerLimit = 0;
60  double upperLimit = 1;
61  };
62  }
63 }
64 
65 
66 
67 template<typename TPixel, unsigned int VImageDimension>
68 static void ExecuteRangeBasedMasking(itk::Image<TPixel, VImageDimension>* image, mitk::Image::Pointer & mask, bool lowerLimitOn, double lowerLimit, bool upperLimitOn, double upperLimit, mitk::Image::Pointer & resultImage)
69 {
70  typedef itk::Image<TPixel, VImageDimension> ImageType;
71  typedef itk::Image<unsigned short, VImageDimension> MaskImageType;
72  typedef itk::BinaryFunctorImageFilter< ImageType, MaskImageType, MaskImageType, mitk::Functor::RangeBasedMasking<TPixel, unsigned short> > DefaultFilterType;
73 
74  typename MaskImageType::Pointer itkMask = MaskImageType::New();
75  mitk::CastToItkImage(mask.GetPointer(), itkMask);
76 
77  typename DefaultFilterType::Pointer filter = DefaultFilterType::New();
78  filter->SetInput1(image);
79  filter->SetInput2(itkMask);
80  filter->GetFunctor().lowerLimitOn = lowerLimitOn;
81  filter->GetFunctor().upperLimitOn = upperLimitOn;
82  filter->GetFunctor().lowerLimit = lowerLimit;
83  filter->GetFunctor().upperLimit = upperLimit;
84  filter->Update();
85 
86  CastToMitkImage(filter->GetOutput(), resultImage);
87 }
88 
89 mitk::Image::Pointer mitk::MaskCleaningOperation::RangeBasedMasking(Image::Pointer & image, Image::Pointer & mask, bool lowerLimitOn, double lowerLimit, bool upperLimitOn, double upperLimit)
90 {
91  Image::Pointer resultImage;
92  AccessByItk_n(image, ExecuteRangeBasedMasking, (mask, lowerLimitOn, lowerLimit, upperLimitOn, upperLimit, resultImage));
93  return resultImage;
94 }
95 
96 
97 template<typename TPixel, unsigned int VImageDimension>
98 static void CalculateMeanAndStd(itk::Image<TPixel, VImageDimension>* image, mitk::Image::Pointer & mask, double &mean, double &std)
99 {
100  typedef itk::Image<TPixel, VImageDimension> ImageType;
101  typedef itk::Image<unsigned short, VImageDimension> MaskImageType;
102 
103  typename MaskImageType::Pointer itkMask = MaskImageType::New();
104  mitk::CastToItkImage(mask.GetPointer(), itkMask);
105 
106  itk::ImageRegionConstIterator<ImageType> image_iter(image, image->GetLargestPossibleRegion());
107  itk::ImageRegionConstIterator<MaskImageType> mask_iter(itkMask, itkMask->GetLargestPossibleRegion());
108 
109  unsigned int number_of_voxels = 0;
110  while (!image_iter.IsAtEnd())
111  {
112  if (mask_iter.Get() > 0)
113  {
114  mean += image_iter.Get();
115  std += image_iter.Get() * image_iter.Get();
116  ++number_of_voxels;
117  }
118 
119  ++image_iter;
120  ++mask_iter;
121  }
122  mean /= number_of_voxels;
123  std /= number_of_voxels;
124  std -= mean;
125  std = sqrt(std);
126 }
127 
128 
130 {
131  Image::Pointer resultImage;
132  double mean = 0;
133  double std = 0;
134  AccessByItk_n(image, CalculateMeanAndStd, (mask, mean, std));
135  return MaskCleaningOperation::RangeBasedMasking(image, mask, true, mean - 3 * std, true, mean + 3 * std);
136 
137 }
static Image::Pointer RangeBasedMasking(Image::Pointer &image, Image::Pointer &mask, bool lowerLimitOn=false, double lowerLimit=0, bool upperLimitOn=false, double upperLimit=1)
Limits a Mask to a given Range.
MITKCORE_EXPORT bool operator!=(const InteractionEvent &a, const InteractionEvent &b)
itk::Image< unsigned char, 3 > ImageType
static void CalculateMeanAndStd(itk::Image< TPixel, VImageDimension > *image, mitk::Image::Pointer &mask, double &mean, double &std)
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
#define AccessByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a MITK image by an ITK image with one or more parameters.
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
static void ExecuteRangeBasedMasking(itk::Image< TPixel, VImageDimension > *image, mitk::Image::Pointer &mask, bool lowerLimitOn, double lowerLimit, bool upperLimitOn, double upperLimit, mitk::Image::Pointer &resultImage)
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:32
mitk::Image::Pointer image
static Image::Pointer MaskOutlierFiltering(Image::Pointer &image, Image::Pointer &mask)
Removes outlier from a mask.
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