ITK  4.2.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 {
38 template< typename THistogram >
40  itkStaticConstMacro(HistogramDimension, unsigned int, THistogram::MeasurementVectorSize);
41 };
42 
85 template< class TMeasurement = float,
86  class TFrequencyContainer = DenseFrequencyContainer2 >
87 class ITK_EXPORT Histogram:
88  public Sample< Array< TMeasurement > >
89 {
90 public:
91 
92  // This type serves as the indirect definition of MeasurementVectorType
94 
96  typedef Histogram Self;
100 
102  itkTypeMacro(Histogram, Sample);
103 
105  itkNewMacro(Self);
106 
108  typedef TMeasurement MeasurementType;
109 
112  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
113  typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
114 
116 
118  typedef TFrequencyContainer FrequencyContainerType;
119  typedef typename FrequencyContainerType::Pointer FrequencyContainerPointer;
120 
122  typedef typename FrequencyContainerType::AbsoluteFrequencyType AbsoluteFrequencyType;
123  typedef typename FrequencyContainerType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
124  typedef typename FrequencyContainerType::RelativeFrequencyType RelativeFrequencyType;
125  typedef typename FrequencyContainerType::TotalRelativeFrequencyType TotalRelativeFrequencyType;
126 
130 
134 
136  typedef std::vector< MeasurementType > BinMinVectorType;
137  typedef std::vector< MeasurementType > BinMaxVectorType;
138  typedef std::vector< BinMinVectorType > BinMinContainerType;
139  typedef std::vector< BinMaxVectorType > BinMaxContainerType;
140 
144  void Initialize(const SizeType & size);
145 
149  void Initialize(const SizeType & size, MeasurementVectorType & lowerBound,
150  MeasurementVectorType & upperBound);
151 
153  void SetToZero();
154 
158  itkLegacyMacro(const IndexType & GetIndex(const MeasurementVectorType & measurement) const);
159 
163  bool GetIndex(const MeasurementVectorType & measurement,
164  IndexType & index) const;
165 
169  const IndexType & GetIndex(InstanceIdentifier id) const;
170 
173  itkGetConstMacro(ClipBinsAtEnds, bool);
174 
177  itkSetMacro(ClipBinsAtEnds, bool);
178 
181  bool IsIndexOutOfBounds(const IndexType & index) const;
182 
186  InstanceIdentifier GetInstanceIdentifier(const IndexType & index) const;
187 
189  InstanceIdentifier Size() const;
190 
192  const SizeType & GetSize() const;
193 
195  SizeValueType GetSize(unsigned int dimension) const;
196 
198  const MeasurementType & GetBinMin(unsigned int dimension,
199  InstanceIdentifier nbin) const;
200 
202  const MeasurementType & GetBinMax(unsigned int dimension,
203  InstanceIdentifier nbin) const;
204 
206  void SetBinMin(unsigned int dimension, InstanceIdentifier nbin,
207  MeasurementType min);
208 
210  void SetBinMax(unsigned int dimension,
212 
215  const MeasurementType & GetBinMinFromValue(unsigned int dimension,
216  float value) const;
217 
220  const MeasurementType & GetBinMaxFromValue(unsigned int dimension,
221  float value) const;
222 
224  const BinMinVectorType & GetDimensionMins(unsigned int dimension) const;
225 
227  const BinMaxVectorType & GetDimensionMaxs(unsigned int dimension) const;
228 
230  const BinMinContainerType & GetMins() const;
231 
233  const BinMaxContainerType & GetMaxs() const;
234 
236  const MeasurementVectorType & GetHistogramMinFromIndex(const IndexType & index) const;
237 
239  const MeasurementVectorType & GetHistogramMaxFromIndex(const IndexType & index) const;
240 
242  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
243 
245  AbsoluteFrequencyType GetFrequency(const IndexType & index) const;
246 
248  void SetFrequency(AbsoluteFrequencyType value);
249 
252  bool SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
253 
256  bool SetFrequencyOfIndex(const IndexType & index,
257  AbsoluteFrequencyType value);
258 
261  bool SetFrequencyOfMeasurement(const MeasurementVectorType & measurement,
262  AbsoluteFrequencyType value);
263 
267  bool IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
268 
272  bool IncreaseFrequencyOfIndex(const IndexType & index,
273  AbsoluteFrequencyType value);
274 
278  bool IncreaseFrequencyOfMeasurement(
279  const MeasurementVectorType & measurement,
280  AbsoluteFrequencyType value);
281 #ifdef ITKV3_COMPATIBILITY
282  //In ITKv4 the member functions are given unique names to dis-ambiguate
283  //the intended behavior. Make aliases of the overloaded calls
284  //for ITKv3 backwards compatibility.
285  bool IncreaseFrequency(const IndexType & index,
286  AbsoluteFrequencyType value)
287  {
288  return IncreaseFrequencyOfIndex(index,value);
289  }
291 
292  bool IncreaseFrequency(
293  const MeasurementVectorType & measurement,
294  AbsoluteFrequencyType value)
295  {
296  return IncreaseFrequencyOfMeasurement(measurement,value);
297  }
298 
299 #endif
300 
304  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
305 
307  const MeasurementVectorType & GetMeasurementVector(const IndexType & index) const;
308 
311  MeasurementType GetMeasurement(InstanceIdentifier n,
312  unsigned int dimension) const;
313 
315  TotalAbsoluteFrequencyType GetTotalFrequency() const;
316 
318  AbsoluteFrequencyType GetFrequency(InstanceIdentifier n,
319  unsigned int dimension) const;
320 
336  double Quantile(unsigned int dimension, double p) const;
338 
340  double Mean(unsigned int dimension) const;
341 
343  virtual void Graft(const DataObject *);
344 
345 protected:
346  void PrintSelf(std::ostream & os, Indent indent) const;
347 
348 public:
349 
355  {
356 public:
357 
358  friend class Histogram;
359 
360  ConstIterator(const Self *histogram)
361  {
362  m_Id = 0;
363  m_Histogram = histogram;
364  }
365 
367  {
368  m_Id = it.m_Id;
369  m_Histogram = it.m_Histogram;
370  }
371 
372  ConstIterator & operator=(const ConstIterator & it)
373  {
374  m_Id = it.m_Id;
375  m_Histogram = it.m_Histogram;
376  return *this;
377  }
378 
379  AbsoluteFrequencyType GetFrequency() const
380  {
381  return m_Histogram->GetFrequency(m_Id);
382  }
383 
384  InstanceIdentifier GetInstanceIdentifier() const
385  {
386  return m_Id;
387  }
388 
389  const MeasurementVectorType & GetMeasurementVector() const
390  {
391  return m_Histogram->GetMeasurementVector(m_Id);
392  }
393 
394  const IndexType & GetIndex() const
395  {
396  return m_Histogram->GetIndex(m_Id);
397  }
398 
399  ConstIterator & operator++()
400  {
401  ++m_Id;
402  return *this;
403  }
404 
405  bool operator!=(const ConstIterator & it)
406  {
407  return ( m_Id != it.m_Id );
408  }
409 
410  bool operator==(const ConstIterator & it)
411  {
412  return ( m_Id == it.m_Id );
413  }
414 
415 protected:
416  // This method is purposely not implemented
417  ConstIterator();
418 
419  ConstIterator(InstanceIdentifier id, const Self *histogram):
420  m_Id(id), m_Histogram(histogram)
421  {}
422 
423  // ConstIterator pointing DenseFrequencyContainer
425 
426  // Pointer of DenseFrequencyContainer
428  }; // end of iterator class
429 
434  class Iterator:public ConstIterator
435  {
436 public:
437 
438  Iterator(Self *histogram):ConstIterator(histogram)
439  {}
440 
442  ConstIterator(id, histogram)
443  {}
444 
446  {}
447 
448  Iterator & operator=(const Iterator & it)
449  {
450  this->ConstIterator::operator=(it);
451  return *this;
452  }
453 
454  bool SetFrequency(const AbsoluteFrequencyType value)
455  {
456  Self *histogram = const_cast< Self * >( this->m_Histogram );
457 
458  return histogram->SetFrequency(this->m_Id, value);
459  }
460 
461 protected:
462  // To ensure const-correctness these method must not be in the public API.
463  // The are purposly not implemented, since they should never be called.
464  Iterator();
465  Iterator(const Self *histogram);
466  Iterator(InstanceIdentifier id, const Self *histogram);
467  Iterator(const ConstIterator & it);
468  ConstIterator & operator=(const ConstIterator & it);
469 
470 private:
471  }; // end of iterator class
472 
473  Iterator Begin()
474  {
475  Iterator iter(0, this);
476 
477  return iter;
478  }
479 
480  Iterator End()
481  {
482  return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
483  }
484 
485  ConstIterator Begin() const
486  {
487  ConstIterator iter(0, this);
488 
489  return iter;
490  }
491 
492  ConstIterator End() const
493  {
494  return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
495  }
496 
497 protected:
498  Histogram();
499  virtual ~Histogram() {}
500 
501  // The number of bins for each dimension
503 private:
504  Histogram(const Self &); //purposely not implemented
505  void operator=(const Self &); //purposely not implemented
506 
507  typedef std::vector< InstanceIdentifier > OffsetTableType;
510  unsigned int m_NumberOfInstances;
511 
512  // This method is provided here just to avoid a "hidden" warning
513  // related to the virtual method available in DataObject.
514  virtual void Initialize() {}
515 
516  // lower bound of each bin
517  std::vector< std::vector< MeasurementType > > m_Min;
518 
519  // upper bound of each bin
520  std::vector< std::vector< MeasurementType > > m_Max;
521 
524 
526 };
527 } // end of namespace Statistics
528 } // end of namespace itk
529 
530 #ifndef ITK_MANUAL_INSTANTIATION
531 #include "itkHistogram.hxx"
532 #endif
533 
534 #endif
535