ITK  6.0.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
28namespace itk
29{
30namespace Statistics
31{
32
76template <typename TMeasurement = float, typename TFrequencyContainer = DenseFrequencyContainer2>
77class ITK_TEMPLATE_EXPORT Histogram : public Sample<Array<TMeasurement>>
78{
79public:
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
146
150 bool
151 GetIndex(const MeasurementVectorType & measurement, IndexType & index) const;
152
156 const IndexType &
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
233
237
241
244 GetFrequency(const IndexType & index) const;
245
247 void
249
252 bool
254
257 bool
259
262 bool
264
268 bool
270
274 bool
276
284 bool
286
292
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
337protected:
338 void
339 PrintSelf(std::ostream & os, Indent indent) const override;
340
341public:
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
366 {
367 m_Id = it.m_Id;
368 m_Histogram = it.m_Histogram;
369 return *this;
370 }
371
374 {
375 return m_Histogram->GetFrequency(m_Id);
376 }
377
380 {
381 return m_Id;
382 }
383
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
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
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
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;
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
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
499protected:
501 ~Histogram() override = default;
502
503 // The number of bins for each dimension
504 SizeType m_Size{};
505
506private:
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
Array class with size defined at construction time.
Definition: itkArray.h:48
itk::IndexValueType ValueType
Definition: itkArray.h:52
Base class for all data objects in ITK.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Base class for most ITK classes.
Definition: itkObject.h:62
class that walks through the elements of the histogram.
Definition: itkHistogram.h:348
const IndexType & GetIndex() const
Definition: itkHistogram.h:391
bool operator==(const ConstIterator &it) const
Definition: itkHistogram.h:404
ConstIterator(const ConstIterator &it)
Definition: itkHistogram.h:358
ConstIterator(InstanceIdentifier id, const Self *histogram)
Definition: itkHistogram.h:412
ConstIterator & operator=(const ConstIterator &it)
Definition: itkHistogram.h:365
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkHistogram.h:379
AbsoluteFrequencyType GetFrequency() const
Definition: itkHistogram.h:373
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkHistogram.h:385
class that walks through the elements of the histogram.
Definition: itkHistogram.h:433
Iterator(const Self *histogram)=delete
Iterator(const ConstIterator &it)=delete
Iterator & operator=(const Iterator &it)
Definition: itkHistogram.h:456
Iterator(InstanceIdentifier id, const Self *histogram)=delete
ConstIterator & operator=(const ConstIterator &it)=delete
bool SetFrequency(const AbsoluteFrequencyType value)
Definition: itkHistogram.h:463
Iterator(InstanceIdentifier id, Self *histogram)
Definition: itkHistogram.h:447
This class stores measurement vectors in the context of n-dimensional histogram.
Definition: itkHistogram.h:78
~Histogram() override=default
void Initialize(const SizeType &size, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
SizeValueType GetSize(unsigned int dimension) const
void Initialize() override
Definition: itkHistogram.h:515
const BinMaxVectorType & GetDimensionMaxs(unsigned int dimension) const
MeasurementType GetMeasurement(InstanceIdentifier n, unsigned int dimension) const
const MeasurementType & GetBinMax(unsigned int dimension, InstanceIdentifier nbin) const
const MeasurementType & GetBinMaxFromValue(unsigned int dimension, float value) const
bool GetIndex(const MeasurementVectorType &measurement, IndexType &index) const
std::vector< BinMaxVectorType > BinMaxContainerType
Definition: itkHistogram.h:129
typename FrequencyContainerType::Pointer FrequencyContainerPointer
Definition: itkHistogram.h:109
double Mean(unsigned int dimension) const
InstanceIdentifier Size() const override
const BinMinVectorType & GetDimensionMins(unsigned int dimension) const
bool SetFrequencyOfMeasurement(const MeasurementVectorType &measurement, AbsoluteFrequencyType value)
bool IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value)
AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override
InstanceIdentifier GetInstanceIdentifier(const IndexType &index) const
const IndexType & GetIndex(InstanceIdentifier id) const
AbsoluteFrequencyType GetFrequency(InstanceIdentifier n, unsigned int dimension) const
typename FrequencyContainerType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkHistogram.h:113
double Quantile(unsigned int dimension, double p) const
const MeasurementVectorType & GetHistogramMaxFromIndex(const IndexType &index) const
const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override
typename FrequencyContainerType::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkHistogram.h:112
const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const
ConstIterator Begin() const
Definition: itkHistogram.h:486
ConstIterator End() const
Definition: itkHistogram.h:494
void SetBinMax(unsigned int dimension, InstanceIdentifier nbin, MeasurementType max)
std::vector< MeasurementType > BinMaxVectorType
Definition: itkHistogram.h:127
const MeasurementType & GetBinMinFromValue(unsigned int dimension, float value) const
typename FrequencyContainerType::RelativeFrequencyType RelativeFrequencyType
Definition: itkHistogram.h:114
bool IncreaseFrequencyOfIndex(const IndexType &index, AbsoluteFrequencyType value)
typename SizeType::ValueType SizeValueType
Definition: itkHistogram.h:123
const SizeType & GetSize() const
void Graft(const DataObject *) override
TFrequencyContainer FrequencyContainerType
Definition: itkHistogram.h:108
void SetBinMin(unsigned int dimension, InstanceIdentifier nbin, MeasurementType min)
const BinMaxContainerType & GetMaxs() const
typename FrequencyContainerType::TotalRelativeFrequencyType TotalRelativeFrequencyType
Definition: itkHistogram.h:115
TotalAbsoluteFrequencyType GetTotalFrequency() const override
std::vector< BinMinVectorType > BinMinContainerType
Definition: itkHistogram.h:128
const MeasurementVectorType & GetHistogramMinFromIndex(const IndexType &index) const
const BinMinContainerType & GetMins() const
void PrintSelf(std::ostream &os, Indent indent) const override
TMeasurement MeasurementType
Definition: itkHistogram.h:98
typename IndexType::ValueType IndexValueType
Definition: itkHistogram.h:119
void Initialize(const SizeType &size)
bool IncreaseFrequencyOfMeasurement(const MeasurementVectorType &measurement, AbsoluteFrequencyType value)
AbsoluteFrequencyType GetFrequency(const IndexType &index) const
std::vector< InstanceIdentifier > OffsetTableType
Definition: itkHistogram.h:507
const MeasurementType & GetBinMin(unsigned int dimension, InstanceIdentifier nbin) const
bool SetFrequencyOfIndex(const IndexType &index, AbsoluteFrequencyType value)
bool SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value)
bool IsIndexOutOfBounds(const IndexType &index) const
void SetFrequency(AbsoluteFrequencyType value)
std::vector< MeasurementType > BinMinVectorType
Definition: itkHistogram.h:126
A collection of measurements for statistical analysis.
Definition: itkSample.h:63
typename MeasurementVectorTraits::InstanceIdentifier InstanceIdentifier
Definition: itkSample.h:91
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....