ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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::Statistics
29{
30
73
74template <typename TMeasurement = float, typename TFrequencyContainer = DenseFrequencyContainer2>
75class ITK_TEMPLATE_EXPORT Histogram : public Sample<Array<TMeasurement>>
76{
77public:
78 ITK_DISALLOW_COPY_AND_MOVE(Histogram);
79
80 // This type serves as the indirect definition of MeasurementVectorType
82
84 using Self = Histogram;
88
90 itkOverrideGetNameOfClassMacro(Histogram);
91
93 itkNewMacro(Self);
94
96 using MeasurementType = TMeasurement;
97
100 using typename Superclass::InstanceIdentifier;
102
104
106 using FrequencyContainerType = TFrequencyContainer;
107 using FrequencyContainerPointer = typename FrequencyContainerType::Pointer;
108
110 using AbsoluteFrequencyType = typename FrequencyContainerType::AbsoluteFrequencyType;
111 using TotalAbsoluteFrequencyType = typename FrequencyContainerType::TotalAbsoluteFrequencyType;
112 using RelativeFrequencyType = typename FrequencyContainerType::RelativeFrequencyType;
113 using TotalRelativeFrequencyType = typename FrequencyContainerType::TotalRelativeFrequencyType;
114
118
122
124 using BinMinVectorType = std::vector<MeasurementType>;
125 using BinMaxVectorType = std::vector<MeasurementType>;
126 using BinMinContainerType = std::vector<BinMinVectorType>;
127 using BinMaxContainerType = std::vector<BinMaxVectorType>;
128
132 void
133 Initialize(const SizeType & size);
134
138 void
139 Initialize(const SizeType & size, MeasurementVectorType & lowerBound, MeasurementVectorType & upperBound);
140
142 void
144
148 bool
149 GetIndex(const MeasurementVectorType & measurement, IndexType & index) const;
150
154 const IndexType &
156
159 itkGetConstMacro(ClipBinsAtEnds, bool);
160 itkSetMacro(ClipBinsAtEnds, bool);
161 itkBooleanMacro(ClipBinsAtEnds);
165 bool
166 IsIndexOutOfBounds(const IndexType & index) const;
167
172 GetInstanceIdentifier(const IndexType & index) const;
173
176 Size() const override;
177
179 const SizeType &
180 GetSize() const;
181
184 GetSize(unsigned int dimension) const;
185
187 const MeasurementType &
188 GetBinMin(unsigned int dimension, InstanceIdentifier nbin) const;
189
191 const MeasurementType &
192 GetBinMax(unsigned int dimension, InstanceIdentifier nbin) const;
193
195 void
196 SetBinMin(unsigned int dimension, InstanceIdentifier nbin, MeasurementType min);
197
199 void
200 SetBinMax(unsigned int dimension, InstanceIdentifier nbin, MeasurementType max);
201
204 const MeasurementType &
205 GetBinMinFromValue(unsigned int dimension, float value) const;
206
209 const MeasurementType &
210 GetBinMaxFromValue(unsigned int dimension, float value) const;
211
213 const BinMinVectorType &
214 GetDimensionMins(unsigned int dimension) const;
215
217 const BinMaxVectorType &
218 GetDimensionMaxs(unsigned int dimension) const;
219
221 const BinMinContainerType &
222 GetMins() const;
223
225 const BinMaxContainerType &
226 GetMaxs() const;
227
231
235
239
242 GetFrequency(const IndexType & index) const;
243
245 void
247
250 bool
252
255 bool
257
260 bool
262
266 bool
268
272 bool
274
282 bool
284
290
293 GetMeasurementVector(const IndexType & index) const;
294
298 GetMeasurement(InstanceIdentifier n, unsigned int dimension) const;
299
302 GetTotalFrequency() const override;
303
306 GetFrequency(InstanceIdentifier n, unsigned int dimension) const;
307
323 double
324 Quantile(unsigned int dimension, double p) const;
325
327 double
328 Mean(unsigned int dimension) const;
329
331 void
332 Graft(const DataObject *) override;
333
334protected:
335 void
336 PrintSelf(std::ostream & os, Indent indent) const override;
337
338public:
345 {
346 public:
347 friend class Histogram;
348
349 ConstIterator() = delete;
350 ConstIterator(const Self * histogram)
351 : m_Id(0)
352 , m_Histogram(histogram)
353 {}
354
356 : m_Id(it.m_Id)
358 {}
359
361 operator=(const ConstIterator & it) = default;
362
363 [[nodiscard]] AbsoluteFrequencyType
365 {
366 return m_Histogram->GetFrequency(m_Id);
367 }
368
369 [[nodiscard]] InstanceIdentifier
371 {
372 return m_Id;
373 }
374
375 [[nodiscard]] const MeasurementVectorType &
377 {
378 return m_Histogram->GetMeasurementVector(m_Id);
379 }
380
381 [[nodiscard]] const IndexType &
382 GetIndex() const
383 {
384 return m_Histogram->GetIndex(m_Id);
385 }
386
389 {
390 ++m_Id;
391 return *this;
392 }
393
394 bool
395 operator==(const ConstIterator & it) const
396 {
397 return (m_Id == it.m_Id);
398 }
399
401
402 protected:
403 ConstIterator(InstanceIdentifier id, const Self * histogram)
404 : m_Id(id)
405 , m_Histogram(histogram)
406 {}
407
408 // ConstIterator pointing DenseFrequencyContainer
410
411 // Pointer of DenseFrequencyContainer
413 }; // end of iterator class
414
420 class Iterator : public ConstIterator
421 {
422 public:
423 Iterator() = delete;
424 Iterator(const Self * histogram) = delete;
425 Iterator(InstanceIdentifier id, const Self * histogram) = delete;
426 Iterator(const ConstIterator & it) = delete;
428 operator=(const ConstIterator & it) = delete;
429
430 Iterator(Self * histogram)
431 : ConstIterator(histogram)
432 {}
433
435 : ConstIterator(id, histogram)
436 {}
437
438 Iterator(const Iterator & it)
439 : ConstIterator(it)
440 {}
441
442 Iterator &
443 operator=(const Iterator & it)
444 {
445 this->ConstIterator::operator=(it);
446 return *this;
447 }
448
449 bool
451 {
452 auto * histogram = const_cast<Self *>(this->m_Histogram);
453
454 return histogram->SetFrequency(this->m_Id, value);
455 }
456 }; // end of iterator class
457
458 Iterator
460 {
461 const Iterator iter(0, this);
462
463 return iter;
464 }
465
466 Iterator
468 {
469 return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
470 }
471
472 ConstIterator
473 Begin() const
474 {
475 const ConstIterator iter(0, this);
476
477 return iter;
478 }
479
480 ConstIterator
481 End() const
482 {
483 return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
484 }
485
486protected:
488 ~Histogram() override = default;
489
490 // The number of bins for each dimension
492
493private:
494 using OffsetTableType = std::vector<InstanceIdentifier>;
497 unsigned int m_NumberOfInstances{ 0 };
498
499 // This method is provided here just to avoid a "hidden" warning
500 // related to the virtual method available in DataObject.
501 void
502 Initialize() override
503 {}
504
505 // lower bound of each bin
506 std::vector<std::vector<MeasurementType>> m_Min{};
507
508 // upper bound of each bin
509 std::vector<std::vector<MeasurementType>> m_Max{};
510
513
514 bool m_ClipBinsAtEnds{ true };
515};
516} // namespace itk::Statistics
517
518#ifndef ITK_MANUAL_INSTANTIATION
519# include "itkHistogram.hxx"
520#endif
521
522#endif
Array class with size defined at construction time.
Definition itkArray.h:48
itk::IndexValueType ValueType
Definition itkArray.h:51
Control indentation during Print() invocation.
Definition itkIndent.h:50
Implements transparent reference counting.
class that walks through the elements of the histogram.
bool operator==(const ConstIterator &it) const
ConstIterator & operator=(const ConstIterator &it)=default
ConstIterator(InstanceIdentifier id, const Self *histogram)
InstanceIdentifier GetInstanceIdentifier() const
AbsoluteFrequencyType GetFrequency() const
const MeasurementVectorType & GetMeasurementVector() const
class that walks through the elements of the histogram.
Iterator(const Self *histogram)=delete
Iterator(const ConstIterator &it)=delete
Iterator & operator=(const Iterator &it)
Iterator(InstanceIdentifier id, const Self *histogram)=delete
ConstIterator & operator=(const ConstIterator &it)=delete
bool SetFrequency(const AbsoluteFrequencyType value)
Iterator(InstanceIdentifier id, Self *histogram)
MeasurementVectorType ValueType
~Histogram() override=default
void Initialize(const SizeType &size, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
SizeValueType GetSize(unsigned int dimension) const
std::vector< std::vector< MeasurementType > > m_Min
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
Sample< ArrayType > Superclass
std::vector< BinMaxVectorType > BinMaxContainerType
typename FrequencyContainerType::Pointer FrequencyContainerPointer
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
Array< itk::IndexValueType > IndexType
const IndexType & GetIndex(InstanceIdentifier id) const
AbsoluteFrequencyType GetFrequency(InstanceIdentifier n, unsigned int dimension) const
typename FrequencyContainerType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
double Quantile(unsigned int dimension, double p) const
FrequencyContainerPointer m_FrequencyContainer
const MeasurementVectorType & GetHistogramMaxFromIndex(const IndexType &index) const
const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override
typename FrequencyContainerType::AbsoluteFrequencyType AbsoluteFrequencyType
const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const
ConstIterator Begin() const
ConstIterator End() const
void SetBinMax(unsigned int dimension, InstanceIdentifier nbin, MeasurementType max)
std::vector< MeasurementType > BinMaxVectorType
const MeasurementType & GetBinMinFromValue(unsigned int dimension, float value) const
typename MeasurementVectorTraits::InstanceIdentifier InstanceIdentifier
Array< TMeasurement > ArrayType
typename FrequencyContainerType::RelativeFrequencyType RelativeFrequencyType
bool IncreaseFrequencyOfIndex(const IndexType &index, AbsoluteFrequencyType value)
typename SizeType::ValueType SizeValueType
const SizeType & GetSize() const
void Graft(const DataObject *) override
TFrequencyContainer FrequencyContainerType
void SetBinMin(unsigned int dimension, InstanceIdentifier nbin, MeasurementType min)
const BinMaxContainerType & GetMaxs() const
Array< itk::SizeValueType > SizeType
typename FrequencyContainerType::TotalRelativeFrequencyType TotalRelativeFrequencyType
TotalAbsoluteFrequencyType GetTotalFrequency() const override
std::vector< std::vector< MeasurementType > > m_Max
std::vector< BinMinVectorType > BinMinContainerType
const MeasurementVectorType & GetHistogramMinFromIndex(const IndexType &index) const
const BinMinContainerType & GetMins() const
void PrintSelf(std::ostream &os, Indent indent) const override
typename IndexType::ValueType IndexValueType
void Initialize(const SizeType &size)
SmartPointer< const Self > ConstPointer
bool IncreaseFrequencyOfMeasurement(const MeasurementVectorType &measurement, AbsoluteFrequencyType value)
AbsoluteFrequencyType GetFrequency(const IndexType &index) const
std::vector< InstanceIdentifier > OffsetTableType
const MeasurementType & GetBinMin(unsigned int dimension, InstanceIdentifier nbin) const
bool SetFrequencyOfIndex(const IndexType &index, AbsoluteFrequencyType value)
ArrayType MeasurementVectorType
Definition itkSample.h:75
SmartPointer< Self > Pointer
bool SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value)
bool IsIndexOutOfBounds(const IndexType &index) const
void SetFrequency(AbsoluteFrequencyType value)
std::vector< MeasurementType > BinMinVectorType
virtual MeasurementVectorSizeType GetMeasurementVectorSize() const
typename MeasurementVectorTraits::InstanceIdentifier InstanceIdentifier
Definition itkSample.h:89