ITK  5.3.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  * 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 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  itkTypeMacro(Histogram, Sample);
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;
109  using FrequencyContainerPointer = typename FrequencyContainerType::Pointer;
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 
161  itkGetConstMacro(ClipBinsAtEnds, bool);
162 
165  itkSetMacro(ClipBinsAtEnds, bool);
166 
169  bool
170  IsIndexOutOfBounds(const IndexType & index) const;
171 
176  GetInstanceIdentifier(const IndexType & index) const;
177 
180  Size() const override;
181 
183  const SizeType &
184  GetSize() const;
185 
188  GetSize(unsigned int dimension) const;
189 
191  const MeasurementType &
192  GetBinMin(unsigned int dimension, InstanceIdentifier nbin) const;
193 
195  const MeasurementType &
196  GetBinMax(unsigned int dimension, InstanceIdentifier nbin) const;
197 
199  void
200  SetBinMin(unsigned int dimension, InstanceIdentifier nbin, MeasurementType min);
201 
203  void
204  SetBinMax(unsigned int dimension, InstanceIdentifier nbin, MeasurementType max);
205 
208  const MeasurementType &
209  GetBinMinFromValue(unsigned int dimension, float value) const;
210 
213  const MeasurementType &
214  GetBinMaxFromValue(unsigned int dimension, float value) const;
215 
217  const BinMinVectorType &
218  GetDimensionMins(unsigned int dimension) const;
219 
221  const BinMaxVectorType &
222  GetDimensionMaxs(unsigned int dimension) const;
223 
225  const BinMinContainerType &
226  GetMins() const;
227 
229  const BinMaxContainerType &
230  GetMaxs() const;
231 
233  const MeasurementVectorType &
234  GetHistogramMinFromIndex(const IndexType & index) const;
235 
237  const MeasurementVectorType &
238  GetHistogramMaxFromIndex(const IndexType & index) const;
239 
242  GetFrequency(InstanceIdentifier id) const override;
243 
246  GetFrequency(const IndexType & index) const;
247 
249  void
250  SetFrequency(AbsoluteFrequencyType value);
251 
254  bool
255  SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
256 
259  bool
260  SetFrequencyOfIndex(const IndexType & index, AbsoluteFrequencyType value);
261 
264  bool
265  SetFrequencyOfMeasurement(const MeasurementVectorType & measurement, AbsoluteFrequencyType value);
266 
270  bool
271  IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
272 
276  bool
277  IncreaseFrequencyOfIndex(const IndexType & index, AbsoluteFrequencyType value);
278 
286  bool
287  IncreaseFrequencyOfMeasurement(const MeasurementVectorType & measurement, AbsoluteFrequencyType value);
288 
292  const MeasurementVectorType &
293  GetMeasurementVector(InstanceIdentifier id) const override;
294 
296  const MeasurementVectorType &
297  GetMeasurementVector(const IndexType & index) const;
298 
302  GetMeasurement(InstanceIdentifier n, unsigned int dimension) const;
303 
306  GetTotalFrequency() const override;
307 
310  GetFrequency(InstanceIdentifier n, unsigned int dimension) const;
311 
327  double
328  Quantile(unsigned int dimension, double p) const;
330 
332  double
333  Mean(unsigned int dimension) const;
334 
336  void
337  Graft(const DataObject *) override;
338 
339 protected:
340  void
341  PrintSelf(std::ostream & os, Indent indent) const override;
342 
343 public:
350  {
351  public:
352  friend class Histogram;
353 
354  ConstIterator(const Self * histogram)
355  {
356  m_Id = 0;
357  m_Histogram = histogram;
358  }
359 
361  {
362  m_Id = it.m_Id;
363  m_Histogram = it.m_Histogram;
364  }
365 
366  ConstIterator &
368  {
369  m_Id = it.m_Id;
370  m_Histogram = it.m_Histogram;
371  return *this;
372  }
373 
375  GetFrequency() const
376  {
377  return m_Histogram->GetFrequency(m_Id);
378  }
379 
382  {
383  return m_Id;
384  }
385 
386  const MeasurementVectorType &
388  {
389  return m_Histogram->GetMeasurementVector(m_Id);
390  }
391 
392  const IndexType &
393  GetIndex() const
394  {
395  return m_Histogram->GetIndex(m_Id);
396  }
397 
398  ConstIterator &
400  {
401  ++m_Id;
402  return *this;
403  }
404 
405  bool
406  operator==(const ConstIterator & it) const
407  {
408  return (m_Id == it.m_Id);
409  }
410 
411  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
412 
413  protected:
414  ConstIterator(InstanceIdentifier id, const Self * histogram)
415  : m_Id(id)
416  , m_Histogram(histogram)
417  {}
418 
419  // ConstIterator pointing DenseFrequencyContainer
421 
422  // Pointer of DenseFrequencyContainer
423  const Self * m_Histogram;
424 
425  private:
426  ConstIterator() = delete;
427  }; // end of iterator class
428 
434  class Iterator : public ConstIterator
435  {
436  public:
437  Iterator() = delete;
438  Iterator(const Self * histogram) = delete;
439  Iterator(InstanceIdentifier id, const Self * histogram) = delete;
440  Iterator(const ConstIterator & it) = delete;
441  ConstIterator &
442  operator=(const ConstIterator & it) = delete;
444 
445  Iterator(Self * histogram)
446  : ConstIterator(histogram)
447  {}
448 
450  : ConstIterator(id, histogram)
451  {}
452 
453  Iterator(const Iterator & it)
454  : ConstIterator(it)
455  {}
456 
457  Iterator &
458  operator=(const Iterator & it)
459  {
460  this->ConstIterator::operator=(it);
461  return *this;
462  }
463 
464  bool
466  {
467  auto * histogram = const_cast<Self *>(this->m_Histogram);
468 
469  return histogram->SetFrequency(this->m_Id, value);
470  }
471  }; // end of iterator class
472 
473  Iterator
475  {
476  Iterator iter(0, this);
477 
478  return iter;
479  }
480 
481  Iterator
482  End()
483  {
484  return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
485  }
486 
487  ConstIterator
488  Begin() const
489  {
490  ConstIterator iter(0, this);
491 
492  return iter;
493  }
494 
495  ConstIterator
496  End() const
497  {
498  return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
499  }
500 
501 protected:
502  Histogram();
503  ~Histogram() override = default;
504 
505  // The number of bins for each dimension
507 
508 private:
509  using OffsetTableType = std::vector<InstanceIdentifier>;
512  unsigned int m_NumberOfInstances{ 0 };
513 
514  // This method is provided here just to avoid a "hidden" warning
515  // related to the virtual method available in DataObject.
516  void
517  Initialize() override
518  {}
519 
520  // lower bound of each bin
521  std::vector<std::vector<MeasurementType>> m_Min;
522 
523  // upper bound of each bin
524  std::vector<std::vector<MeasurementType>> m_Max;
525 
528 
529  bool m_ClipBinsAtEnds{ true };
530 };
531 } // end of namespace Statistics
532 } // end of namespace itk
533 
534 #ifndef ITK_MANUAL_INSTANTIATION
535 # include "itkHistogram.hxx"
536 #endif
537 
538 #endif
itk::Statistics::Histogram::Iterator
class that walks through the elements of the histogram.
Definition: itkHistogram.h:434
itk::Statistics::Histogram::Iterator::operator=
Iterator & operator=(const Iterator &it)
Definition: itkHistogram.h:458
itk::Statistics::Histogram::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkHistogram.h:381
itk::Statistics::Histogram::ConstIterator::m_Histogram
const Self * m_Histogram
Definition: itkHistogram.h:423
itk::Statistics::Histogram::FrequencyContainerPointer
typename FrequencyContainerType::Pointer FrequencyContainerPointer
Definition: itkHistogram.h:109
itk::Statistics::Histogram::Iterator::Iterator
Iterator(const Iterator &it)
Definition: itkHistogram.h:453
itk::Statistics::Histogram::m_TempIndex
IndexType m_TempIndex
Definition: itkHistogram.h:527
itk::Statistics::Histogram::ConstIterator::ConstIterator
ConstIterator(const Self *histogram)
Definition: itkHistogram.h:354
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::Statistics::Histogram::Initialize
void Initialize() override
Definition: itkHistogram.h:517
itk::Statistics::Histogram::BinMinVectorType
std::vector< MeasurementType > BinMinVectorType
Definition: itkHistogram.h:126
itk::Statistics::Histogram::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkHistogram.h:399
itk::SmartPointer< Self >
itk::Statistics::Histogram::Begin
ConstIterator Begin() const
Definition: itkHistogram.h:488
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:474
itkDenseFrequencyContainer2.h
itk::Statistics::Histogram::m_OffsetTable
OffsetTableType m_OffsetTable
Definition: itkHistogram.h:510
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:406
itk::Statistics::Histogram::m_Size
SizeType m_Size
Definition: itkHistogram.h:506
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::m_TempMeasurementVector
MeasurementVectorType m_TempMeasurementVector
Definition: itkHistogram.h:526
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:445
itkSparseFrequencyContainer2.h
itk::Statistics::Histogram::m_FrequencyContainer
FrequencyContainerPointer m_FrequencyContainer
Definition: itkHistogram.h:511
itk::Statistics::Histogram::End
Iterator End()
Definition: itkHistogram.h:482
itk::Statistics::Histogram::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &it)
Definition: itkHistogram.h:367
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::m_Min
std::vector< std::vector< MeasurementType > > m_Min
Definition: itkHistogram.h:521
itk::Statistics::Histogram::OffsetTableType
std::vector< InstanceIdentifier > OffsetTableType
Definition: itkHistogram.h:509
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:375
itkSample.h
itk::Statistics::Histogram::m_Max
std::vector< std::vector< MeasurementType > > m_Max
Definition: itkHistogram.h:524
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:62
itk::Statistics::Histogram::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkHistogram.h:387
itk::Statistics::Histogram::Iterator::Iterator
Iterator(InstanceIdentifier id, Self *histogram)
Definition: itkHistogram.h:449
itk::Statistics::Histogram::ConstIterator::m_Id
InstanceIdentifier m_Id
Definition: itkHistogram.h:420
itk::Statistics::Histogram::ConstIterator::GetIndex
const IndexType & GetIndex() const
Definition: itkHistogram.h:393
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:349
itk::Statistics::Histogram::End
ConstIterator End() const
Definition: itkHistogram.h:496
itk::Statistics::Histogram::ConstIterator::ConstIterator
ConstIterator(InstanceIdentifier id, const Self *histogram)
Definition: itkHistogram.h:414
itk::Statistics::Histogram::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &it)
Definition: itkHistogram.h:360
itk::Statistics::Histogram::Iterator::SetFrequency
bool SetFrequency(const AbsoluteFrequencyType value)
Definition: itkHistogram.h:465
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293