ITK  5.2.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  * http://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 <mutex>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace itk
30 {
61 template <typename TInputImage, typename TLabelImage>
62 class ITK_TEMPLATE_EXPORT LabelStatisticsImageFilter : public ImageSink<TInputImage>
63 {
64 public:
65  ITK_DISALLOW_COPY_AND_MOVE(LabelStatisticsImageFilter);
66 
72 
74  itkNewMacro(Self);
75 
78 
80  using InputImagePointer = typename TInputImage::Pointer;
82  using SizeType = typename TInputImage::SizeType;
84  using PixelType = typename TInputImage::PixelType;
85 
87  using LabelImageType = TLabelImage;
88  using LabelImagePointer = typename TLabelImage::Pointer;
92  using LabelPixelType = typename TLabelImage::PixelType;
93 
95  static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
96 
99 
102 
105 
107  using BoundingBoxType = std::vector<IndexValueType>;
108 
112 
118  {
119  public:
120  // default constructor
122  {
123  // initialized to the default values
126  m_SumOfSquares = NumericTraits<RealType>::ZeroValue();
127 
128  // Set such that the first pixel encountered can be compared
129  m_Minimum = NumericTraits<RealType>::max();
131 
132  // Default these to zero
135  m_Variance = NumericTraits<RealType>::ZeroValue();
136 
137  const unsigned int imageDimension = Self::ImageDimension;
138  m_BoundingBox.resize(imageDimension * 2);
139  for (unsigned int i = 0; i < imageDimension * 2; i += 2)
140  {
141  m_BoundingBox[i] = NumericTraits<IndexValueType>::max();
142  m_BoundingBox[i + 1] = NumericTraits<IndexValueType>::NonpositiveMin();
143  }
144  m_Histogram = nullptr;
145  }
146 
147  // constructor with histogram enabled
148  LabelStatistics(int size, RealType lowerBound, RealType upperBound)
149  {
150  // initialized to the default values
153  m_SumOfSquares = NumericTraits<RealType>::ZeroValue();
154 
155  // Set such that the first pixel encountered can be compared
156  m_Minimum = NumericTraits<RealType>::max();
158 
159  // Default these to zero
162  m_Variance = NumericTraits<RealType>::ZeroValue();
163 
164  const unsigned int imageDimension = Self::ImageDimension;
165  m_BoundingBox.resize(imageDimension * 2);
166  for (unsigned int i = 0; i < imageDimension * 2; i += 2)
167  {
168  m_BoundingBox[i] = NumericTraits<IndexValueType>::max();
169  m_BoundingBox[i + 1] = NumericTraits<IndexValueType>::NonpositiveMin();
170  }
171 
172  // Histogram
173  m_Histogram = HistogramType::New();
174  typename HistogramType::SizeType hsize;
177  hsize.SetSize(1);
178  lb.SetSize(1);
179  ub.SetSize(1);
180  m_Histogram->SetMeasurementVectorSize(1);
181  hsize[0] = size;
182  lb[0] = lowerBound;
183  ub[0] = upperBound;
184  m_Histogram->Initialize(hsize, lb, ub);
185  }
186 
187  // need copy constructor because of smart pointer to histogram
189  {
190  m_Count = l.m_Count;
191  m_Minimum = l.m_Minimum;
192  m_Maximum = l.m_Maximum;
193  m_Mean = l.m_Mean;
194  m_Sum = l.m_Sum;
195  m_SumOfSquares = l.m_SumOfSquares;
196  m_Sigma = l.m_Sigma;
197  m_Variance = l.m_Variance;
198  m_BoundingBox = l.m_BoundingBox;
199  m_Histogram = l.m_Histogram;
200  }
201 
202  LabelStatistics(LabelStatistics &&) = default;
203 
204  // added for completeness
207  {
208  if (this != &l)
209  {
210  m_Count = l.m_Count;
211  m_Minimum = l.m_Minimum;
212  m_Maximum = l.m_Maximum;
213  m_Mean = l.m_Mean;
214  m_Sum = l.m_Sum;
215  m_SumOfSquares = l.m_SumOfSquares;
216  m_Sigma = l.m_Sigma;
217  m_Variance = l.m_Variance;
218  m_BoundingBox = l.m_BoundingBox;
219  m_Histogram = l.m_Histogram;
220  }
221  return *this;
222  }
223 
234  };
235 
237  using MapType = std::unordered_map<LabelPixelType, LabelStatistics>;
238  using MapIterator = typename MapType::iterator;
239  using MapConstIterator = typename MapType::const_iterator;
241 
243  using ValidLabelValuesContainerType = std::vector<LabelPixelType>;
244 
245  // macros for Histogram enables
246  itkSetMacro(UseHistograms, bool);
247  itkGetConstMacro(UseHistograms, bool);
248  itkBooleanMacro(UseHistograms);
249 
250 
251  virtual const ValidLabelValuesContainerType &
253  {
254  return m_ValidLabelValues;
255  }
256 
258  itkSetInputMacro(LabelInput, TLabelImage);
259  itkGetInputMacro(LabelInput, TLabelImage);
261 
264  bool
266  {
267  return m_LabelStatistics.find(label) != m_LabelStatistics.end();
268  }
269 
271  MapSizeType
273  {
274  return static_cast<MapSizeType>(m_LabelStatistics.size());
275  }
276 
277  MapSizeType
279  {
280  return static_cast<MapSizeType>(this->GetNumberOfObjects());
281  }
282 
284  RealType
285  GetMinimum(LabelPixelType label) const;
286 
288  RealType
289  GetMaximum(LabelPixelType label) const;
290 
292  RealType
293  GetMean(LabelPixelType label) const;
294 
297  RealType
298  GetMedian(LabelPixelType label) const;
299 
301  RealType
302  GetSigma(LabelPixelType label) const;
303 
305  RealType
306  GetVariance(LabelPixelType label) const;
307 
311  BoundingBoxType
312  GetBoundingBox(LabelPixelType label) const;
313 
315  RegionType
316  GetRegion(LabelPixelType label) const;
317 
319  RealType
320  GetSum(LabelPixelType label) const;
321 
323  MapSizeType
324  GetCount(LabelPixelType label) const;
325 
327  HistogramPointer
328  GetHistogram(LabelPixelType label) const;
329 
331  void
332  SetHistogramParameters(const int numBins, RealType lowerBound, RealType upperBound);
333 
334  // Change the access from protected to public to expose streaming option, a using statement can not be used due to
335  // limitations of wrapping.
336  void
337  SetNumberOfStreamDivisions(const unsigned int n) override
338  {
339  Superclass::SetNumberOfStreamDivisions(n);
340  }
341  unsigned int
342  GetNumberOfStreamDivisions() const override
343  {
344  return Superclass::GetNumberOfStreamDivisions();
345  }
346 
347 
348 #ifdef ITK_USE_CONCEPT_CHECKING
349  // Begin concept checking
350  itkConceptMacro(InputHasNumericTraitsCheck, (Concept::HasNumericTraits<PixelType>));
351  // End concept checking
352 #endif
353 
354 protected:
356  ~LabelStatisticsImageFilter() override = default;
357  void
358  PrintSelf(std::ostream & os, Indent indent) const override;
359 
360  void
362  {
363  this->AllocateOutputs();
364  m_LabelStatistics.clear();
365  }
366 
369  void
370  AfterStreamedGenerateData() override;
371 
372  void
373  ThreadedStreamedGenerateData(const RegionType &) override;
374 
375 private:
376  void
377  MergeMap(MapType &, MapType &) const;
378 
381 
383 
385 
388 
389  std::mutex m_Mutex;
390 
391 }; // end of class
392 } // end namespace itk
393 
394 #ifndef ITK_MANUAL_INSTANTIATION
395 # include "itkLabelStatisticsImageFilter.hxx"
396 #endif
397 
398 #endif
itk::LabelStatisticsImageFilter::LabelStatistics::m_Variance
RealType m_Variance
Definition: itkLabelStatisticsImageFilter.h:231
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:98
itk::LabelStatisticsImageFilter::MapIterator
typename MapType::iterator MapIterator
Definition: itkLabelStatisticsImageFilter.h:238
itk::LabelStatisticsImageFilter::SizeType
typename TInputImage::SizeType SizeType
Definition: itkLabelStatisticsImageFilter.h:82
itk::Concept::HasNumericTraits
Definition: itkConceptChecking.h:714
itk::LabelStatisticsImageFilter::LabelIndexType
typename TLabelImage::IndexType LabelIndexType
Definition: itkLabelStatisticsImageFilter.h:91
itk::LabelStatisticsImageFilter::LabelImageType
TLabelImage LabelImageType
Definition: itkLabelStatisticsImageFilter.h:87
itk::LabelStatisticsImageFilter::SetNumberOfStreamDivisions
void SetNumberOfStreamDivisions(const unsigned int n) override
Definition: itkLabelStatisticsImageFilter.h:337
itk::LabelStatisticsImageFilter::LabelImagePointer
typename TLabelImage::Pointer LabelImagePointer
Definition: itkLabelStatisticsImageFilter.h:88
itk::LabelStatisticsImageFilter::PixelType
typename TInputImage::PixelType PixelType
Definition: itkLabelStatisticsImageFilter.h:84
itk::LabelStatisticsImageFilter::m_LowerBound
RealType m_LowerBound
Definition: itkLabelStatisticsImageFilter.h:386
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:97
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:225
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:237
itk::LabelStatisticsImageFilter::GetNumberOfLabels
MapSizeType GetNumberOfLabels() const
Definition: itkLabelStatisticsImageFilter.h:278
itk::LabelStatisticsImageFilter::m_ValidLabelValues
ValidLabelValuesContainerType m_ValidLabelValues
Definition: itkLabelStatisticsImageFilter.h:380
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LabelStatisticsImageFilter::m_LabelStatistics
MapType m_LabelStatistics
Definition: itkLabelStatisticsImageFilter.h:379
itk::LabelStatisticsImageFilter::LabelStatistics::m_Count
IdentifierType m_Count
Definition: itkLabelStatisticsImageFilter.h:224
itk::LabelStatisticsImageFilter::GetNumberOfObjects
MapSizeType GetNumberOfObjects() const
Definition: itkLabelStatisticsImageFilter.h:272
itk::LabelStatisticsImageFilter::LabelPixelType
typename TLabelImage::PixelType LabelPixelType
Definition: itkLabelStatisticsImageFilter.h:92
itk::LabelStatisticsImageFilter::ValidLabelValuesContainerType
std::vector< LabelPixelType > ValidLabelValuesContainerType
Definition: itkLabelStatisticsImageFilter.h:243
itk::LabelStatisticsImageFilter::HistogramPointer
typename HistogramType::Pointer HistogramPointer
Definition: itkLabelStatisticsImageFilter.h:111
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageToImageFilter
Base class for filters that take an image as input and produce an image as output.
Definition: itkImageToImageFilter.h:108
itk::Statistics::Histogram::MeasurementVectorType
typename Superclass::MeasurementVectorType MeasurementVectorType
Definition: itkHistogram.h:101
itk::LabelStatisticsImageFilter::LabelStatistics::m_Maximum
RealType m_Maximum
Definition: itkLabelStatisticsImageFilter.h:226
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:206
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::LabelStatisticsImageFilter::IndexType
typename TInputImage::IndexType IndexType
Definition: itkLabelStatisticsImageFilter.h:83
itk::LabelStatisticsImageFilter
Given an intensity image and a label map, compute min, max, variance and mean of the pixels associate...
Definition: itkLabelStatisticsImageFilter.h:62
itk::LabelStatisticsImageFilter::BoundingBoxType
std::vector< IndexValueType > BoundingBoxType
Definition: itkLabelStatisticsImageFilter.h:107
itk::LabelStatisticsImageFilter::MapConstIterator
typename MapType::const_iterator MapConstIterator
Definition: itkLabelStatisticsImageFilter.h:239
itk::LabelStatisticsImageFilter::BeforeStreamedGenerateData
void BeforeStreamedGenerateData() override
Definition: itkLabelStatisticsImageFilter.h:361
itk::LabelStatisticsImageFilter::LabelSizeType
typename TLabelImage::SizeType LabelSizeType
Definition: itkLabelStatisticsImageFilter.h:90
itk::LabelStatisticsImageFilter::HasLabel
bool HasLabel(LabelPixelType label) const
Definition: itkLabelStatisticsImageFilter.h:265
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:167
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk::LabelStatisticsImageFilter::LabelStatistics::m_BoundingBox
BoundingBoxType m_BoundingBox
Definition: itkLabelStatisticsImageFilter.h:232
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:65
itk::LabelStatisticsImageFilter::LabelStatistics::m_Histogram
HistogramType::Pointer m_Histogram
Definition: itkLabelStatisticsImageFilter.h:233
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::LabelStatisticsImageFilter::m_Mutex
std::mutex m_Mutex
Definition: itkLabelStatisticsImageFilter.h:389
itk::LabelStatisticsImageFilter::m_NumBins
HistogramType::SizeType m_NumBins
Definition: itkLabelStatisticsImageFilter.h:384
itk::LabelStatisticsImageFilter::LabelStatistics::m_Sigma
RealType m_Sigma
Definition: itkLabelStatisticsImageFilter.h:230
itk::LabelStatisticsImageFilter::LabelStatistics::m_SumOfSquares
RealType m_SumOfSquares
Definition: itkLabelStatisticsImageFilter.h:229
itk::Array<::itk::SizeValueType >
itk::LabelStatisticsImageFilter::LabelStatistics::LabelStatistics
LabelStatistics()
Definition: itkLabelStatisticsImageFilter.h:121
itkNumericTraits.h
itk::LabelStatisticsImageFilter::RegionType
typename TInputImage::RegionType RegionType
Definition: itkLabelStatisticsImageFilter.h:81
itk::LabelStatisticsImageFilter::GetValidLabelValues
virtual const ValidLabelValuesContainerType & GetValidLabelValues() const
Definition: itkLabelStatisticsImageFilter.h:252
itk::LabelStatisticsImageFilter::LabelRegionType
typename TLabelImage::RegionType LabelRegionType
Definition: itkLabelStatisticsImageFilter.h:89
itkSimpleDataObjectDecorator.h
itk::LabelStatisticsImageFilter::LabelStatistics::LabelStatistics
LabelStatistics(const LabelStatistics &l)
Definition: itkLabelStatisticsImageFilter.h:188
itk::LabelStatisticsImageFilter::MapSizeType
IdentifierType MapSizeType
Definition: itkLabelStatisticsImageFilter.h:240
itk::LabelStatisticsImageFilter::LabelStatistics::LabelStatistics
LabelStatistics(int size, RealType lowerBound, RealType upperBound)
Definition: itkLabelStatisticsImageFilter.h:148
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:342
itk::IdentifierType
SizeValueType IdentifierType
Definition: itkIntTypes.h:87
itk::LabelStatisticsImageFilter::LabelStatistics::m_Sum
RealType m_Sum
Definition: itkLabelStatisticsImageFilter.h:228
itk::LabelStatisticsImageFilter::LabelStatistics::m_Mean
RealType m_Mean
Definition: itkLabelStatisticsImageFilter.h:227
itk::LabelStatisticsImageFilter::m_UseHistograms
bool m_UseHistograms
Definition: itkLabelStatisticsImageFilter.h:382
itk::LabelStatisticsImageFilter::m_UpperBound
RealType m_UpperBound
Definition: itkLabelStatisticsImageFilter.h:387
itk::Array::SetSize
void SetSize(SizeValueType sz)
itk::LabelStatisticsImageFilter::LabelStatistics
Statistics stored per label.
Definition: itkLabelStatisticsImageFilter.h:117