Medical Imaging Interaction Toolkit  2018.4.99-6aa36ba9
Medical Imaging Interaction Toolkit
mitkExtendedLabelStatisticsImageFilter.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 #ifndef __mitkExtendedLabelStatisticsImageFilter
13 #define __mitkExtendedLabelStatisticsImageFilter
14 
15 #include "itkLabelStatisticsImageFilter.h"
16 
17 namespace itk
18 {
29  template< class TInputImage, class TLabelImage >
30  class ExtendedLabelStatisticsImageFilter : public LabelStatisticsImageFilter< TInputImage, TLabelImage >
31  {
32  public:
33 
35  typedef LabelStatisticsImageFilter < TInputImage, TLabelImage > Superclass;
38  typedef typename Superclass::LabelPixelType LabelPixelType;
39  typedef typename Superclass::RealType RealType;
40  typedef typename Superclass::PixelType PixelType;
41  typedef typename Superclass::MapIterator MapIterator;
43  typedef typename Superclass::RegionType RegionType;
44  typedef itk::Statistics::Histogram<double> HistogramType;
45 
46  itkFactorylessNewMacro( Self );
47  itkCloneMacro( Self );
48  itkTypeMacro(ExtendedLabelStatisticsImageFilter, LabelStatisticsImageFilter);
49 
50 
56  {
57  public:
58 
59  // default constructor
61  {
62  // initialized to the default values
63  m_Count = NumericTraits< IdentifierType >::ZeroValue();
64  m_PositivePixelCount = NumericTraits< IdentifierType >::ZeroValue();
65  m_Sum = NumericTraits< RealType >::ZeroValue();
66  m_SumOfPositivePixels = NumericTraits< RealType >::ZeroValue();
67 
68  m_SumOfSquares = NumericTraits< RealType >::ZeroValue();
69  m_SumOfCubes = NumericTraits< RealType >::ZeroValue();
70  m_SumOfQuadruples = NumericTraits< RealType >::ZeroValue();
71 
72  // Set such that the first pixel encountered can be compared
74  m_Maximum = NumericTraits< RealType >::NonpositiveMin();
75 
76  // Default these to zero
77  m_Mean = NumericTraits< RealType >::ZeroValue();
78  m_Sigma = NumericTraits< RealType >::ZeroValue();
79  m_Variance = NumericTraits< RealType >::ZeroValue();
80  m_MPP = NumericTraits< RealType >::ZeroValue();
81  m_Median = NumericTraits< RealType >::ZeroValue();
82  m_Uniformity = NumericTraits< RealType >::ZeroValue();
83  m_UPP = NumericTraits< RealType >::ZeroValue();
84  m_Entropy = NumericTraits< RealType >::ZeroValue();
85  m_Skewness = NumericTraits< RealType >::ZeroValue();
86  m_Kurtosis = NumericTraits< RealType >::ZeroValue();
87 
88  unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
89  m_BoundingBox.resize(imageDimension * 2);
90  for ( unsigned int i = 0; i < imageDimension * 2; i += 2 )
91  {
93  m_BoundingBox[i + 1] = NumericTraits< IndexValueType >::NonpositiveMin();
94  }
95  m_Histogram = nullptr;
96  }
97 
98  // constructor with histogram enabled
99  LabelStatistics(int size, RealType lowerBound, RealType upperBound)
100  {
101  // initialized to the default values
102  m_Count = NumericTraits< IdentifierType >::ZeroValue();
103  m_PositivePixelCount = NumericTraits< IdentifierType >::ZeroValue();
104  m_Sum = NumericTraits< RealType >::ZeroValue();
105  m_SumOfPositivePixels = NumericTraits< RealType >::ZeroValue();
106 
107  m_SumOfSquares = NumericTraits< RealType >::ZeroValue();
108  m_SumOfCubes = NumericTraits< RealType >::ZeroValue();
109  m_SumOfQuadruples = NumericTraits< RealType >::ZeroValue();
110 
111  // Set such that the first pixel encountered can be compared
113  m_Maximum = NumericTraits< RealType >::NonpositiveMin();
114 
115  // Default these to zero
116  m_Mean = NumericTraits< RealType >::ZeroValue();
117  m_Sigma = NumericTraits< RealType >::ZeroValue();
118  m_Variance = NumericTraits< RealType >::ZeroValue();
119  m_MPP = NumericTraits< RealType >::ZeroValue();
120  m_Median = NumericTraits< RealType >::ZeroValue();
121  m_Uniformity = NumericTraits< RealType >::ZeroValue();
122  m_UPP = NumericTraits< RealType >::ZeroValue();
123  m_Entropy = NumericTraits< RealType >::ZeroValue();
124  m_Skewness = NumericTraits< RealType >::ZeroValue();
125  m_Kurtosis = NumericTraits< RealType >::ZeroValue();
126 
127 
128  unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
129  m_BoundingBox.resize(imageDimension * 2);
130  for ( unsigned int i = 0; i < imageDimension * 2; i += 2 )
131  {
133  m_BoundingBox[i + 1] = NumericTraits< IndexValueType >::NonpositiveMin();
134  }
135 
136  // Histogram
137  m_Histogram = HistogramType::New();
138  typename HistogramType::SizeType hsize;
139  typename HistogramType::MeasurementVectorType lb;
140  typename HistogramType::MeasurementVectorType ub;
141  hsize.SetSize(1);
142  lb.SetSize(1);
143  ub.SetSize(1);
144  m_Histogram->SetMeasurementVectorSize(1);
145  hsize[0] = size;
146  lb[0] = lowerBound;
147  ub[0] = upperBound;
148  m_Histogram->Initialize(hsize, lb, ub);
149  }
150 
151  // need copy constructor because of smart pointer to histogram
153  {
154  m_Count = l.m_Count;
155  m_Minimum = l.m_Minimum;
156  m_Maximum = l.m_Maximum;
157  m_Mean = l.m_Mean;
158  m_Sum = l.m_Sum;
160  m_Sigma = l.m_Sigma;
162  m_MPP = l.m_MPP;
163  m_Median = l.m_Median;
165  m_UPP = l.m_UPP;
166  m_Entropy = l.m_Entropy;
175  }
176 
177  // added for completeness
179  {
180  if(this != &l)
181  {
182  m_Count = l.m_Count;
183  m_Minimum = l.m_Minimum;
184  m_Maximum = l.m_Maximum;
185  m_Mean = l.m_Mean;
186  m_Sum = l.m_Sum;
188  m_Sigma = l.m_Sigma;
190  m_MPP = l.m_MPP;
191  m_Median = l.m_Median;
193  m_UPP = l.m_UPP;
194  m_Entropy = l.m_Entropy;
203  }
204  return *this;
205  }
206 
207  IdentifierType m_Count;
208  RealType m_Minimum;
209  RealType m_Maximum;
210  RealType m_Mean;
211  RealType m_Sum;
212  RealType m_SumOfSquares;
213  RealType m_Sigma;
214  RealType m_Variance;
215  RealType m_MPP;
216  RealType m_Median;
217  RealType m_Uniformity;
218  RealType m_UPP;
219  RealType m_Entropy;
220  RealType m_Skewness;
221  RealType m_Kurtosis;
222  IdentifierType m_PositivePixelCount;
224  RealType m_SumOfCubes;
227  typename HistogramType::Pointer m_Histogram;
228  };
229 
231  typedef itksys::hash_map< LabelPixelType, LabelStatistics > MapType;
232  typedef typename itksys::hash_map< LabelPixelType, LabelStatistics >::const_iterator StatisticsMapConstIterator;
233  typedef typename itksys::hash_map< LabelPixelType, LabelStatistics >::iterator StatisticsMapIterator;
234  typedef IdentifierType MapSizeType;
235 
237  typedef std::vector<LabelPixelType> ValidLabelValuesContainerType;
238 
240  RealType GetMinimum(LabelPixelType label) const;
241 
243  RealType GetMaximum(LabelPixelType label) const;
244 
246  RealType GetMean(LabelPixelType label) const;
247 
249  RealType GetSigma(LabelPixelType label) const;
250 
252  RealType GetVariance(LabelPixelType label) const;
253 
255  BoundingBoxType GetBoundingBox(LabelPixelType label) const;
256 
258  RegionType GetRegion(LabelPixelType label) const;
259 
261  RealType GetSum(LabelPixelType label) const;
262 
264  MapSizeType GetCount(LabelPixelType label) const;
265 
267  HistogramType::Pointer GetHistogram(LabelPixelType label) const;
268 
269  /*getter method for the new statistics*/
270  RealType GetSkewness(LabelPixelType label) const;
271  RealType GetKurtosis(LabelPixelType label) const;
272  RealType GetUniformity( LabelPixelType label) const;
273  RealType GetMedian( LabelPixelType label) const;
274  RealType GetEntropy( LabelPixelType label) const;
275  RealType GetMPP( LabelPixelType label) const;
276  RealType GetUPP( LabelPixelType label) const;
277 
278  bool GetMaskingNonEmpty() const;
279 
280  std::list<int> GetRelevantLabels() const;
281 
282 
284  void SetHistogramParameters(const int numBins, RealType lowerBound,
285  RealType upperBound);
286 
288  void SetHistogramParametersForLabels(std::map<LabelPixelType, unsigned int> numBins, std::map<LabelPixelType, PixelType> lowerBound,
289  std::map<LabelPixelType, PixelType> upperBound);
290 
291  protected:
293  m_GlobalHistogramParametersSet(false),
294  m_MaskNonEmpty(false),
295  m_LabelHistogramParametersSet(false),
296  m_PreferGlobalHistogramParameters(false)
297  {
298  m_NumBins.set_size(1);
299  }
300 
302 
303  void AfterThreadedGenerateData() override;
304 
306  void BeforeThreadedGenerateData() override;
307 
309  void ThreadedGenerateData(const typename TInputImage::RegionType &
310  outputRegionForThread,
311  ThreadIdType threadId) override;
312 
315  bool HasLabel(LabelPixelType label) const
316  {
317  return m_LabelStatistics.find(label) != m_LabelStatistics.end();
318  }
319 
320  private:
321  std::vector< MapType > m_LabelStatisticsPerThread;
322  MapType m_LabelStatistics;
323  ValidLabelValuesContainerType m_ValidLabelValues;
324 
325  bool m_GlobalHistogramParametersSet;
326 
327  typename HistogramType::SizeType m_NumBins;
328 
329  RealType m_LowerBound;
330  RealType m_UpperBound;
331 
332  bool m_MaskNonEmpty;
333 
334  bool m_LabelHistogramParametersSet;
335  std::map<LabelPixelType, PixelType> m_LabelMin, m_LabelMax;
336  std::map<LabelPixelType, unsigned int> m_LabelNBins;
337  bool m_PreferGlobalHistogramParameters;
338 
339  }; // end of class
340 
341 } // end namespace itk
342 
343 #ifndef ITK_MANUAL_INSTANTIATION
344 #include "mitkExtendedLabelStatisticsImageFilter.hxx"
345 #endif
346 
347 #endif
BoundingBoxType GetBoundingBox(LabelPixelType label) const
HistogramType::Pointer GetHistogram(LabelPixelType label) const
Extension of the itkLabelStatisticsImageFilter that also calculates the Skewness,Kurtosis,Entropy,Uniformity.
void SetHistogramParametersForLabels(std::map< LabelPixelType, unsigned int > numBins, std::map< LabelPixelType, PixelType > lowerBound, std::map< LabelPixelType, PixelType > upperBound)
RealType GetMean(LabelPixelType label) const
void ThreadedGenerateData(const typename TInputImage::RegionType &outputRegionForThread, ThreadIdType threadId) override
RealType GetSigma(LabelPixelType label) const
RealType GetVariance(LabelPixelType label) const
RegionType GetRegion(LabelPixelType label) const
void SetHistogramParameters(const int numBins, RealType lowerBound, RealType upperBound)
RealType GetMedian(LabelPixelType label) const
MapSizeType GetCount(LabelPixelType label) const
RealType GetMinimum(LabelPixelType label) const
RealType GetKurtosis(LabelPixelType label) const
itk::BoundingBox< unsigned long, 3, mitk::ScalarType > BoundingBoxType
RealType GetUniformity(LabelPixelType label) const
itksys::hash_map< LabelPixelType, LabelStatistics >::iterator StatisticsMapIterator
static T max(T x, T y)
Definition: svm.cpp:56
RealType GetSum(LabelPixelType label) const
RealType GetEntropy(LabelPixelType label) const
RealType GetUPP(LabelPixelType label) const
itksys::hash_map< LabelPixelType, LabelStatistics >::const_iterator StatisticsMapConstIterator
RealType GetSkewness(LabelPixelType label) const
RealType GetMPP(LabelPixelType label) const
RealType GetMaximum(LabelPixelType label) const
std::list< int > GetRelevantLabels() const
unsigned short PixelType
LabelStatistics(int size, RealType lowerBound, RealType upperBound)
itksys::hash_map< LabelPixelType, LabelStatistics > MapType
LabelStatisticsImageFilter< TInputImage, TLabelImage > Superclass