ITK  4.4.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< class TMeasurement = float,
76  class TFrequencyContainer = DenseFrequencyContainer2 >
77 class ITK_EXPORT Histogram:
78  public Sample< Array< TMeasurement > >
79 {
80 public:
81 
82  // This type serves as the indirect definition of MeasurementVectorType
84 
86  typedef Histogram Self;
90 
92  itkTypeMacro(Histogram, Sample);
93 
95  itkNewMacro(Self);
96 
98  typedef TMeasurement MeasurementType;
99 
102  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
103  typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
104 
106 
108  typedef TFrequencyContainer FrequencyContainerType;
109  typedef typename FrequencyContainerType::Pointer FrequencyContainerPointer;
110 
112  typedef typename FrequencyContainerType::AbsoluteFrequencyType AbsoluteFrequencyType;
113  typedef typename FrequencyContainerType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
114  typedef typename FrequencyContainerType::RelativeFrequencyType RelativeFrequencyType;
115  typedef typename FrequencyContainerType::TotalRelativeFrequencyType TotalRelativeFrequencyType;
116 
120 
124 
126  typedef std::vector< MeasurementType > BinMinVectorType;
127  typedef std::vector< MeasurementType > BinMaxVectorType;
128  typedef std::vector< BinMinVectorType > BinMinContainerType;
129  typedef std::vector< BinMaxVectorType > BinMaxContainerType;
130 
134  void Initialize(const SizeType & size);
135 
139  void Initialize(const SizeType & size, MeasurementVectorType & lowerBound,
140  MeasurementVectorType & upperBound);
141 
143  void SetToZero();
144 
148  itkLegacyMacro(const IndexType & GetIndex(const MeasurementVectorType & measurement) const);
149 
153  bool GetIndex(const MeasurementVectorType & measurement,
154  IndexType & index) const;
155 
159  const IndexType & GetIndex(InstanceIdentifier id) const;
160 
163  itkGetConstMacro(ClipBinsAtEnds, bool);
164 
167  itkSetMacro(ClipBinsAtEnds, bool);
168 
171  bool IsIndexOutOfBounds(const IndexType & index) const;
172 
176  InstanceIdentifier GetInstanceIdentifier(const IndexType & index) const;
177 
179  InstanceIdentifier Size() const;
180 
182  const SizeType & GetSize() const;
183 
185  SizeValueType GetSize(unsigned int dimension) const;
186 
188  const MeasurementType & GetBinMin(unsigned int dimension,
189  InstanceIdentifier nbin) const;
190 
192  const MeasurementType & GetBinMax(unsigned int dimension,
193  InstanceIdentifier nbin) const;
194 
196  void SetBinMin(unsigned int dimension, InstanceIdentifier nbin,
197  MeasurementType min);
198 
200  void SetBinMax(unsigned int dimension,
202 
205  const MeasurementType & GetBinMinFromValue(unsigned int dimension,
206  float value) const;
207 
210  const MeasurementType & GetBinMaxFromValue(unsigned int dimension,
211  float value) const;
212 
214  const BinMinVectorType & GetDimensionMins(unsigned int dimension) const;
215 
217  const BinMaxVectorType & GetDimensionMaxs(unsigned int dimension) const;
218 
220  const BinMinContainerType & GetMins() const;
221 
223  const BinMaxContainerType & GetMaxs() const;
224 
226  const MeasurementVectorType & GetHistogramMinFromIndex(const IndexType & index) const;
227 
229  const MeasurementVectorType & GetHistogramMaxFromIndex(const IndexType & index) const;
230 
232  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
233 
235  AbsoluteFrequencyType GetFrequency(const IndexType & index) const;
236 
238  void SetFrequency(AbsoluteFrequencyType value);
239 
242  bool SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
243 
246  bool SetFrequencyOfIndex(const IndexType & index,
247  AbsoluteFrequencyType value);
248 
251  bool SetFrequencyOfMeasurement(const MeasurementVectorType & measurement,
252  AbsoluteFrequencyType value);
253 
257  bool IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
258 
262  bool IncreaseFrequencyOfIndex(const IndexType & index,
263  AbsoluteFrequencyType value);
264 
268  bool IncreaseFrequencyOfMeasurement(
269  const MeasurementVectorType & measurement,
270  AbsoluteFrequencyType value);
271 #ifdef ITKV3_COMPATIBILITY
272  //In ITKv4 the member functions are given unique names to dis-ambiguate
273  //the intended behavior. Make aliases of the overloaded calls
274  //for ITKv3 backwards compatibility.
275  bool IncreaseFrequency(const IndexType & index,
276  AbsoluteFrequencyType value)
277  {
278  return IncreaseFrequencyOfIndex(index,value);
279  }
281 
282  bool IncreaseFrequency(
283  const MeasurementVectorType & measurement,
284  AbsoluteFrequencyType value)
285  {
286  return IncreaseFrequencyOfMeasurement(measurement,value);
287  }
288 
289 #endif
290 
294  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
295 
297  const MeasurementVectorType & GetMeasurementVector(const IndexType & index) const;
298 
301  MeasurementType GetMeasurement(InstanceIdentifier n,
302  unsigned int dimension) const;
303 
305  TotalAbsoluteFrequencyType GetTotalFrequency() const;
306 
308  AbsoluteFrequencyType GetFrequency(InstanceIdentifier n,
309  unsigned int dimension) const;
310 
326  double Quantile(unsigned int dimension, double p) const;
328 
330  double Mean(unsigned int dimension) const;
331 
333  virtual void Graft(const DataObject *);
334 
335 protected:
336  void PrintSelf(std::ostream & os, Indent indent) const;
337 
338 public:
339 
345  {
346 public:
347 
348  friend class Histogram;
349 
350  ConstIterator(const Self *histogram)
351  {
352  m_Id = 0;
353  m_Histogram = histogram;
354  }
355 
357  {
358  m_Id = it.m_Id;
359  m_Histogram = it.m_Histogram;
360  }
361 
362  ConstIterator & operator=(const ConstIterator & it)
363  {
364  m_Id = it.m_Id;
365  m_Histogram = it.m_Histogram;
366  return *this;
367  }
368 
369  AbsoluteFrequencyType GetFrequency() const
370  {
371  return m_Histogram->GetFrequency(m_Id);
372  }
373 
374  InstanceIdentifier GetInstanceIdentifier() const
375  {
376  return m_Id;
377  }
378 
379  const MeasurementVectorType & GetMeasurementVector() const
380  {
381  return m_Histogram->GetMeasurementVector(m_Id);
382  }
383 
384  const IndexType & GetIndex() const
385  {
386  return m_Histogram->GetIndex(m_Id);
387  }
388 
389  ConstIterator & operator++()
390  {
391  ++m_Id;
392  return *this;
393  }
394 
395  bool operator!=(const ConstIterator & it)
396  {
397  return ( m_Id != it.m_Id );
398  }
399 
400  bool operator==(const ConstIterator & it)
401  {
402  return ( m_Id == it.m_Id );
403  }
404 
405 protected:
406  // This method is purposely not implemented
407  ConstIterator();
408 
409  ConstIterator(InstanceIdentifier id, const Self *histogram):
410  m_Id(id), m_Histogram(histogram)
411  {}
412 
413  // ConstIterator pointing DenseFrequencyContainer
415 
416  // Pointer of DenseFrequencyContainer
418  }; // end of iterator class
419 
424  class Iterator:public ConstIterator
425  {
426 public:
427 
428  Iterator(Self *histogram):ConstIterator(histogram)
429  {}
430 
432  ConstIterator(id, histogram)
433  {}
434 
436  {}
437 
438  Iterator & operator=(const Iterator & it)
439  {
440  this->ConstIterator::operator=(it);
441  return *this;
442  }
443 
444  bool SetFrequency(const AbsoluteFrequencyType value)
445  {
446  Self *histogram = const_cast< Self * >( this->m_Histogram );
447 
448  return histogram->SetFrequency(this->m_Id, value);
449  }
450 
451 protected:
452  // To ensure const-correctness these method must not be in the public API.
453  // The are purposly not implemented, since they should never be called.
454  Iterator();
455  Iterator(const Self *histogram);
456  Iterator(InstanceIdentifier id, const Self *histogram);
457  Iterator(const ConstIterator & it);
458  ConstIterator & operator=(const ConstIterator & it);
459 
460 private:
461  }; // end of iterator class
462 
463  Iterator Begin()
464  {
465  Iterator iter(0, this);
466 
467  return iter;
468  }
469 
470  Iterator End()
471  {
472  return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
473  }
474 
475  ConstIterator Begin() const
476  {
477  ConstIterator iter(0, this);
478 
479  return iter;
480  }
481 
482  ConstIterator End() const
483  {
484  return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
485  }
486 
487 protected:
488  Histogram();
489  virtual ~Histogram() {}
490 
491  // The number of bins for each dimension
493 
494 private:
495  Histogram(const Self &); //purposely not implemented
496  void operator=(const Self &); //purposely not implemented
497 
498  typedef std::vector< InstanceIdentifier > OffsetTableType;
501  unsigned int m_NumberOfInstances;
502 
503  // This method is provided here just to avoid a "hidden" warning
504  // related to the virtual method available in DataObject.
505  virtual void Initialize() {}
506 
507  // lower bound of each bin
508  std::vector< std::vector< MeasurementType > > m_Min;
509 
510  // upper bound of each bin
511  std::vector< std::vector< MeasurementType > > m_Max;
512 
515 
517 };
518 } // end of namespace Statistics
519 } // end of namespace itk
520 
521 #ifndef ITK_MANUAL_INSTANTIATION
522 #include "itkHistogram.hxx"
523 #endif
524 
525 #endif
526