ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkHistogram.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 
75 template< typename TMeasurement = float,
76  typename TFrequencyContainer = DenseFrequencyContainer2 >
77 class ITK_TEMPLATE_EXPORT Histogram:
78  public Sample< Array< TMeasurement > >
79 {
80 public:
81  ITK_DISALLOW_COPY_AND_ASSIGN(Histogram);
82 
83  // This type serves as the indirect definition of MeasurementVectorType
85 
87  using Self = Histogram;
91 
93  itkTypeMacro(Histogram, Sample);
94 
96  itkNewMacro(Self);
97 
99  using MeasurementType = TMeasurement;
100 
103  using InstanceIdentifier = typename Superclass::InstanceIdentifier;
104  using MeasurementVectorSizeType = typename Superclass::MeasurementVectorSizeType;
105 
107 
109  using FrequencyContainerType = TFrequencyContainer;
110  using FrequencyContainerPointer = typename FrequencyContainerType::Pointer;
111 
113  using AbsoluteFrequencyType = typename FrequencyContainerType::AbsoluteFrequencyType;
114  using TotalAbsoluteFrequencyType = typename FrequencyContainerType::TotalAbsoluteFrequencyType;
115  using RelativeFrequencyType = typename FrequencyContainerType::RelativeFrequencyType;
116  using TotalRelativeFrequencyType = typename FrequencyContainerType::TotalRelativeFrequencyType;
117 
121 
125 
127  using BinMinVectorType = std::vector< MeasurementType >;
128  using BinMaxVectorType = std::vector< MeasurementType >;
129  using BinMinContainerType = std::vector< BinMinVectorType >;
130  using BinMaxContainerType = std::vector< BinMaxVectorType >;
131 
135  void Initialize(const SizeType & size);
136 
140  void Initialize(const SizeType & size, MeasurementVectorType & lowerBound,
141  MeasurementVectorType & upperBound);
142 
144  void SetToZero();
145 
149  bool GetIndex(const MeasurementVectorType & measurement,
150  IndexType & index) const;
151 
155  const IndexType & GetIndex(InstanceIdentifier id) const;
156 
159  itkGetConstMacro(ClipBinsAtEnds, bool);
160 
163  itkSetMacro(ClipBinsAtEnds, bool);
164 
167  bool IsIndexOutOfBounds(const IndexType & index) const;
168 
172  InstanceIdentifier GetInstanceIdentifier(const IndexType & index) const;
173 
175  InstanceIdentifier Size() const override;
176 
178  const SizeType & GetSize() const;
179 
181  SizeValueType GetSize(unsigned int dimension) const;
182 
184  const MeasurementType & GetBinMin(unsigned int dimension,
185  InstanceIdentifier nbin) const;
186 
188  const MeasurementType & GetBinMax(unsigned int dimension,
189  InstanceIdentifier nbin) const;
190 
192  void SetBinMin(unsigned int dimension, InstanceIdentifier nbin,
193  MeasurementType min);
194 
196  void SetBinMax(unsigned int dimension,
198 
201  const MeasurementType & GetBinMinFromValue(unsigned int dimension,
202  float value) const;
203 
206  const MeasurementType & GetBinMaxFromValue(unsigned int dimension,
207  float value) const;
208 
210  const BinMinVectorType & GetDimensionMins(unsigned int dimension) const;
211 
213  const BinMaxVectorType & GetDimensionMaxs(unsigned int dimension) const;
214 
216  const BinMinContainerType & GetMins() const;
217 
219  const BinMaxContainerType & GetMaxs() const;
220 
222  const MeasurementVectorType & GetHistogramMinFromIndex(const IndexType & index) const;
223 
225  const MeasurementVectorType & GetHistogramMaxFromIndex(const IndexType & index) const;
226 
228  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override;
229 
231  AbsoluteFrequencyType GetFrequency(const IndexType & index) const;
232 
234  void SetFrequency(AbsoluteFrequencyType value);
235 
238  bool SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
239 
242  bool SetFrequencyOfIndex(const IndexType & index,
243  AbsoluteFrequencyType value);
244 
247  bool SetFrequencyOfMeasurement(const MeasurementVectorType & measurement,
248  AbsoluteFrequencyType value);
249 
253  bool IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
254 
258  bool IncreaseFrequencyOfIndex(const IndexType & index,
259  AbsoluteFrequencyType value);
260 
268  bool IncreaseFrequencyOfMeasurement(
269  const MeasurementVectorType & measurement,
270  AbsoluteFrequencyType value);
271 
275  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override;
276 
278  const MeasurementVectorType & GetMeasurementVector(const IndexType & index) const;
279 
282  MeasurementType GetMeasurement(InstanceIdentifier n,
283  unsigned int dimension) const;
284 
286  TotalAbsoluteFrequencyType GetTotalFrequency() const override;
287 
290  unsigned int dimension) const;
291 
307  double Quantile(unsigned int dimension, double p) const;
309 
311  double Mean(unsigned int dimension) const;
312 
314  void Graft(const DataObject *) override;
315 
316 protected:
317  void PrintSelf(std::ostream & os, Indent indent) const override;
318 
319 public:
320 
326  {
327 public:
328 
329  friend class Histogram;
330 
331  ConstIterator(const Self *histogram)
332  {
333  m_Id = 0;
334  m_Histogram = histogram;
335  }
336 
338  {
339  m_Id = it.m_Id;
340  m_Histogram = it.m_Histogram;
341  }
342 
344  {
345  m_Id = it.m_Id;
346  m_Histogram = it.m_Histogram;
347  return *this;
348  }
349 
351  {
352  return m_Histogram->GetFrequency(m_Id);
353  }
354 
356  {
357  return m_Id;
358  }
359 
361  {
362  return m_Histogram->GetMeasurementVector(m_Id);
363  }
364 
365  const IndexType & GetIndex() const
366  {
367  return m_Histogram->GetIndex(m_Id);
368  }
369 
371  {
372  ++m_Id;
373  return *this;
374  }
375 
376  bool operator!=(const ConstIterator & it)
377  {
378  return ( m_Id != it.m_Id );
379  }
380 
381  bool operator==(const ConstIterator & it)
382  {
383  return ( m_Id == it.m_Id );
384  }
385 
386 protected:
387 
388  ConstIterator(InstanceIdentifier id, const Self *histogram):
389  m_Id(id), m_Histogram(histogram)
390  {}
391 
392  // ConstIterator pointing DenseFrequencyContainer
394 
395  // Pointer of DenseFrequencyContainer
397 private:
398  ConstIterator() = delete;
399  }; // end of iterator class
400 
405  class Iterator:public ConstIterator
406  {
407 public:
408 
409  Iterator(Self *histogram):ConstIterator(histogram)
410  {}
411 
413  ConstIterator(id, histogram)
414  {}
415 
417  {}
418 
420  {
421  this->ConstIterator::operator=(it);
422  return *this;
423  }
424 
426  {
427  auto * histogram = const_cast< Self * >( this->m_Histogram );
428 
429  return histogram->SetFrequency(this->m_Id, value);
430  }
431 
432 private:
433  // To ensure const-correctness these method must not be in the public API.
434  // The are not implemented, since they should never be called.
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 & operator=(const ConstIterator & it) = delete;
440  }; // end of iterator class
441 
443  {
444  Iterator iter(0, this);
445 
446  return iter;
447  }
448 
450  {
451  return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
452  }
453 
455  {
456  ConstIterator iter(0, this);
457 
458  return iter;
459  }
460 
462  {
463  return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
464  }
465 
466 protected:
467  Histogram();
468  ~Histogram() override = default;
469 
470  // The number of bins for each dimension
472 
473 private:
474  using OffsetTableType = std::vector< InstanceIdentifier >;
477  unsigned int m_NumberOfInstances{0};
478 
479  // This method is provided here just to avoid a "hidden" warning
480  // related to the virtual method available in DataObject.
481  void Initialize() override {}
482 
483  // lower bound of each bin
484  std::vector< std::vector< MeasurementType > > m_Min;
485 
486  // upper bound of each bin
487  std::vector< std::vector< MeasurementType > > m_Max;
488 
491 
492  bool m_ClipBinsAtEnds{true};
493 };
494 } // end of namespace Statistics
495 } // end of namespace itk
496 
497 #ifndef ITK_MANUAL_INSTANTIATION
498 #include "itkHistogram.hxx"
499 #endif
500 
501 #endif
Array class with size defined at construction time.
Definition: itkArray.h:46
ConstIterator(InstanceIdentifier id, const Self *histogram)
Definition: itkHistogram.h:388
std::vector< InstanceIdentifier > OffsetTableType
Definition: itkHistogram.h:474
ConstIterator End() const
Definition: itkHistogram.h:461
ConstIterator(const ConstIterator &it)
Definition: itkHistogram.h:337
class that walks through the elements of the histogram.
Definition: itkHistogram.h:325
TFrequencyContainer FrequencyContainerType
Definition: itkHistogram.h:109
::itk::IndexValueType ValueType
Definition: itkArray.h:51
This class stores measurement vectors in the context of n-dimensional histogram.
Definition: itkHistogram.h:77
std::vector< BinMinVectorType > BinMinContainerType
Definition: itkHistogram.h:129
bool SetFrequency(const AbsoluteFrequencyType value)
Definition: itkHistogram.h:425
void Initialize() override
Definition: itkHistogram.h:481
typename FrequencyContainerType::TotalRelativeFrequencyType TotalRelativeFrequencyType
Definition: itkHistogram.h:116
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkHistogram.h:360
typename FrequencyContainerType::Pointer FrequencyContainerPointer
Definition: itkHistogram.h:110
std::vector< std::vector< MeasurementType > > m_Max
Definition: itkHistogram.h:487
std::vector< std::vector< MeasurementType > > m_Min
Definition: itkHistogram.h:484
const IndexType & GetIndex() const
Definition: itkHistogram.h:365
TMeasurement MeasurementType
Definition: itkHistogram.h:99
OffsetTableType m_OffsetTable
Definition: itkHistogram.h:475
ConstIterator & operator=(const ConstIterator &it)
Definition: itkHistogram.h:343
Iterator(InstanceIdentifier id, Self *histogram)
Definition: itkHistogram.h:412
typename FrequencyContainerType::RelativeFrequencyType RelativeFrequencyType
Definition: itkHistogram.h:115
typename SizeType::ValueType SizeValueType
Definition: itkHistogram.h:124
MeasurementVectorType ValueType
Definition: itkHistogram.h:106
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
TMeasurementVector MeasurementVectorType
Definition: itkSample.h:76
ConstIterator Begin() const
Definition: itkHistogram.h:454
Iterator & operator=(const Iterator &it)
Definition: itkHistogram.h:419
std::vector< MeasurementType > BinMaxVectorType
Definition: itkHistogram.h:128
bool operator==(const ConstIterator &it)
Definition: itkHistogram.h:381
typename FrequencyContainerType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkHistogram.h:114
MeasurementVectorType m_TempMeasurementVector
Definition: itkHistogram.h:489
FrequencyContainerPointer m_FrequencyContainer
Definition: itkHistogram.h:476
typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: itkHistogram.h:104
class that walks through the elements of the histogram.
Definition: itkHistogram.h:405
A collection of measurements for statistical analysis.
Definition: itkSample.h:61
Control indentation during Print() invocation.
Definition: itkIndent.h:49
typename Superclass::MeasurementVectorType MeasurementVectorType
Definition: itkHistogram.h:102
typename IndexType::ValueType IndexValueType
Definition: itkHistogram.h:120
Base class for most ITK classes.
Definition: itkObject.h:60
typename Superclass::InstanceIdentifier InstanceIdentifier
Definition: itkHistogram.h:103
typename FrequencyContainerType::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkHistogram.h:113
std::vector< MeasurementType > BinMinVectorType
Definition: itkHistogram.h:127
Base class for all data objects in ITK.
bool operator!=(const ConstIterator &it)
Definition: itkHistogram.h:376
AbsoluteFrequencyType GetFrequency() const
Definition: itkHistogram.h:350
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkHistogram.h:355
std::vector< BinMaxVectorType > BinMaxContainerType
Definition: itkHistogram.h:130