Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

Numerics/Statistics/itkHistogram.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkHistogram.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-03-04 15:23:50 $
00007   Version:   $Revision: 1.50 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkHistogram_h
00018 #define __itkHistogram_h
00019 
00020 #include <vector>
00021 
00022 #include "itkIndex.h"
00023 #include "itkSize.h"
00024 #include "itkFixedArray.h"
00025 #include "itkSample.h"
00026 #include "itkDenseFrequencyContainer.h"
00027 #include "itkSparseFrequencyContainer.h"
00028 
00029 namespace itk {
00030 namespace Statistics {
00031 
00038 template <typename THistogram>
00039 struct GetHistogramDimension
00040 {
00041   itkStaticConstMacro(HistogramDimension, unsigned int, THistogram::MeasurementVectorSize);
00042 }; 
00043 
00080 template < class TMeasurement = float, unsigned int VMeasurementVectorSize = 1,
00081            class TFrequencyContainer = DenseFrequencyContainer > 
00082 class ITK_EXPORT Histogram 
00083   : public Sample < FixedArray< TMeasurement, VMeasurementVectorSize > >
00084 {
00085 public:
00086 
00087 
00089   typedef Histogram                 Self;
00090   typedef Sample< FixedArray< TMeasurement, VMeasurementVectorSize > >
00091                                     Superclass;
00092   typedef SmartPointer<Self>        Pointer;
00093   typedef SmartPointer<const Self>  ConstPointer;
00094 
00096   itkTypeMacro(Histogram, Sample);
00097 
00099   itkNewMacro(Self);
00100 
00102   itkStaticConstMacro(MeasurementVectorSize, unsigned int,
00103                       VMeasurementVectorSize);
00104 
00105   
00107   typedef TMeasurement MeasurementType;
00108 
00110   typedef typename Superclass::MeasurementVectorType     MeasurementVectorType;
00111   typedef typename Superclass::InstanceIdentifier        InstanceIdentifier;
00112   typedef MeasurementVectorType                          ValueType;
00113   typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
00114 
00116   typedef TFrequencyContainer                      FrequencyContainerType;
00117   typedef typename FrequencyContainerType::Pointer FrequencyContainerPointer;
00118 
00120   typedef typename FrequencyContainerType::FrequencyType      FrequencyType;
00121   typedef typename FrequencyContainerType::TotalFrequencyType TotalFrequencyType;
00122 
00124   typedef itk::Index< VMeasurementVectorSize >  IndexType;
00125   typedef typename IndexType::IndexValueType    IndexValueType;
00126 
00128   typedef itk::Size< VMeasurementVectorSize > SizeType;
00129   typedef typename SizeType::SizeValueType    SizeValueType;
00130 
00132   typedef std::vector< MeasurementType >  BinMinVectorType;
00133   typedef std::vector< MeasurementType >  BinMaxVectorType;
00134   typedef std::vector< BinMinVectorType > BinMinContainerType;
00135   typedef std::vector< BinMaxVectorType > BinMaxContainerType;
00136 
00140   void Initialize(const SizeType &size);
00141 
00142 
00146   void Initialize(const SizeType &size, MeasurementVectorType& lowerBound,
00147                   MeasurementVectorType& upperBound);
00148 
00150   void SetToZero();
00151 
00155   const IndexType & GetIndex(const MeasurementVectorType& measurement) const;
00156 
00160   bool GetIndex(const MeasurementVectorType & measurement,
00161                 IndexType & index ) const;
00162 
00166   const IndexType & GetIndex(const InstanceIdentifier &id) const;
00167 
00170   itkGetMacro(ClipBinsAtEnds, bool);
00171 
00174   itkSetMacro(ClipBinsAtEnds, bool);
00175 
00178   bool IsIndexOutOfBounds(const IndexType &index) const;
00179 
00183   InstanceIdentifier GetInstanceIdentifier(const IndexType &index) const;
00184 
00186   unsigned int Size() const;
00187 
00189   SizeType GetSize() const
00190     { return m_Size; }
00191 
00193   SizeValueType GetSize(const unsigned int dimension) const
00194     {
00195     return m_Size[dimension]; 
00196     }
00197 
00199   const MeasurementType& GetBinMin(const unsigned int dimension, 
00200                              const unsigned long nbin) const
00201   { return m_Min[dimension][nbin]; }
00202 
00204   const MeasurementType& GetBinMax(const unsigned int dimension,
00205                              const unsigned long nbin) const
00206   { return m_Max[dimension][nbin]; }
00207 
00209   void SetBinMin(const unsigned int dimension, const unsigned long nbin,
00210                  const MeasurementType min)
00211     { m_Min[dimension][nbin] = min; }
00212 
00214   void SetBinMax(const unsigned int dimension, 
00215                  unsigned long nbin, const MeasurementType max)
00216     { m_Max[dimension][nbin] = max; }
00217 
00220   const MeasurementType& GetBinMinFromValue(const unsigned int dimension, 
00221                                       const float value ) const;
00222 
00225   const MeasurementType& GetBinMaxFromValue(const unsigned int dimension, 
00226                                       const float value ) const;
00227 
00229   const BinMinVectorType& GetDimensionMins(const unsigned int dimension) const
00230   { return m_Min[dimension]; }
00231 
00233   const BinMaxVectorType& GetDimensionMaxs(const unsigned int dimension) const
00234   {  return m_Max[dimension]; }
00235 
00237   const BinMinContainerType& GetMins() const
00238     { return m_Min; }
00239 
00241   const BinMaxContainerType& GetMaxs() const
00242     { return m_Max; }
00243 
00245   MeasurementVectorType& GetHistogramMinFromIndex(const IndexType &index);
00246 
00248   MeasurementVectorType& GetHistogramMaxFromIndex(const IndexType &index); 
00249 
00251   FrequencyType GetFrequency(const InstanceIdentifier &id) const
00252   { return m_FrequencyContainer->GetFrequency(id); }
00253 
00255   FrequencyType GetFrequency(const IndexType &index) const;
00256 
00258   void SetFrequency(const FrequencyType value);
00259 
00262   bool SetFrequency(const InstanceIdentifier &id, const FrequencyType value) 
00263   { return m_FrequencyContainer->SetFrequency(id, value); }
00264 
00267   bool SetFrequency(const IndexType &index, 
00268                     const FrequencyType value);
00269 
00272   bool SetFrequency(const MeasurementVectorType &measurement, 
00273                     const FrequencyType value);
00274 
00275 
00279   bool IncreaseFrequency(const InstanceIdentifier &id,
00280                          const FrequencyType value) 
00281   { return m_FrequencyContainer->IncreaseFrequency(id, value); }
00282 
00286   bool IncreaseFrequency(const IndexType &index, 
00287                          const FrequencyType value);
00288 
00292   bool IncreaseFrequency(const MeasurementVectorType &measurement, 
00293                          const FrequencyType value);
00294 
00298   const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
00299 
00301   const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const;
00302 
00305   MeasurementType GetMeasurement(const unsigned long n,
00306                                   const unsigned int dimension) const;
00307 
00309   TotalFrequencyType GetTotalFrequency() const;
00310 
00312   FrequencyType GetFrequency(const unsigned long n,
00313                              const unsigned int dimension) const;
00314 
00330   double Quantile(const unsigned int dimension, const double &p) const;
00332 
00333 protected:
00334   void PrintSelf(std::ostream& os, Indent indent) const;
00335 
00336 public:
00339   class Iterator
00340     {
00341     public:
00342     Iterator(){};
00343     
00344     Iterator(Self * histogram) 
00345       { 
00346       m_Id = 0;
00347       m_Histogram = histogram; 
00348       } 
00349     
00350     Iterator(InstanceIdentifier id, Self * histogram)
00351       : m_Id(id), m_Histogram(histogram) {}
00352     
00353     FrequencyType GetFrequency() const
00354       { 
00355       return  m_Histogram->GetFrequency(m_Id);
00356       }
00357     
00358     bool SetFrequency(const FrequencyType value) 
00359       { 
00360       return m_Histogram->SetFrequency(m_Id, value); 
00361       }
00362 
00363     InstanceIdentifier GetInstanceIdentifier() const
00364       { return m_Id; }
00365 
00366     const MeasurementVectorType & GetMeasurementVector() const
00367       { 
00368       return m_Histogram->GetMeasurementVector(m_Id);
00369       } 
00370 
00371     Iterator& operator++() 
00372       { 
00373       ++m_Id; 
00374       return *this;
00375       }
00376     
00377     bool operator!=(const Iterator& it) 
00378       { return (m_Id != it.m_Id); }
00379     
00380     bool operator==(const Iterator& it) 
00381       { return (m_Id == it.m_Id); }
00382     
00383     Iterator& operator=(const Iterator& it)
00384       { 
00385       m_Id  = it.m_Id;
00386       m_Histogram = it.m_Histogram; 
00387       return *this;
00388       }
00389 
00390     Iterator(const Iterator& it)
00391       { 
00392       m_Id        = it.m_Id;
00393       m_Histogram = it.m_Histogram; 
00394       }
00395    
00396   private:
00397     // Iterator pointing DenseFrequencyContainer
00398     InstanceIdentifier m_Id;
00399     
00400     // Pointer of DenseFrequencyContainer
00401     Self* m_Histogram;
00402   }; // end of iterator class
00403 
00404   // Const Iterator
00405   class ConstIterator
00406     {
00407     public:
00408     ConstIterator(){};
00409       
00410     ConstIterator(const Self * histogram) 
00411       { 
00412       m_Id = 0;
00413       m_Histogram = histogram; 
00414       } 
00415     
00416     ConstIterator(InstanceIdentifier id, const Self * histogram)
00417       : m_Id(id), m_Histogram(histogram) {}
00418     
00419     FrequencyType GetFrequency() const
00420       { 
00421       return  m_Histogram->GetFrequency(m_Id);
00422       }
00423     
00424     InstanceIdentifier GetInstanceIdentifier() const
00425       { return m_Id; }
00426 
00427     const MeasurementVectorType & GetMeasurementVector() const
00428       { 
00429       return m_Histogram->GetMeasurementVector(m_Id);
00430       } 
00431 
00432     ConstIterator& operator++() 
00433       { 
00434       ++m_Id; 
00435       return *this;
00436       }
00437     
00438     bool operator!=(const ConstIterator& it) 
00439       { return (m_Id != it.m_Id); }
00440     
00441     bool operator==(const ConstIterator& it) 
00442       { return (m_Id == it.m_Id); }
00443     
00444     ConstIterator& operator=(const ConstIterator& it)
00445       { 
00446       m_Id  = it.m_Id;
00447       m_Histogram = it.m_Histogram; 
00448       return *this;
00449       }
00450 
00451     ConstIterator(const ConstIterator & it)
00452       { 
00453       m_Id        = it.m_Id;
00454       m_Histogram = it.m_Histogram; 
00455       }
00456    
00457     private:
00458       // ConstIterator pointing DenseFrequencyContainer
00459       InstanceIdentifier m_Id;
00460     
00461       // Pointer of DenseFrequencyContainer
00462       const Self* m_Histogram;
00463   }; // end of iterator class
00464   
00465   Iterator  Begin()
00466     { 
00467     Iterator iter(0, this); 
00468     return iter;
00469     }
00470            
00471   Iterator  End()
00472     {
00473     return Iterator(m_OffsetTable[VMeasurementVectorSize], this);
00474     }
00475   
00476   ConstIterator  Begin() const
00477     { 
00478     ConstIterator iter(0, this); 
00479     return iter;
00480     }
00481            
00482   ConstIterator End() const
00483     {
00484     return ConstIterator(m_OffsetTable[VMeasurementVectorSize], this);
00485     }
00486 
00487   virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s )
00488     {
00489     if( s!= VMeasurementVectorSize )
00490       { 
00491       itkExceptionMacro( << "This Histogram class is meant to be used only for "
00492                          << "fixed length vectors of length " << VMeasurementVectorSize  << 
00493                          ". Cannot set this to " << s);
00494       }
00495     }
00496   MeasurementVectorSizeType GetMeasurementVectorSize() const
00497     {
00498     return VMeasurementVectorSize;
00499     }
00500 
00501 protected:
00502   Histogram();
00503   virtual ~Histogram() {}
00504 
00505   // The number of bins for each dimension
00506   SizeType m_Size;
00507   
00508 private:
00509   Histogram(const Self&); //purposely not implemented
00510   void operator=(const Self&); //purposely not implemented
00511 
00512   InstanceIdentifier          m_OffsetTable[VMeasurementVectorSize + 1];
00513   FrequencyContainerPointer   m_FrequencyContainer;
00514   unsigned int                m_NumberOfInstances;
00515 
00516   // lower bound of each bin
00517   std::vector< std::vector<MeasurementType> > m_Min;
00518   
00519   // upper bound of each bin
00520   std::vector< std::vector<MeasurementType> > m_Max;
00521   
00522   mutable MeasurementVectorType   m_TempMeasurementVector;
00523   mutable IndexType               m_TempIndex;
00524 
00525   bool                            m_ClipBinsAtEnds;
00526 
00527 }; 
00528 
00529 } // end of namespace Statistics 
00530 } // end of namespace itk 
00531 
00532 #ifndef ITK_MANUAL_INSTANTIATION
00533 #include "itkHistogram.txx"
00534 #endif
00535 
00536 #endif
00537 

Generated at Tue Sep 15 03:12:00 2009 for ITK by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2000