Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkHistogramStatisticsCalculator.cpp
Go to the documentation of this file.
2 #include <stdexcept>
3 
4 namespace mitk {
5 
7  m_StatisticsCalculated(false)
8 {
9 
10 }
11 
13 {
14  if (m_Histogram != histogram)
15  {
16  m_Histogram = histogram;
17  m_StatisticsCalculated = false;
18  }
19 }
20 
22 {
23  if (!m_StatisticsCalculated)
24  {
25  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
27  }
28  return m_Entropy;
29 }
30 
32 {
33  if (!m_StatisticsCalculated)
34  {
35  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
37  }
38  return m_Median;
39 }
40 
42 {
43  if (!m_StatisticsCalculated)
44  {
45  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
47  }
48  return m_Uniformity;
49 }
50 
52 {
53  if (!m_StatisticsCalculated)
54  {
55  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
57  }
58  return m_UPP;
59 }
60 
62 {
63  if (m_Histogram.IsNull())
64  {
65  throw std::runtime_error("Histogram not set in HistogramStatisticsCalculator::CalculateStatistics()");
66  }
67 
68  unsigned int nBins = m_Histogram->GetSize()[0];
69  m_Uniformity = 0;
70  m_Entropy = 0;
71  m_UPP = 0;
72  m_Median = 0;
73 
74  MeasurementType cumulativeProbability = 0.0;
75  MeasurementType partialProbability;
76  bool medianFound(false);
77 
78  for (unsigned int i = 0; i < nBins; i++)
79  {
80  partialProbability = m_Histogram->GetFrequency(i, 0) / double( m_Histogram->GetTotalFrequency() );
81  cumulativeProbability += partialProbability;
82 
83  if (partialProbability != 0)
84  {
85  m_Entropy -= partialProbability * (std::log2( partialProbability ));
86  m_Uniformity += std::pow(partialProbability, 2);
87 
88  if (m_Histogram->GetMeasurement(i, 0) > 0)
89  {
90  m_UPP += std::pow(partialProbability, 2);
91  }
92 
93  }
94 
95  if (cumulativeProbability >= 0.5 && !medianFound)
96  {
97  MeasurementType binMin = m_Histogram->GetBinMin(0, i);
98  MeasurementType binMax = m_Histogram->GetBinMax(0, i);
99  m_Median = (binMax + binMin) / 2.0;
100  medianFound = true;
101  }
102 
103  }
104  m_StatisticsCalculated = true;
105 }
106 
107 }
108 
itk::SmartPointer< Self > Pointer
DataCollection - Class to facilitate loading/accessing structured data.
void SetHistogram(HistogramType::Pointer histogram)
SetHistogram requires a itk::Statistics::Histogram
#define MITK_WARN
Definition: mitkLogMacros.h:23