ITK  5.4.0
Insight Toolkit
itkHistogram.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 itkHistogram_h
19 #define itkHistogram_h
20 
21 #include <vector>
22 
23 #include "itkArray.h"
24 #include "itkSample.h"
27 
28 namespace itk
29 {
30 namespace Statistics
31 {
32 
76 template <typename TMeasurement = float, typename TFrequencyContainer = DenseFrequencyContainer2>
77 class ITK_TEMPLATE_EXPORT Histogram : public Sample<Array<TMeasurement>>
78 {
79 public:
80  ITK_DISALLOW_COPY_AND_MOVE(Histogram);
81 
82  // This type serves as the indirect definition of MeasurementVectorType
84 
86  using Self = Histogram;
90 
92  itkOverrideGetNameOfClassMacro(Histogram);
93 
95  itkNewMacro(Self);
96 
98  using MeasurementType = TMeasurement;
99 
101  using typename Superclass::MeasurementVectorType;
102  using typename Superclass::InstanceIdentifier;
103  using typename Superclass::MeasurementVectorSizeType;
104 
106 
108  using FrequencyContainerType = TFrequencyContainer;
110 
112  using AbsoluteFrequencyType = typename FrequencyContainerType::AbsoluteFrequencyType;
113  using TotalAbsoluteFrequencyType = typename FrequencyContainerType::TotalAbsoluteFrequencyType;
114  using RelativeFrequencyType = typename FrequencyContainerType::RelativeFrequencyType;
115  using TotalRelativeFrequencyType = typename FrequencyContainerType::TotalRelativeFrequencyType;
116 
120 
124 
126  using BinMinVectorType = std::vector<MeasurementType>;
127  using BinMaxVectorType = std::vector<MeasurementType>;
128  using BinMinContainerType = std::vector<BinMinVectorType>;
129  using BinMaxContainerType = std::vector<BinMaxVectorType>;
130 
134  void
135  Initialize(const SizeType & size);
136 
140  void
141  Initialize(const SizeType & size, MeasurementVectorType & lowerBound, MeasurementVectorType & upperBound);
142 
144  void
145  SetToZero();
146 
150  bool
151  GetIndex(const MeasurementVectorType & measurement, IndexType & index) const;
152 
156  const IndexType &
157  GetIndex(InstanceIdentifier id) const;
158 
160  itkGetConstMacro(ClipBinsAtEnds, bool);
161  itkSetMacro(ClipBinsAtEnds, bool);
162  itkBooleanMacro(ClipBinsAtEnds);
167  bool
168  IsIndexOutOfBounds(const IndexType & index) const;
169 
174  GetInstanceIdentifier(const IndexType & index) const;
175 
178  Size() const override;
179 
181  const SizeType &
182  GetSize() const;
183 
186  GetSize(unsigned int dimension) const;
187 
189  const MeasurementType &
190  GetBinMin(unsigned int dimension, InstanceIdentifier nbin) const;
191 
193  const MeasurementType &
194  GetBinMax(unsigned int dimension, InstanceIdentifier nbin) const;
195 
197  void
198  SetBinMin(unsigned int dimension, InstanceIdentifier nbin, MeasurementType min);
199 
201  void
202  SetBinMax(unsigned int dimension, InstanceIdentifier nbin, MeasurementType max);
203 
206  const MeasurementType &
207  GetBinMinFromValue(unsigned int dimension, float value) const;
208 
211  const MeasurementType &
212  GetBinMaxFromValue(unsigned int dimension, float value) const;
213 
215  const BinMinVectorType &
216  GetDimensionMins(unsigned int dimension) const;
217 
219  const BinMaxVectorType &
220  GetDimensionMaxs(unsigned int dimension) const;
221 
223  const BinMinContainerType &
224  GetMins() const;
225 
227  const BinMaxContainerType &
228  GetMaxs() const;
229 
231  const MeasurementVectorType &
232  GetHistogramMinFromIndex(const IndexType & index) const;
233 
235  const MeasurementVectorType &
236  GetHistogramMaxFromIndex(const IndexType & index) const;
237 
240  GetFrequency(InstanceIdentifier id) const override;
241 
244  GetFrequency(const IndexType & index) const;
245 
247  void
248  SetFrequency(AbsoluteFrequencyType value);
249 
252  bool
253  SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
254 
257  bool
258  SetFrequencyOfIndex(const IndexType & index, AbsoluteFrequencyType value);
259 
262  bool
263  SetFrequencyOfMeasurement(const MeasurementVectorType & measurement, AbsoluteFrequencyType value);
264 
268  bool
269  IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
270 
274  bool
275  IncreaseFrequencyOfIndex(const IndexType & index, AbsoluteFrequencyType value);
276 
284  bool
285  IncreaseFrequencyOfMeasurement(const MeasurementVectorType & measurement, AbsoluteFrequencyType value);
286 
290  const MeasurementVectorType &
291  GetMeasurementVector(InstanceIdentifier id) const override;
292 
294  const MeasurementVectorType &
295  GetMeasurementVector(const IndexType & index) const;
296 
300  GetMeasurement(InstanceIdentifier n, unsigned int dimension) const;
301 
304  GetTotalFrequency() const override;
305 
308  GetFrequency(InstanceIdentifier n, unsigned int dimension) const;
309 
325  double
326  Quantile(unsigned int dimension, double p) const;
330  double
331  Mean(unsigned int dimension) const;
332 
334  void
335  Graft(const DataObject *) override;
336 
337 protected:
338  void
339  PrintSelf(std::ostream & os, Indent indent) const override;
340 
341 public:
348  {
349  public:
350  friend class Histogram;
351 
352  ConstIterator(const Self * histogram)
353  {
354  m_Id = 0;
355  m_Histogram = histogram;
356  }
357 
359  {
360  m_Id = it.m_Id;
361  m_Histogram = it.m_Histogram;
362  }
363 
364  ConstIterator &
366  {
367  m_Id = it.m_Id;
368  m_Histogram = it.m_Histogram;
369  return *this;
370  }
371 
373  GetFrequency() const
374  {
375  return m_Histogram->GetFrequency(m_Id);
376  }
377 
380  {
381  return m_Id;
382  }
383 
384  const MeasurementVectorType &
386  {
387  return m_Histogram->GetMeasurementVector(m_Id);
388  }
389 
390  const IndexType &
391  GetIndex() const
392  {
393  return m_Histogram->GetIndex(m_Id);
394  }
395 
396  ConstIterator &
398  {
399  ++m_Id;
400  return *this;
401  }
402 
403  bool
404  operator==(const ConstIterator & it) const
405  {
406  return (m_Id == it.m_Id);
407  }
408 
409  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
410 
411  protected:
412  ConstIterator(InstanceIdentifier id, const Self * histogram)
413  : m_Id(id)
414  , m_Histogram(histogram)
415  {}
416 
417  // ConstIterator pointing DenseFrequencyContainer
419 
420  // Pointer of DenseFrequencyContainer
421  const Self * m_Histogram;
422 
423  private:
424  ConstIterator() = delete;
425  }; // end of iterator class
426 
432  class Iterator : public ConstIterator
433  {
434  public:
435  Iterator() = delete;
436  Iterator(const Self * histogram) = delete;
437  Iterator(InstanceIdentifier id, const Self * histogram) = delete;
438  Iterator(const ConstIterator & it) = delete;
439  ConstIterator &
440  operator=(const ConstIterator & it) = delete;
443  Iterator(Self * histogram)
444  : ConstIterator(histogram)
445  {}
446 
448  : ConstIterator(id, histogram)
449  {}
450 
451  Iterator(const Iterator & it)
452  : ConstIterator(it)
453  {}
454 
455  Iterator &
456  operator=(const Iterator & it)
457  {
458  this->ConstIterator::operator=(it);
459  return *this;
460  }
461 
462  bool
464  {
465  auto * histogram = const_cast<Self *>(this->m_Histogram);
466 
467  return histogram->SetFrequency(this->m_Id, value);
468  }
469  }; // end of iterator class
470 
471  Iterator
473  {
474  Iterator iter(0, this);
475 
476  return iter;
477  }
478 
479  Iterator
480  End()
481  {
482  return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
483  }
484 
485  ConstIterator
486  Begin() const
487  {
488  ConstIterator iter(0, this);
489 
490  return iter;
491  }
492 
493  ConstIterator
494  End() const
495  {
496  return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
497  }
498 
499 protected:
500  Histogram();
501  ~Histogram() override = default;
502 
503  // The number of bins for each dimension
504  SizeType m_Size{};
505 
506 private:
507  using OffsetTableType = std::vector<InstanceIdentifier>;
508  OffsetTableType m_OffsetTable{};
509  FrequencyContainerPointer m_FrequencyContainer{};
510  unsigned int m_NumberOfInstances{ 0 };
511 
512  // This method is provided here just to avoid a "hidden" warning
513  // related to the virtual method available in DataObject.
514  void
515  Initialize() override
516  {}
517 
518  // lower bound of each bin
519  std::vector<std::vector<MeasurementType>> m_Min{};
520 
521  // upper bound of each bin
522  std::vector<std::vector<MeasurementType>> m_Max{};
523 
524  mutable MeasurementVectorType m_TempMeasurementVector{};
525  mutable IndexType m_TempIndex{};
526 
527  bool m_ClipBinsAtEnds{ true };
528 };
529 } // end of namespace Statistics
530 } // end of namespace itk
531 
532 #ifndef ITK_MANUAL_INSTANTIATION
533 # include "itkHistogram.hxx"
534 #endif
535 
536 #endif
itk::Statistics::Histogram::Iterator
class that walks through the elements of the histogram.
Definition: itkHistogram.h:432
itk::Statistics::Histogram::Iterator::operator=
Iterator & operator=(const Iterator &it)
Definition: itkHistogram.h:456
itk::Statistics::Histogram::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkHistogram.h:379
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::Statistics::Histogram::ConstIterator::m_Histogram
const Self * m_Histogram
Definition: itkHistogram.h:421
itk::Statistics::Histogram::FrequencyContainerPointer
typename FrequencyContainerType::Pointer FrequencyContainerPointer
Definition: itkHistogram.h:109
itk::Statistics::Histogram::Iterator::Iterator
Iterator(const Iterator &it)
Definition: itkHistogram.h:451
itk::Statistics::Histogram::ConstIterator::ConstIterator
ConstIterator(const Self *histogram)
Definition: itkHistogram.h:352
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:71
itk::Statistics::Histogram::Initialize
void Initialize() override
Definition: itkHistogram.h:515
itk::Statistics::Histogram::BinMinVectorType
std::vector< MeasurementType > BinMinVectorType
Definition: itkHistogram.h:126
itk::Statistics::Histogram::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkHistogram.h:397
itk::SmartPointer< Self >
itk::Statistics::Histogram::Begin
ConstIterator Begin() const
Definition: itkHistogram.h:486
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::Sample< Array< TMeasurement > >::InstanceIdentifier
typename MeasurementVectorTraits::InstanceIdentifier InstanceIdentifier
Definition: itkSample.h:91
itk::Statistics::Histogram::Begin
Iterator Begin()
Definition: itkHistogram.h:472
itkDenseFrequencyContainer2.h
itk::Statistics::Histogram::BinMaxContainerType
std::vector< BinMaxVectorType > BinMaxContainerType
Definition: itkHistogram.h:129
itk::Statistics::Histogram
This class stores measurement vectors in the context of n-dimensional histogram.
Definition: itkHistogram.h:77
itk::Statistics::Histogram::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: itkHistogram.h:404
itk::Statistics::Histogram::BinMinContainerType
std::vector< BinMinVectorType > BinMinContainerType
Definition: itkHistogram.h:128
itk::Statistics::Histogram::TotalAbsoluteFrequencyType
typename FrequencyContainerType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkHistogram.h:113
itk::Statistics::Histogram::RelativeFrequencyType
typename FrequencyContainerType::RelativeFrequencyType RelativeFrequencyType
Definition: itkHistogram.h:114
itk::Statistics::Histogram::SizeValueType
typename SizeType::ValueType SizeValueType
Definition: itkHistogram.h:123
itk::Statistics::Histogram::AbsoluteFrequencyType
typename FrequencyContainerType::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkHistogram.h:112
itk::Statistics::Histogram::Iterator::Iterator
Iterator(Self *histogram)
Definition: itkHistogram.h:443
itkSparseFrequencyContainer2.h
itk::Statistics::Histogram::End
Iterator End()
Definition: itkHistogram.h:480
itk::Statistics::Histogram::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &it)
Definition: itkHistogram.h:365
itkArray.h
itk::Statistics::Histogram::BinMaxVectorType
std::vector< MeasurementType > BinMaxVectorType
Definition: itkHistogram.h:127
itk::Statistics::Histogram::TotalRelativeFrequencyType
typename FrequencyContainerType::TotalRelativeFrequencyType TotalRelativeFrequencyType
Definition: itkHistogram.h:115
itk::Statistics::Histogram::OffsetTableType
std::vector< InstanceIdentifier > OffsetTableType
Definition: itkHistogram.h:507
itk::Array< itk::IndexValueType >::ValueType
itk::IndexValueType ValueType
Definition: itkArray.h:52
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Statistics::Histogram::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkHistogram.h:373
itkSample.h
itk::Statistics::Histogram::IndexValueType
typename IndexType::ValueType IndexValueType
Definition: itkHistogram.h:119
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:47
itk::Statistics::Histogram::FrequencyContainerType
TFrequencyContainer FrequencyContainerType
Definition: itkHistogram.h:108
itk::Statistics::Sample
A collection of measurements for statistical analysis.
Definition: itkSample.h:62
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::Statistics::Histogram::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkHistogram.h:385
itk::Statistics::Histogram::Iterator::Iterator
Iterator(InstanceIdentifier id, Self *histogram)
Definition: itkHistogram.h:447
itk::Statistics::Histogram::ConstIterator::m_Id
InstanceIdentifier m_Id
Definition: itkHistogram.h:418
itk::Statistics::Histogram::ConstIterator::GetIndex
const IndexType & GetIndex() const
Definition: itkHistogram.h:391
itk::Statistics::Histogram::MeasurementType
TMeasurement MeasurementType
Definition: itkHistogram.h:98
itk::Statistics::Histogram::ConstIterator
class that walks through the elements of the histogram.
Definition: itkHistogram.h:347
itk::Statistics::Histogram::End
ConstIterator End() const
Definition: itkHistogram.h:494
itk::Statistics::Histogram::ConstIterator::ConstIterator
ConstIterator(InstanceIdentifier id, const Self *histogram)
Definition: itkHistogram.h:412
itk::Statistics::Histogram::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &it)
Definition: itkHistogram.h:358
itk::Statistics::Histogram::Iterator::SetFrequency
bool SetFrequency(const AbsoluteFrequencyType value)
Definition: itkHistogram.h:463
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293