Medical Imaging Interaction Toolkit  2026.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 {
48  template <typename TInputImage>
49  class LabelStatisticsImageFilter : public itk::ImageSink<TInputImage>
50  {
51  public:
53  using Superclass = itk::ImageSink<TInputImage>;
56 
57  itkFactorylessNewMacro(Self);
58 
59  itkTypeMacro(LabelStatisticsImageFilter, itk::ImageSink);
60 
61  using IndexType = typename TInputImage::IndexType;
62  using SizeType = typename TInputImage::SizeType;
63  using RegionType = typename TInputImage::RegionType;
64  using PixelType = typename TInputImage::PixelType;
66 
67  static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
68 
69  using RealType = typename itk::NumericTraits<PixelType>::RealType;
70 
71  using DataObjectPointer = typename itk::DataObject::Pointer;
72 
73  using RealObjectType = itk::SimpleDataObjectDecorator<RealType>;
74 
75  using BoundingBoxType = std::vector<itk::IndexValueType>;
76 
77  using HistogramType = itk::Statistics::Histogram<RealType>;
78  using HistogramPointer = typename HistogramType::Pointer;
79 
81  {
82  public:
84  LabelStatistics(unsigned int size, RealType lowerBound, RealType upperBound);
86 
87  itk::SizeValueType m_Count;
88  itk::SizeValueType m_CountOfPositivePixels;
92  itk::CompensatedSummation<RealType> m_Sum;
93  itk::CompensatedSummation<RealType> m_SumOfPositivePixels;
94  itk::CompensatedSummation<RealType> m_SumOfSquares;
95  itk::CompensatedSummation<RealType> m_SumOfCubes;
96  itk::CompensatedSummation<RealType> m_SumOfQuadruples;
108  };
109 
110  using MapType = std::unordered_map<LabelPixelType, LabelStatistics>;
111  using MapIterator = typename MapType::iterator;
112  using MapConstIterator = typename MapType::const_iterator;
113 
114  using ValidLabelValuesContainerType = std::vector<LabelPixelType>;
120 
132  const std::unordered_map<LabelPixelType, unsigned int>& sizes,
133  const std::unordered_map<LabelPixelType, RealType>& lowerBounds,
134  const std::unordered_map<LabelPixelType, RealType>& upperBounds);
135 
136  using LabelImageType = itk::Image<LabelPixelType, ImageDimension>;
137  using ProcessObject = itk::ProcessObject;
138 
141 
142  bool HasLabel(LabelPixelType label) const;
143  unsigned int GetNumberOfObjects() const;
144  unsigned int GetNumberOfLabels() const;
145 
160  itk::SizeValueType GetCount(LabelPixelType label) const;
166 
167  protected:
170 
171  void BeforeStreamedGenerateData() override;
173  void AfterStreamedGenerateData() override;
174 
175  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
176 
177  private:
178  const LabelStatistics& GetLabelStatistics(LabelPixelType label) const;
179  const LabelStatistics& GetLabelHistogramStatistics(LabelPixelType label) const;
180 
181  void MergeMap(MapType& map1, MapType& map2) const;
182 
183  MapType m_LabelStatistics;
184  ValidLabelValuesContainerType m_ValidLabelValues;
185 
186  bool m_ComputeHistograms;
187  std::unordered_map<LabelPixelType, unsigned int> m_HistogramSizes;
188  std::unordered_map<LabelPixelType, RealType> m_HistogramLowerBounds;
189  std::unordered_map<LabelPixelType, RealType> m_HistogramUpperBounds;
190 
191  std::mutex m_Mutex;
192  };
193 }
194 
195 #ifndef ITK_MANUAL_INSTANTIATION
196 #include <mitkLabelStatisticsImageFilter.tpp>
197 #endif
198 
199 #endif
LabelStatistics(unsigned int size, RealType lowerBound, RealType upperBound)
Computes per-label image statistics including histogram, moments, and entropy.
void ThreadedStreamedGenerateData(const RegionType &) override
HistogramPointer GetHistogram(LabelPixelType label) const
itk::SizeValueType GetCount(LabelPixelType label) const
static constexpr unsigned int ImageDimension
unsigned int GetNumberOfLabels() const
RealType GetSumOfQuadruples(LabelPixelType label) const
unsigned int GetNumberOfObjects() const
RealType GetMean(LabelPixelType label) const
RealType GetUPP(LabelPixelType label) const
itk::Image< LabelPixelType, ImageDimension > LabelImageType
const ValidLabelValuesContainerType & GetValidLabelValues() const
Get the list of label values that were found during processing.
BoundingBoxType GetBoundingBox(LabelPixelType label) const
typename TInputImage::IndexType IndexType
std::unordered_map< LabelPixelType, LabelStatistics > MapType
typename itk::NumericTraits< PixelType >::RealType RealType
bool HasLabel(LabelPixelType label) const
RegionType GetRegion(LabelPixelType label) const
typename MapType::const_iterator MapConstIterator
typename HistogramType::Pointer HistogramPointer
RealType GetVariance(LabelPixelType label) const
itk::Statistics::Histogram< RealType > HistogramType
RealType GetMPP(LabelPixelType label) const
RealType GetSumOfCubes(LabelPixelType label) const
RealType GetEntropy(LabelPixelType label) const
void AfterStreamedGenerateData() override
PixelType GetMaximum(LabelPixelType label) const
void PrintSelf(std::ostream &os, itk::Indent indent) const override
itk::SimpleDataObjectDecorator< RealType > RealObjectType
itkGetInputMacro(LabelInput, LabelImageType)
RealType GetSum(LabelPixelType label) const
std::vector< LabelPixelType > ValidLabelValuesContainerType
RealType GetKurtosis(LabelPixelType label) const
std::vector< itk::IndexValueType > BoundingBoxType
RealType GetSkewness(LabelPixelType label) const
void BeforeStreamedGenerateData() override
typename itk::DataObject::Pointer DataObjectPointer
itkSetInputMacro(LabelInput, LabelImageType)
PixelType GetMinimum(LabelPixelType label) const
RealType GetMedian(LabelPixelType label) const
RealType GetSigma(LabelPixelType label) const
typename TInputImage::PixelType PixelType
RealType GetUniformity(LabelPixelType label) const
RealType GetSumOfSquares(LabelPixelType label) const
typename TInputImage::RegionType RegionType
void SetHistogramParameters(const std::unordered_map< LabelPixelType, unsigned int > &sizes, const std::unordered_map< LabelPixelType, RealType > &lowerBounds, const std::unordered_map< LabelPixelType, RealType > &upperBounds)
Set per-label histogram parameters.
typename mitk::Label::PixelType LabelPixelType
unsigned short PixelType
Pixel type used for label values (unsigned short).
Definition: mitkLabel.h:106
Find image slices visible on a given plane.