Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
Medical Imaging Interaction Toolkit
mitkHistogramStatisticsCalculator.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 #include <stdexcept>
15 
16 namespace mitk {
17 
19  m_Uniformity(0), m_UPP(0), m_Entropy(0), m_Median(0), m_StatisticsCalculated(false)
20 {
21 
22 }
23 
24 void HistogramStatisticsCalculator::SetHistogram(HistogramType::Pointer histogram)
25 {
26  if (m_Histogram != histogram)
27  {
28  m_Histogram = histogram;
29  m_StatisticsCalculated = false;
30  }
31 }
32 
34 {
35  if (!m_StatisticsCalculated)
36  {
37  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
39  }
40  return m_Entropy;
41 }
42 
44 {
45  if (!m_StatisticsCalculated)
46  {
47  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
49  }
50  return m_Median;
51 }
52 
54 {
55  if (!m_StatisticsCalculated)
56  {
57  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
59  }
60  return m_Uniformity;
61 }
62 
64 {
65  if (!m_StatisticsCalculated)
66  {
67  MITK_WARN("Statistics have not yet been calculated, running calculation now...");
69  }
70  return m_UPP;
71 }
72 
74 {
75  if (m_Histogram.IsNull())
76  {
77  throw std::runtime_error("Histogram not set in HistogramStatisticsCalculator::CalculateStatistics()");
78  }
79 
80  unsigned int nBins = m_Histogram->GetSize()[0];
81  m_Uniformity = 0;
82  m_Entropy = 0;
83  m_UPP = 0;
84  m_Median = 0;
85 
86  MeasurementType cumulativeProbability = 0.0;
87  bool medianFound(false);
88 
89  for (unsigned int i = 0; i < nBins; i++)
90  {
91  MeasurementType partialProbability = m_Histogram->GetFrequency(i, 0) / double( m_Histogram->GetTotalFrequency() );
92  cumulativeProbability += partialProbability;
93 
94  if (partialProbability != 0)
95  {
96  m_Entropy -= partialProbability * (std::log2( partialProbability ));
97  m_Uniformity += std::pow(partialProbability, 2);
98 
99  if (m_Histogram->GetMeasurement(i, 0) > 0)
100  {
101  m_UPP += std::pow(partialProbability, 2);
102  }
103 
104  }
105 
106  if (cumulativeProbability >= 0.5 && !medianFound)
107  {
108  MeasurementType binMin = m_Histogram->GetBinMin(0, i);
109  MeasurementType binMax = m_Histogram->GetBinMax(0, i);
110  m_Median = (binMax + binMin) / 2.0;
111  medianFound = true;
112  }
113 
114  }
115  m_StatisticsCalculated = true;
116 }
117 
118 }
119 
DataCollection - Class to facilitate loading/accessing structured data.
void SetHistogram(HistogramType::Pointer histogram)
SetHistogram requires a itk::Statistics::Histogram<double>
#define MITK_WARN
Definition: mitkLogMacros.h:19