ITK  5.2.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_ASSIGN(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 
102  using InstanceIdentifier = typename Superclass::InstanceIdentifier;
103  using MeasurementVectorSizeType = 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
407  {
408  return (m_Id != it.m_Id);
409  }
410 
411  bool
413  {
414  return (m_Id == it.m_Id);
415  }
416 
417  protected:
418  ConstIterator(InstanceIdentifier id, const Self * histogram)
419  : m_Id(id)
420  , m_Histogram(histogram)
421  {}
422 
423  // ConstIterator pointing DenseFrequencyContainer
425 
426  // Pointer of DenseFrequencyContainer
427  const Self * m_Histogram;
428 
429  private:
430  ConstIterator() = delete;
431  }; // end of iterator class
432 
438  class Iterator : public ConstIterator
439  {
440  public:
441  Iterator() = delete;
442  Iterator(const Self * histogram) = delete;
443  Iterator(InstanceIdentifier id, const Self * histogram) = delete;
444  Iterator(const ConstIterator & it) = delete;
445  ConstIterator &
446  operator=(const ConstIterator & it) = delete;
448 
449  Iterator(Self * histogram)
450  : ConstIterator(histogram)
451  {}
452 
454  : ConstIterator(id, histogram)
455  {}
456 
457  Iterator(const Iterator & it)
458  : ConstIterator(it)
459  {}
460 
461  Iterator &
462  operator=(const Iterator & it)
463  {
464  this->ConstIterator::operator=(it);
465  return *this;
466  }
467 
468  bool
470  {
471  auto * histogram = const_cast<Self *>(this->m_Histogram);
472 
473  return histogram->SetFrequency(this->m_Id, value);
474  }
475  }; // end of iterator class
476 
477  Iterator
479  {
480  Iterator iter(0, this);
481 
482  return iter;
483  }
484 
485  Iterator
486  End()
487  {
488  return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
489  }
490 
491  ConstIterator
492  Begin() const
493  {
494  ConstIterator iter(0, this);
495 
496  return iter;
497  }
498 
499  ConstIterator
500  End() const
501  {
502  return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
503  }
504 
505 protected:
506  Histogram();
507  ~Histogram() override = default;
508 
509  // The number of bins for each dimension
511 
512 private:
513  using OffsetTableType = std::vector<InstanceIdentifier>;
516  unsigned int m_NumberOfInstances{ 0 };
517 
518  // This method is provided here just to avoid a "hidden" warning
519  // related to the virtual method available in DataObject.
520  void
521  Initialize() override
522  {}
523 
524  // lower bound of each bin
525  std::vector<std::vector<MeasurementType>> m_Min;
526 
527  // upper bound of each bin
528  std::vector<std::vector<MeasurementType>> m_Max;
529 
532 
533  bool m_ClipBinsAtEnds{ true };
534 };
535 } // end of namespace Statistics
536 } // end of namespace itk
537 
538 #ifndef ITK_MANUAL_INSTANTIATION
539 # include "itkHistogram.hxx"
540 #endif
541 
542 #endif
itk::Statistics::Histogram::Iterator
class that walks through the elements of the histogram.
Definition: itkHistogram.h:438
itk::Statistics::Histogram::Iterator::operator=
Iterator & operator=(const Iterator &it)
Definition: itkHistogram.h:462
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:427
itk::Statistics::Histogram::FrequencyContainerPointer
typename FrequencyContainerType::Pointer FrequencyContainerPointer
Definition: itkHistogram.h:109
itk::Statistics::Histogram::Iterator::Iterator
Iterator(const Iterator &it)
Definition: itkHistogram.h:457
itk::Statistics::Histogram::m_TempIndex
IndexType m_TempIndex
Definition: itkHistogram.h:531
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:521
itk::Statistics::Sample::MeasurementVectorType
TMeasurementVector MeasurementVectorType
Definition: itkSample.h:77
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:492
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::Histogram::Begin
Iterator Begin()
Definition: itkHistogram.h:478
itkDenseFrequencyContainer2.h
itk::Statistics::Histogram::ConstIterator::operator!=
bool operator!=(const ConstIterator &it)
Definition: itkHistogram.h:406
itk::Statistics::Histogram::m_OffsetTable
OffsetTableType m_OffsetTable
Definition: itkHistogram.h:514
itk::Statistics::Histogram::BinMaxContainerType
std::vector< BinMaxVectorType > BinMaxContainerType
Definition: itkHistogram.h:129
itk::Statistics::Histogram::MeasurementVectorType
typename Superclass::MeasurementVectorType MeasurementVectorType
Definition: itkHistogram.h:101
itk::Statistics::Histogram
This class stores measurement vectors in the context of n-dimensional histogram.
Definition: itkHistogram.h:77
itk::Statistics::Histogram::m_Size
SizeType m_Size
Definition: itkHistogram.h:510
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:530
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:449
itk::Statistics::Histogram::ValueType
MeasurementVectorType ValueType
Definition: itkHistogram.h:105
itkSparseFrequencyContainer2.h
itk::Statistics::Histogram::m_FrequencyContainer
FrequencyContainerPointer m_FrequencyContainer
Definition: itkHistogram.h:515
itk::Statistics::Histogram::End
Iterator End()
Definition: itkHistogram.h:486
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:525
itk::Statistics::Histogram::OffsetTableType
std::vector< InstanceIdentifier > OffsetTableType
Definition: itkHistogram.h:513
itk::Statistics::Histogram::MeasurementVectorSizeType
typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: itkHistogram.h:103
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:528
itk::Statistics::Histogram::ConstIterator::operator==
bool operator==(const ConstIterator &it)
Definition: itkHistogram.h:412
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:60
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:453
itk::Statistics::Histogram::ConstIterator::m_Id
InstanceIdentifier m_Id
Definition: itkHistogram.h:424
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:500
itk::Statistics::Histogram::ConstIterator::ConstIterator
ConstIterator(InstanceIdentifier id, const Self *histogram)
Definition: itkHistogram.h:418
itk::Statistics::Histogram::InstanceIdentifier
typename Superclass::InstanceIdentifier InstanceIdentifier
Definition: itkHistogram.h:102
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:469
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293