ITK  6.0.0
Insight Toolkit
itkLabelStatisticsImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkLabelStatisticsImageFilter_h
19 #define itkLabelStatisticsImageFilter_h
20 
21 #include "itkImageSink.h"
22 #include "itkNumericTraits.h"
24 #include "itkHistogram.h"
25 #include "itkPrintHelper.h"
26 #include <mutex>
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace itk
31 {
62 template <typename TInputImage, typename TLabelImage>
63 class ITK_TEMPLATE_EXPORT LabelStatisticsImageFilter : public ImageSink<TInputImage>
64 {
65 public:
66  ITK_DISALLOW_COPY_AND_MOVE(LabelStatisticsImageFilter);
67 
73 
75  itkNewMacro(Self);
76 
78  itkOverrideGetNameOfClassMacro(LabelStatisticsImageFilter);
79 
83  using SizeType = typename TInputImage::SizeType;
85  using PixelType = typename TInputImage::PixelType;
86 
88  using LabelImageType = TLabelImage;
93  using LabelPixelType = typename TLabelImage::PixelType;
94 
96  static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
97 
100 
103 
106 
108  using BoundingBoxType = std::vector<IndexValueType>;
109 
113 
119  {
120  public:
121  // default constructor
123  {
124  // initialized to the default values
125  m_Count = IdentifierType{};
126  m_Sum = RealType{};
127  m_SumOfSquares = RealType{};
128 
129  // Set such that the first pixel encountered can be compared
130  m_Minimum = NumericTraits<RealType>::max();
132 
133  // Default these to zero
134  m_Mean = RealType{};
135  m_Sigma = RealType{};
136  m_Variance = RealType{};
137 
138  const unsigned int imageDimension = Self::ImageDimension;
139  m_BoundingBox.resize(imageDimension * 2);
140  for (unsigned int i = 0; i < imageDimension * 2; i += 2)
141  {
142  m_BoundingBox[i] = NumericTraits<IndexValueType>::max();
143  m_BoundingBox[i + 1] = NumericTraits<IndexValueType>::NonpositiveMin();
144  }
145  m_Histogram = nullptr;
146  }
147 
148  // constructor with histogram enabled
149  LabelStatistics(int size, RealType lowerBound, RealType upperBound)
150  {
151  // initialized to the default values
152  m_Count = IdentifierType{};
153  m_Sum = RealType{};
154  m_SumOfSquares = RealType{};
155 
156  // Set such that the first pixel encountered can be compared
157  m_Minimum = NumericTraits<RealType>::max();
159 
160  // Default these to zero
161  m_Mean = RealType{};
162  m_Sigma = RealType{};
163  m_Variance = RealType{};
164 
165  const unsigned int imageDimension = Self::ImageDimension;
166  m_BoundingBox.resize(imageDimension * 2);
167  for (unsigned int i = 0; i < imageDimension * 2; i += 2)
168  {
169  m_BoundingBox[i] = NumericTraits<IndexValueType>::max();
170  m_BoundingBox[i + 1] = NumericTraits<IndexValueType>::NonpositiveMin();
171  }
172 
173  // Histogram
174  m_Histogram = HistogramType::New();
175  typename HistogramType::SizeType hsize;
178  hsize.SetSize(1);
179  lb.SetSize(1);
180  ub.SetSize(1);
181  m_Histogram->SetMeasurementVectorSize(1);
182  hsize[0] = size;
183  lb[0] = lowerBound;
184  ub[0] = upperBound;
185  m_Histogram->Initialize(hsize, lb, ub);
186  }
187 
188  // need copy constructor because of smart pointer to histogram
190  {
191  m_Count = l.m_Count;
192  m_Minimum = l.m_Minimum;
193  m_Maximum = l.m_Maximum;
194  m_Mean = l.m_Mean;
195  m_Sum = l.m_Sum;
196  m_SumOfSquares = l.m_SumOfSquares;
197  m_Sigma = l.m_Sigma;
198  m_Variance = l.m_Variance;
199  m_BoundingBox = l.m_BoundingBox;
200  m_Histogram = l.m_Histogram;
201  }
202 
203  LabelStatistics(LabelStatistics &&) = default;
204 
205  // added for completeness
208  {
209  if (this != &l)
210  {
211  m_Count = l.m_Count;
212  m_Minimum = l.m_Minimum;
213  m_Maximum = l.m_Maximum;
214  m_Mean = l.m_Mean;
215  m_Sum = l.m_Sum;
216  m_SumOfSquares = l.m_SumOfSquares;
217  m_Sigma = l.m_Sigma;
218  m_Variance = l.m_Variance;
219  m_BoundingBox = l.m_BoundingBox;
220  m_Histogram = l.m_Histogram;
221  }
222  return *this;
223  }
224 
225  friend std::ostream &
226  operator<<(std::ostream & os, const LabelStatistics & labelStatistics)
227  {
228  using namespace print_helper;
229 
230  os << "Count: " << static_cast<typename NumericTraits<IdentifierType>::PrintType>(labelStatistics.m_Count)
231  << std::endl;
232  os << "Minimum: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Minimum)
233  << std::endl;
234  os << "Maximum: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Maximum)
235  << std::endl;
236  os << "Mean: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Mean) << std::endl;
237  os << "Sum: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Sum) << std::endl;
238  os << "SumOfSquares: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_SumOfSquares)
239  << std::endl;
240  os << "Sigma: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Sigma) << std::endl;
241  os << "Variance: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Variance)
242  << std::endl;
243  os << "BoundingBox: " << labelStatistics.m_BoundingBox << std::endl;
244 
245  os << "Histogram: ";
246  if (labelStatistics.m_Histogram)
247  {
248  labelStatistics.m_Histogram->Print(os);
249  }
250  else
251  {
252  os << "nullptr" << std::endl;
253  }
254 
255  return os;
256  }
257 
268  };
269 
271  using MapType = std::unordered_map<LabelPixelType, LabelStatistics>;
272  using MapIterator = typename MapType::iterator;
273  using MapConstIterator = typename MapType::const_iterator;
275 
277  using ValidLabelValuesContainerType = std::vector<LabelPixelType>;
278 
279  // macros for Histogram enables
280  itkSetMacro(UseHistograms, bool);
281  itkGetConstMacro(UseHistograms, bool);
282  itkBooleanMacro(UseHistograms);
283 
284 
285  virtual const ValidLabelValuesContainerType &
287  {
288  return m_ValidLabelValues;
289  }
290 
292  itkSetInputMacro(LabelInput, TLabelImage);
293  itkGetInputMacro(LabelInput, TLabelImage);
298  bool
300  {
301  return m_LabelStatistics.find(label) != m_LabelStatistics.end();
302  }
303 
305  MapSizeType
307  {
308  return static_cast<MapSizeType>(m_LabelStatistics.size());
309  }
310 
311  MapSizeType
313  {
314  return static_cast<MapSizeType>(this->GetNumberOfObjects());
315  }
316 
318  RealType
319  GetMinimum(LabelPixelType label) const;
320 
322  RealType
323  GetMaximum(LabelPixelType label) const;
324 
326  RealType
327  GetMean(LabelPixelType label) const;
328 
331  RealType
332  GetMedian(LabelPixelType label) const;
333 
335  RealType
336  GetSigma(LabelPixelType label) const;
337 
339  RealType
340  GetVariance(LabelPixelType label) const;
341 
345  BoundingBoxType
346  GetBoundingBox(LabelPixelType label) const;
347 
349  RegionType
350  GetRegion(LabelPixelType label) const;
351 
353  RealType
354  GetSum(LabelPixelType label) const;
355 
357  MapSizeType
358  GetCount(LabelPixelType label) const;
359 
361  HistogramPointer
362  GetHistogram(LabelPixelType label) const;
363 
365  void
366  SetHistogramParameters(const int numBins, RealType lowerBound, RealType upperBound);
367 
368  // Change the access from protected to public to expose streaming option, a using statement can not be used due to
369  // limitations of wrapping.
370  void
371  SetNumberOfStreamDivisions(const unsigned int n) override
372  {
373  Superclass::SetNumberOfStreamDivisions(n);
374  }
375  unsigned int
376  GetNumberOfStreamDivisions() const override
377  {
378  return Superclass::GetNumberOfStreamDivisions();
379  }
380 
381 
382 #ifdef ITK_USE_CONCEPT_CHECKING
383  // Begin concept checking
384  itkConceptMacro(InputHasNumericTraitsCheck, (Concept::HasNumericTraits<PixelType>));
385  // End concept checking
386 #endif
387 
388 protected:
390  ~LabelStatisticsImageFilter() override = default;
391  void
392  PrintSelf(std::ostream & os, Indent indent) const override;
393 
394  void
396  {
397  this->AllocateOutputs();
398  m_LabelStatistics.clear();
399  }
400 
403  void
404  AfterStreamedGenerateData() override;
405 
406  void
407  ThreadedStreamedGenerateData(const RegionType &) override;
408 
409 private:
410  void
411  MergeMap(MapType &, MapType &) const;
412 
413  MapType m_LabelStatistics{};
414  ValidLabelValuesContainerType m_ValidLabelValues{};
415 
416  bool m_UseHistograms{};
417 
418  typename HistogramType::SizeType m_NumBins{};
419 
420  RealType m_LowerBound{};
421  RealType m_UpperBound{};
422 
423  std::mutex m_Mutex{};
424 
425 }; // end of class
426 } // end namespace itk
427 
428 #ifndef ITK_MANUAL_INSTANTIATION
429 # include "itkLabelStatisticsImageFilter.hxx"
430 #endif
431 
432 #endif
itk::LabelStatisticsImageFilter::LabelStatistics::m_Variance
RealType m_Variance
Definition: itkLabelStatisticsImageFilter.h:265
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::SimpleDataObjectDecorator
Decorates any "simple" data type (data types without smart pointers) with a DataObject API.
Definition: itkSimpleDataObjectDecorator.h:66
itk::LabelStatisticsImageFilter::RealType
typename NumericTraits< PixelType >::RealType RealType
Definition: itkLabelStatisticsImageFilter.h:99
itk::LabelStatisticsImageFilter::MapIterator
typename MapType::iterator MapIterator
Definition: itkLabelStatisticsImageFilter.h:272
itk::LabelStatisticsImageFilter::SizeType
typename TInputImage::SizeType SizeType
Definition: itkLabelStatisticsImageFilter.h:83
itk::Concept::HasNumericTraits
Definition: itkConceptChecking.h:717
itk::LabelStatisticsImageFilter::LabelIndexType
typename TLabelImage::IndexType LabelIndexType
Definition: itkLabelStatisticsImageFilter.h:92
itk::LabelStatisticsImageFilter::LabelImageType
TLabelImage LabelImageType
Definition: itkLabelStatisticsImageFilter.h:88
itk::LabelStatisticsImageFilter::SetNumberOfStreamDivisions
void SetNumberOfStreamDivisions(const unsigned int n) override
Definition: itkLabelStatisticsImageFilter.h:371
itk::LabelStatisticsImageFilter::LabelImagePointer
typename TLabelImage::Pointer LabelImagePointer
Definition: itkLabelStatisticsImageFilter.h:89
itk::LabelStatisticsImageFilter::PixelType
typename TInputImage::PixelType PixelType
Definition: itkLabelStatisticsImageFilter.h:85
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:99
itk::StreamingProcessObject
Base class interface to process data on multiple requested input chunks.
Definition: itkStreamingProcessObject.h:40
itk::LabelStatisticsImageFilter::LabelStatistics::m_Minimum
RealType m_Minimum
Definition: itkLabelStatisticsImageFilter.h:259
itk::ImageSink::InputImagePointer
typename InputImageType::Pointer InputImagePointer
Definition: itkImageSink.h:74
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::LabelStatisticsImageFilter::MapType
std::unordered_map< LabelPixelType, LabelStatistics > MapType
Definition: itkLabelStatisticsImageFilter.h:271
itk::LabelStatisticsImageFilter::GetNumberOfLabels
MapSizeType GetNumberOfLabels() const
Definition: itkLabelStatisticsImageFilter.h:312
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LabelStatisticsImageFilter::LabelStatistics::m_Count
IdentifierType m_Count
Definition: itkLabelStatisticsImageFilter.h:258
itkPrintHelper.h
itk::LabelStatisticsImageFilter::GetNumberOfObjects
MapSizeType GetNumberOfObjects() const
Definition: itkLabelStatisticsImageFilter.h:306
itk::LabelStatisticsImageFilter::LabelPixelType
typename TLabelImage::PixelType LabelPixelType
Definition: itkLabelStatisticsImageFilter.h:93
itk::LabelStatisticsImageFilter::ValidLabelValuesContainerType
std::vector< LabelPixelType > ValidLabelValuesContainerType
Definition: itkLabelStatisticsImageFilter.h:277
itk::LabelStatisticsImageFilter::HistogramPointer
typename HistogramType::Pointer HistogramPointer
Definition: itkLabelStatisticsImageFilter.h:112
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::LabelStatisticsImageFilter::LabelStatistics::m_Maximum
RealType m_Maximum
Definition: itkLabelStatisticsImageFilter.h:260
itk::Statistics::Histogram
This class stores measurement vectors in the context of n-dimensional histogram.
Definition: itkHistogram.h:77
itkImageSink.h
itkHistogram.h
itk::LabelStatisticsImageFilter::LabelStatistics::operator=
LabelStatistics & operator=(const LabelStatistics &l)
Definition: itkLabelStatisticsImageFilter.h:207
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::NumericTraits::PrintType
T PrintType
Definition: itkNumericTraits.h:70
itk::LabelStatisticsImageFilter::IndexType
typename TInputImage::IndexType IndexType
Definition: itkLabelStatisticsImageFilter.h:84
itk::LabelStatisticsImageFilter
Given an intensity image and a label map, compute min, max, variance and mean of the pixels associate...
Definition: itkLabelStatisticsImageFilter.h:63
itk::LabelStatisticsImageFilter::BoundingBoxType
std::vector< IndexValueType > BoundingBoxType
Definition: itkLabelStatisticsImageFilter.h:108
itk::LabelStatisticsImageFilter::MapConstIterator
typename MapType::const_iterator MapConstIterator
Definition: itkLabelStatisticsImageFilter.h:273
itk::LabelStatisticsImageFilter::BeforeStreamedGenerateData
void BeforeStreamedGenerateData() override
Definition: itkLabelStatisticsImageFilter.h:395
itk::LabelStatisticsImageFilter::LabelSizeType
typename TLabelImage::SizeType LabelSizeType
Definition: itkLabelStatisticsImageFilter.h:91
itk::LabelStatisticsImageFilter::HasLabel
bool HasLabel(LabelPixelType label) const
Definition: itkLabelStatisticsImageFilter.h:299
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:60
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:169
itk::LabelStatisticsImageFilter::LabelStatistics::m_BoundingBox
BoundingBoxType m_BoundingBox
Definition: itkLabelStatisticsImageFilter.h:266
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:65
itk::LabelStatisticsImageFilter::LabelStatistics::m_Histogram
HistogramType::Pointer m_Histogram
Definition: itkLabelStatisticsImageFilter.h:267
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::LabelStatisticsImageFilter::LabelStatistics::m_Sigma
RealType m_Sigma
Definition: itkLabelStatisticsImageFilter.h:264
itk::LabelStatisticsImageFilter::LabelStatistics::m_SumOfSquares
RealType m_SumOfSquares
Definition: itkLabelStatisticsImageFilter.h:263
itk::Array< itk::SizeValueType >
itk::LabelStatisticsImageFilter::LabelStatistics::LabelStatistics
LabelStatistics()
Definition: itkLabelStatisticsImageFilter.h:122
itkNumericTraits.h
itk::LabelStatisticsImageFilter::RegionType
typename TInputImage::RegionType RegionType
Definition: itkLabelStatisticsImageFilter.h:82
itk::LabelStatisticsImageFilter::GetValidLabelValues
virtual const ValidLabelValuesContainerType & GetValidLabelValues() const
Definition: itkLabelStatisticsImageFilter.h:286
itk::LabelStatisticsImageFilter::LabelRegionType
typename TLabelImage::RegionType LabelRegionType
Definition: itkLabelStatisticsImageFilter.h:90
itkSimpleDataObjectDecorator.h
New
static Pointer New()
itk::LabelStatisticsImageFilter::LabelStatistics::LabelStatistics
LabelStatistics(const LabelStatistics &l)
Definition: itkLabelStatisticsImageFilter.h:189
itk::LabelStatisticsImageFilter::MapSizeType
IdentifierType MapSizeType
Definition: itkLabelStatisticsImageFilter.h:274
itk::LabelStatisticsImageFilter::LabelStatistics::LabelStatistics
LabelStatistics(int size, RealType lowerBound, RealType upperBound)
Definition: itkLabelStatisticsImageFilter.h:149
itk::DataObject::Pointer
SmartPointer< Self > Pointer
Definition: itkDataObject.h:301
itk::ImageSink
Definition: itkImageSink.h:53
itk::LabelStatisticsImageFilter::GetNumberOfStreamDivisions
unsigned int GetNumberOfStreamDivisions() const override
Definition: itkLabelStatisticsImageFilter.h:376
itk::IdentifierType
SizeValueType IdentifierType
Definition: itkIntTypes.h:90
itk::LabelStatisticsImageFilter::LabelStatistics::m_Sum
RealType m_Sum
Definition: itkLabelStatisticsImageFilter.h:262
itk::LabelStatisticsImageFilter::LabelStatistics::m_Mean
RealType m_Mean
Definition: itkLabelStatisticsImageFilter.h:261
itk::Array::SetSize
void SetSize(SizeValueType sz)
itk::LabelStatisticsImageFilter::LabelStatistics
Statistics stored per label.
Definition: itkLabelStatisticsImageFilter.h:118