Medical Imaging Interaction Toolkit  2018.4.99-ef453c4b
Medical Imaging Interaction Toolkit
mitkIntensityQuantifier.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 // STD
16 #include <numeric>
17 
18 // ITK
19 #include <itkImageRegionConstIterator.h>
20 
21 // MITK
22 #include <mitkImageCast.h>
23 #include <mitkImageAccessByItk.h>
24 
25 template<typename TPixel, unsigned int VImageDimension>
26 static void
27 CalculateImageMinMax(itk::Image<TPixel, VImageDimension>* itkImage, double &minimum, double &maximum)
28 {
29  typedef itk::Image<TPixel, VImageDimension> ImageType;
30 
32  maximum = std::numeric_limits<TPixel>::lowest();
33 
34  itk::ImageRegionConstIterator<ImageType> iter(itkImage, itkImage->GetLargestPossibleRegion());
35 
36  while (!iter.IsAtEnd())
37  {
38  minimum = std::min<TPixel>(minimum, iter.Get());
39  maximum = std::max<TPixel>(maximum, iter.Get());
40  ++iter;
41  }
42 }
43 
44 template<typename TPixel, unsigned int VImageDimension>
45 static void
46 CalculateImageRegionMinMax(itk::Image<TPixel, VImageDimension>* itkImage, mitk::Image::Pointer mask, double &minimum, double &maximum)
47 {
48  typedef itk::Image<TPixel, VImageDimension> ImageType;
49  typedef itk::Image<int, VImageDimension> MaskType;
50 
51  typename MaskType::Pointer itkMask = MaskType::New();
52  mitk::CastToItkImage(mask, itkMask);
53 
55  maximum = std::numeric_limits<TPixel>::lowest();
56 
57  itk::ImageRegionConstIterator<ImageType> iter(itkImage, itkImage->GetLargestPossibleRegion());
58  itk::ImageRegionConstIterator<MaskType> maskIter(itkMask, itkMask->GetLargestPossibleRegion());
59 
60  while (!iter.IsAtEnd())
61  {
62  if (maskIter.Get() > 0)
63  {
64  minimum = std::min<TPixel>(minimum, iter.Get());
65  maximum = std::max<TPixel>(maximum, iter.Get());
66  }
67  ++iter;
68  ++maskIter;
69  }
70 }
71 
73  m_Initialized(false),
74  m_Bins(0),
75  m_Binsize(0),
76  m_Minimum(0),
77  m_Maximum(0)
78 {}
79 
80 void mitk::IntensityQuantifier::InitializeByMinimumMaximum(double minimum, double maximum, unsigned int bins) {
81  m_Minimum = minimum;
82  m_Maximum = maximum;
83  m_Bins = bins;
84  m_Binsize = (maximum - minimum) / bins;
85  m_Initialized = true;
86 }
87 
88 void mitk::IntensityQuantifier::InitializeByBinsizeAndBins(double minimum, unsigned int bins, double binsize) {
89  m_Minimum = minimum;
90  m_Maximum = minimum + bins*binsize;
91  m_Bins = bins;
92  m_Binsize = binsize;
93  m_Initialized = true;
94 }
95 
96 void mitk::IntensityQuantifier::InitializeByBinsizeAndMaximum(double minimum, double maximum, double binsize) {
97  m_Minimum = minimum;
98  m_Bins = std::ceil((maximum - minimum) / binsize);
99  m_Maximum = minimum + m_Bins*binsize;
100  m_Binsize = binsize;
101  m_Initialized = true;
102 }
103 
105  double minimum, maximum;
106  AccessByItk_2(image, CalculateImageMinMax, minimum, maximum);
107  InitializeByMinimumMaximum(minimum, maximum, bins);
108 }
109 
111  double tmp, maximum;
112  AccessByItk_2(image, CalculateImageMinMax, tmp, maximum);
113  InitializeByMinimumMaximum(minimum, maximum, bins);
114 }
115 
117  double minimum, tmp;
118  AccessByItk_2(image, CalculateImageMinMax, minimum, tmp);
119  InitializeByMinimumMaximum(minimum, maximum, bins);
120 }
121 
123  double minimum, maximum;
124  AccessByItk_3(image, CalculateImageRegionMinMax, mask, minimum, maximum);
125  InitializeByMinimumMaximum(minimum, maximum, bins);
126 }
127 
129  double tmp, maximum;
130  AccessByItk_3(image, CalculateImageRegionMinMax, mask, tmp, maximum);
131  InitializeByMinimumMaximum(minimum, maximum, bins);
132 }
133 
135  double minimum, tmp;
136  AccessByItk_3(image, CalculateImageRegionMinMax, mask, minimum, tmp);
137  InitializeByMinimumMaximum(minimum, maximum, bins);
138 }
139 
141  double minimum, maximum;
142  AccessByItk_2(image, CalculateImageMinMax, minimum, maximum);
143  InitializeByBinsizeAndMaximum(minimum, maximum, binsize);
144 }
145 
147  double tmp, maximum;
148  AccessByItk_2(image, CalculateImageMinMax, tmp, maximum);
149  InitializeByBinsizeAndMaximum(minimum, maximum, binsize);
150 }
151 
153  double minimum, tmp;
154  AccessByItk_2(image, CalculateImageMinMax, minimum, tmp);
155  InitializeByBinsizeAndMaximum(minimum, maximum, binsize);
156 }
157 
159  double minimum, maximum;
160  AccessByItk_3(image, CalculateImageRegionMinMax, mask, minimum, maximum);
161  InitializeByBinsizeAndMaximum(minimum, maximum, binsize);
162 }
163 
165  double tmp, maximum;
166  AccessByItk_3(image, CalculateImageRegionMinMax, mask, tmp, maximum);
167  InitializeByBinsizeAndMaximum(minimum, maximum, binsize);
168 }
169 
171  double minimum, tmp;
172  AccessByItk_3(image, CalculateImageRegionMinMax, mask, minimum, tmp);
173  InitializeByBinsizeAndMaximum(minimum, maximum, binsize);
174 }
175 
176 unsigned int mitk::IntensityQuantifier::IntensityToIndex(double intensity)
177 {
178  double index = std::floor((intensity - m_Minimum) / m_Binsize);
179  return std::max<double>(0, std::min<double>(index, m_Bins-1));
180 }
181 
183 {
184  return index*m_Binsize + m_Minimum;
185 }
186 
188 {
189  return (index + 0.5) * m_Binsize + m_Minimum;
190 }
191 
193 {
194  return (index + 1) * m_Binsize + m_Minimum;
195 }
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
double IndexToMinimumIntensity(unsigned int index)
itk::Image< unsigned char, 3 > ImageType
void InitializeByImage(mitk::Image::Pointer image, unsigned int bins)
void InitializeByImageAndMaximum(mitk::Image::Pointer image, double maximum, unsigned int bins)
double IndexToMaximumIntensity(unsigned int index)
double IndexToMeanIntensity(unsigned int index)
void InitializeByMinimumMaximum(double minimum, double maximum, unsigned int bins)
void InitializeByImageAndBinsizeAndMaximum(mitk::Image::Pointer image, double maximum, double binsize)
void InitializeByBinsizeAndBins(double minimum, unsigned int bins, double binsize)
void InitializeByImageRegion(mitk::Image::Pointer image, mitk::Image::Pointer mask, unsigned int bins)
void InitializeByImageRegionAndBinsize(mitk::Image::Pointer image, mitk::Image::Pointer mask, double binsize)
unsigned int IntensityToIndex(double intensity)
static T max(T x, T y)
Definition: svm.cpp:56
mitk::Image::Pointer image
void InitializeByImageRegionAndBinsizeAndMinimum(mitk::Image::Pointer image, mitk::Image::Pointer mask, double minimum, double binsize)
static void CalculateImageMinMax(itk::Image< TPixel, VImageDimension > *itkImage, double &minimum, double &maximum)
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 CalculateImageRegionMinMax(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer mask, double &minimum, double &maximum)
void InitializeByImageRegionAndBinsizeAndMaximum(mitk::Image::Pointer image, mitk::Image::Pointer mask, double maximum, double binsize)
mitk::Image::Pointer mask
void InitializeByImageRegionAndMinimum(mitk::Image::Pointer image, mitk::Image::Pointer mask, double minimum, unsigned int bins)
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
void InitializeByBinsizeAndMaximum(double minimum, double maximum, double binsize)
void InitializeByImageAndMinimum(mitk::Image::Pointer image, double minimum, unsigned int bins)
void InitializeByImageAndBinsizeAndMinimum(mitk::Image::Pointer image, double minimum, double binsize)
void InitializeByImageRegionAndMaximum(mitk::Image::Pointer image, mitk::Image::Pointer mask, double maximum, unsigned int bins)
void InitializeByImageAndBinsize(mitk::Image::Pointer image, double binsize)