Medical Imaging Interaction Toolkit  2024.06.00
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 #ifndef mitkLabelStatisticsImageFilter_h
14 #define mitkLabelStatisticsImageFilter_h
15 
16 // This file is based on ITK's itkLabelStatisticsImageFilter.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
mitk::LabelStatisticsImageFilter::LabelStatistics::m_MPP
RealType m_MPP
Definition: mitkLabelStatisticsImageFilter.h:83
mitk::LabelStatisticsImageFilter::ProcessObject
itk::ProcessObject ProcessObject
Definition: mitkLabelStatisticsImageFilter.h:107
mitk::LabelStatisticsImageFilter::GetCount
itk::SizeValueType GetCount(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::LabelStatistics::m_CountOfPositivePixels
itk::SizeValueType m_CountOfPositivePixels
Definition: mitkLabelStatisticsImageFilter.h:72
mitk::LabelStatisticsImageFilter::GetSumOfQuadruples
RealType GetSumOfQuadruples(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::GetBoundingBox
BoundingBoxType GetBoundingBox(LabelPixelType label) const
mitkLabel.h
mitk::LabelStatisticsImageFilter::ThreadedStreamedGenerateData
void ThreadedStreamedGenerateData(const RegionType &) override
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Mean
RealType m_Mean
Definition: mitkLabelStatisticsImageFilter.h:75
mitk::LabelStatisticsImageFilter::ValidLabelValuesContainerType
std::vector< LabelPixelType > ValidLabelValuesContainerType
Definition: mitkLabelStatisticsImageFilter.h:98
mitk::LabelStatisticsImageFilter::GetSumOfSquares
RealType GetSumOfSquares(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::RealObjectType
itk::SimpleDataObjectDecorator< RealType > RealObjectType
Definition: mitkLabelStatisticsImageFilter.h:57
mitk::LabelStatisticsImageFilter::GetMaximum
PixelType GetMaximum(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::MapType
std::unordered_map< LabelPixelType, LabelStatistics > MapType
Definition: mitkLabelStatisticsImageFilter.h:94
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Sum
itk::CompensatedSummation< RealType > m_Sum
Definition: mitkLabelStatisticsImageFilter.h:76
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Sigma
RealType m_Sigma
Definition: mitkLabelStatisticsImageFilter.h:81
mitk::LabelStatisticsImageFilter::GetEntropy
RealType GetEntropy(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::LabelStatistics::m_UPP
RealType m_UPP
Definition: mitkLabelStatisticsImageFilter.h:86
mitk::LabelStatisticsImageFilter::GetSigma
RealType GetSigma(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::GetSkewness
RealType GetSkewness(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::SizeType
typename TInputImage::SizeType SizeType
Definition: mitkLabelStatisticsImageFilter.h:46
mitk::LabelStatisticsImageFilter::LabelStatistics::~LabelStatistics
~LabelStatistics()
mitk::LabelStatisticsImageFilter::~LabelStatisticsImageFilter
~LabelStatisticsImageFilter()
mitk::LabelStatisticsImageFilter::RegionType
typename TInputImage::RegionType RegionType
Definition: mitkLabelStatisticsImageFilter.h:47
itk::SmartPointer< Self >
mitk::LabelStatisticsImageFilter::GetHistogram
HistogramPointer GetHistogram(LabelPixelType label) const
mitk::Label::PixelType
unsigned short PixelType
Definition: mitkLabel.h:34
mitk::LabelStatisticsImageFilter::GetKurtosis
RealType GetKurtosis(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::LabelStatistics::m_SumOfCubes
itk::CompensatedSummation< RealType > m_SumOfCubes
Definition: mitkLabelStatisticsImageFilter.h:79
mitk::LabelStatisticsImageFilter::LabelStatistics
Definition: mitkLabelStatisticsImageFilter.h:64
mitk::LabelStatisticsImageFilter::GetNumberOfLabels
unsigned int GetNumberOfLabels() const
mitk::LabelStatisticsImageFilter::GetRegion
RegionType GetRegion(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::LabelStatistics::m_SumOfQuadruples
itk::CompensatedSummation< RealType > m_SumOfQuadruples
Definition: mitkLabelStatisticsImageFilter.h:80
mitk::LabelStatisticsImageFilter::GetVariance
RealType GetVariance(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::GetMPP
RealType GetMPP(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::HasLabel
bool HasLabel(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::ImageDimension
static constexpr unsigned int ImageDimension
Definition: mitkLabelStatisticsImageFilter.h:51
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::LabelStatisticsImageFilter::LabelStatistics::m_SumOfSquares
itk::CompensatedSummation< RealType > m_SumOfSquares
Definition: mitkLabelStatisticsImageFilter.h:78
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Kurtosis
RealType m_Kurtosis
Definition: mitkLabelStatisticsImageFilter.h:89
mitk::LabelStatisticsImageFilter::MapIterator
typename MapType::iterator MapIterator
Definition: mitkLabelStatisticsImageFilter.h:95
mitk::LabelStatisticsImageFilter::RealType
typename itk::NumericTraits< PixelType >::RealType RealType
Definition: mitkLabelStatisticsImageFilter.h:53
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Median
RealType m_Median
Definition: mitkLabelStatisticsImageFilter.h:84
mitk::LabelStatisticsImageFilter::LabelStatistics::m_BoundingBox
BoundingBoxType m_BoundingBox
Definition: mitkLabelStatisticsImageFilter.h:90
mitk::LabelStatisticsImageFilter::IndexType
typename TInputImage::IndexType IndexType
Definition: mitkLabelStatisticsImageFilter.h:45
mitk::LabelStatisticsImageFilter::LabelStatistics::LabelStatistics
LabelStatistics()
mitk::LabelStatisticsImageFilter::MapConstIterator
typename MapType::const_iterator MapConstIterator
Definition: mitkLabelStatisticsImageFilter.h:96
mitk::LabelStatisticsImageFilter::LabelStatisticsImageFilter
LabelStatisticsImageFilter()
mitk::LabelStatisticsImageFilter::GetMean
RealType GetMean(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::HistogramType
itk::Statistics::Histogram< RealType > HistogramType
Definition: mitkLabelStatisticsImageFilter.h:61
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Min
RealType m_Min
Definition: mitkLabelStatisticsImageFilter.h:73
mitk::LabelStatisticsImageFilter::PixelType
typename TInputImage::PixelType PixelType
Definition: mitkLabelStatisticsImageFilter.h:48
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Histogram
HistogramPointer m_Histogram
Definition: mitkLabelStatisticsImageFilter.h:91
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Count
itk::SizeValueType m_Count
Definition: mitkLabelStatisticsImageFilter.h:71
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Variance
RealType m_Variance
Definition: mitkLabelStatisticsImageFilter.h:82
mitk::LabelStatisticsImageFilter::GetNumberOfObjects
unsigned int GetNumberOfObjects() const
mitk::LabelStatisticsImageFilter::GetMinimum
PixelType GetMinimum(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::LabelStatistics::m_SumOfPositivePixels
itk::CompensatedSummation< RealType > m_SumOfPositivePixels
Definition: mitkLabelStatisticsImageFilter.h:77
mitk::LabelStatisticsImageFilter::GetUniformity
RealType GetUniformity(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::BoundingBoxType
std::vector< itk::IndexValueType > BoundingBoxType
Definition: mitkLabelStatisticsImageFilter.h:59
mitk::LabelStatisticsImageFilter::GetUPP
RealType GetUPP(LabelPixelType label) const
mitk::LabelStatisticsImageFilter
Definition: mitkLabelStatisticsImageFilter.h:33
mitk::LabelStatisticsImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Max
RealType m_Max
Definition: mitkLabelStatisticsImageFilter.h:74
mitk::LabelStatisticsImageFilter::GetSumOfCubes
RealType GetSumOfCubes(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::Superclass
itk::ImageSink< TInputImage > Superclass
Definition: mitkLabelStatisticsImageFilter.h:37
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Uniformity
RealType m_Uniformity
Definition: mitkLabelStatisticsImageFilter.h:85
mitk::LabelStatisticsImageFilter::HistogramPointer
typename HistogramType::Pointer HistogramPointer
Definition: mitkLabelStatisticsImageFilter.h:62
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Entropy
RealType m_Entropy
Definition: mitkLabelStatisticsImageFilter.h:87
mitk::LabelStatisticsImageFilter::AfterStreamedGenerateData
void AfterStreamedGenerateData() override
mitk::LabelStatisticsImageFilter::GetSum
RealType GetSum(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::SetHistogramParameters
void SetHistogramParameters(const std::unordered_map< LabelPixelType, unsigned int > &sizes, const std::unordered_map< LabelPixelType, RealType > &lowerBounds, const std::unordered_map< LabelPixelType, RealType > &upperBounds)
mitk::LabelStatisticsImageFilter::GetValidLabelValues
const ValidLabelValuesContainerType & GetValidLabelValues() const
mitk::LabelStatisticsImageFilter::itkGetInputMacro
itkGetInputMacro(LabelInput, LabelImageType)
mitk::LabelStatisticsImageFilter::GetMedian
RealType GetMedian(LabelPixelType label) const
mitk::LabelStatisticsImageFilter::LabelImageType
itk::Image< LabelPixelType, ImageDimension > LabelImageType
Definition: mitkLabelStatisticsImageFilter.h:106
mitk::LabelStatisticsImageFilter::BeforeStreamedGenerateData
void BeforeStreamedGenerateData() override
mitk::LabelStatisticsImageFilter::LabelStatistics::m_Skewness
RealType m_Skewness
Definition: mitkLabelStatisticsImageFilter.h:88
mitk::LabelStatisticsImageFilter::itkSetInputMacro
itkSetInputMacro(LabelInput, LabelImageType)
mitk::LabelStatisticsImageFilter::LabelPixelType
typename mitk::Label::PixelType LabelPixelType
Definition: mitkLabelStatisticsImageFilter.h:49
mitk::LabelStatisticsImageFilter::DataObjectPointer
typename itk::DataObject::Pointer DataObjectPointer
Definition: mitkLabelStatisticsImageFilter.h:55