Medical Imaging Interaction Toolkit  2022.04.99-b878213f
Medical Imaging Interaction Toolkit
mitkLabelStatisticsImageFilter.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 
13 // This file is based on ITK's itkLabelStatisticsImageFilter.h
14 
15 #ifndef mitkLabelStatisticsImageFilter_h
16 #define mitkLabelStatisticsImageFilter_h
17 
18 #include <itkCompensatedSummation.h>
19 #include <itkHistogram.h>
20 #include <itkImageSink.h>
21 #include <itkNumericTraits.h>
22 #include <itkSimpleDataObjectDecorator.h>
23 
24 #include <mutex>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include <mitkLabel.h>
29 
30 namespace mitk
31 {
32  template <typename TInputImage>
33  class LabelStatisticsImageFilter : public itk::ImageSink<TInputImage>
34  {
35  public:
37  using Superclass = itk::ImageSink<TInputImage>;
40 
41  itkFactorylessNewMacro(Self);
42 
43  itkTypeMacro(LabelStatisticsImageFilter, itk::ImageSink);
44 
45  using IndexType = typename TInputImage::IndexType;
46  using SizeType = typename TInputImage::SizeType;
47  using RegionType = typename TInputImage::RegionType;
48  using PixelType = typename TInputImage::PixelType;
50 
51  static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
52 
53  using RealType = typename itk::NumericTraits<PixelType>::RealType;
54 
55  using DataObjectPointer = typename itk::DataObject::Pointer;
56 
57  using RealObjectType = itk::SimpleDataObjectDecorator<RealType>;
58 
59  using BoundingBoxType = std::vector<itk::IndexValueType>;
60 
61  using HistogramType = itk::Statistics::Histogram<RealType>;
62  using HistogramPointer = typename HistogramType::Pointer;
63 
65  {
66  public:
68  LabelStatistics(unsigned int size, RealType lowerBound, RealType upperBound);
70 
71  itk::SizeValueType m_Count;
72  itk::SizeValueType m_CountOfPositivePixels;
76  itk::CompensatedSummation<RealType> m_Sum;
77  itk::CompensatedSummation<RealType> m_SumOfPositivePixels;
78  itk::CompensatedSummation<RealType> m_SumOfSquares;
79  itk::CompensatedSummation<RealType> m_SumOfCubes;
80  itk::CompensatedSummation<RealType> m_SumOfQuadruples;
92  };
93 
94  using MapType = std::unordered_map<LabelPixelType, LabelStatistics>;
95  using MapIterator = typename MapType::iterator;
96  using MapConstIterator = typename MapType::const_iterator;
97 
98  using ValidLabelValuesContainerType = std::vector<LabelPixelType>;
100 
102  const std::unordered_map<LabelPixelType, unsigned int>& sizes,
103  const std::unordered_map<LabelPixelType, RealType>& lowerBounds,
104  const std::unordered_map<LabelPixelType, RealType>& upperBounds);
105 
106  using LabelImageType = itk::Image<LabelPixelType, ImageDimension>;
107  using ProcessObject = itk::ProcessObject;
108 
109  itkSetInputMacro(LabelInput, LabelImageType);
110  itkGetInputMacro(LabelInput, LabelImageType);
111 
112  bool HasLabel(LabelPixelType label) const;
113  unsigned int GetNumberOfObjects() const;
114  unsigned int GetNumberOfLabels() const;
115 
116  PixelType GetMinimum(LabelPixelType label) const;
117  PixelType GetMaximum(LabelPixelType label) const;
118  RealType GetMean(LabelPixelType label) const;
119  RealType GetSigma(LabelPixelType label) const;
120  RealType GetVariance(LabelPixelType label) const;
122  RegionType GetRegion(LabelPixelType label) const;
123  RealType GetSum(LabelPixelType label) const;
127  RealType GetSkewness(LabelPixelType label) const;
128  RealType GetKurtosis(LabelPixelType label) const;
129  RealType GetMPP(LabelPixelType label) const;
130  itk::SizeValueType GetCount(LabelPixelType label) const;
132  RealType GetEntropy(LabelPixelType label) const;
134  RealType GetUPP(LabelPixelType label) const;
135  RealType GetMedian(LabelPixelType label) const;
136 
137  protected:
140 
141  void BeforeStreamedGenerateData() override;
142  void ThreadedStreamedGenerateData(const RegionType&) override;
143  void AfterStreamedGenerateData() override;
144 
145  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
146 
147  private:
148  const LabelStatistics& GetLabelStatistics(LabelPixelType label) const;
149  const LabelStatistics& GetLabelHistogramStatistics(LabelPixelType label) const;
150 
151  void MergeMap(MapType& map1, MapType& map2) const;
152 
153  MapType m_LabelStatistics;
154  ValidLabelValuesContainerType m_ValidLabelValues;
155 
156  bool m_ComputeHistograms;
157  std::unordered_map<LabelPixelType, unsigned int> m_HistogramSizes;
158  std::unordered_map<LabelPixelType, RealType> m_HistogramLowerBounds;
159  std::unordered_map<LabelPixelType, RealType> m_HistogramUpperBounds;
160 
161  std::mutex m_Mutex;
162  };
163 }
164 
165 #ifndef ITK_MANUAL_INSTANTIATION
166 #include <mitkLabelStatisticsImageFilter.hxx>
167 #endif
168 
169 #endif
RealType GetSigma(LabelPixelType label) const
RealType GetSum(LabelPixelType label) const
std::vector< LabelPixelType > ValidLabelValuesContainerType
RealType GetSumOfCubes(LabelPixelType label) const
PixelType GetMaximum(LabelPixelType label) const
typename itk::DataObject::Pointer DataObjectPointer
typename mitk::Label::PixelType LabelPixelType
RealType GetKurtosis(LabelPixelType label) const
RealType GetVariance(LabelPixelType label) const
void BeforeStreamedGenerateData() override
typename TInputImage::RegionType RegionType
DataCollection - Class to facilitate loading/accessing structured data.
unsigned int GetNumberOfObjects() const
itkSetInputMacro(LabelInput, LabelImageType)
RealType GetUniformity(LabelPixelType label) const
RealType GetMPP(LabelPixelType label) const
BoundingBoxType GetBoundingBox(LabelPixelType label) const
itk::SimpleDataObjectDecorator< RealType > RealObjectType
std::unordered_map< LabelPixelType, LabelStatistics > MapType
RealType GetSumOfSquares(LabelPixelType label) const
itk::SizeValueType GetCount(LabelPixelType label) const
unsigned int GetNumberOfLabels() const
RegionType GetRegion(LabelPixelType label) const
static constexpr unsigned int ImageDimension
itkGetInputMacro(LabelInput, LabelImageType)
typename TInputImage::IndexType IndexType
RealType GetMedian(LabelPixelType label) const
bool HasLabel(LabelPixelType label) const
typename TInputImage::PixelType PixelType
HistogramPointer GetHistogram(LabelPixelType label) const
void SetHistogramParameters(const std::unordered_map< LabelPixelType, unsigned int > &sizes, const std::unordered_map< LabelPixelType, RealType > &lowerBounds, const std::unordered_map< LabelPixelType, RealType > &upperBounds)
void ThreadedStreamedGenerateData(const RegionType &) override
typename itk::NumericTraits< PixelType >::RealType RealType
RealType GetMean(LabelPixelType label) const
typename HistogramType::Pointer HistogramPointer
PixelType GetMinimum(LabelPixelType label) const
void PrintSelf(std::ostream &os, itk::Indent indent) const override
RealType GetUPP(LabelPixelType label) const
void AfterStreamedGenerateData() override
RealType GetEntropy(LabelPixelType label) const
unsigned short PixelType
Definition: mitkLabel.h:33
RealType GetSkewness(LabelPixelType label) const
const ValidLabelValuesContainerType & GetValidLabelValues() const
itk::Statistics::Histogram< RealType > HistogramType
itk::Image< LabelPixelType, ImageDimension > LabelImageType
typename MapType::const_iterator MapConstIterator
RealType GetSumOfQuadruples(LabelPixelType label) const
std::vector< itk::IndexValueType > BoundingBoxType