00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00063 template < class TMeasurement = float, unsigned int VMeasurementVectorSize = 1,
00064 class TFrequencyContainer = DenseFrequencyContainer< float > >
00065 class ITK_EXPORT Histogram
00066 : public Sample < FixedArray< TMeasurement, VMeasurementVectorSize > >
00067 {
00068 public:
00070 typedef Histogram Self ;
00071 typedef Sample< FixedArray< TMeasurement, VMeasurementVectorSize > > Superclass ;
00072 typedef SmartPointer<Self> Pointer ;
00073
00075 itkTypeMacro(Histogram, Sample) ;
00076
00078 itkNewMacro(Self) ;
00079
00081 itkStaticConstMacro(MeasurementVectorSize, unsigned int,
00082 VMeasurementVectorSize);
00083
00085 typedef TMeasurement MeasurementType ;
00086
00088 typedef typename Superclass::MeasurementVectorType MeasurementVectorType ;
00089 typedef typename Superclass::InstanceIdentifier InstanceIdentifier ;
00090 typedef MeasurementVectorType ValueType ;
00091
00093 typedef TFrequencyContainer FrequencyContainerType ;
00094 typedef typename FrequencyContainerType::Pointer FrequencyContainerPointer ;
00095
00097 typedef typename FrequencyContainerType::FrequencyType FrequencyType ;
00098
00100 typedef itk::Index< VMeasurementVectorSize > IndexType;
00101 typedef typename IndexType::IndexValueType IndexValueType;
00102
00104 typedef itk::Size< VMeasurementVectorSize > SizeType ;
00105 typedef typename SizeType::SizeValueType SizeValueType ;
00106
00108 typedef std::vector< MeasurementType > BinMinVectorType ;
00109 typedef std::vector< MeasurementType > BinMaxVectorType ;
00110 typedef std::vector< BinMinVectorType > BinMinContainerType ;
00111 typedef std::vector< BinMaxVectorType > BinMaxContainerType ;
00112
00117 void Initialize(const SizeType &size) ;
00118
00119
00125 void Initialize(const SizeType &size, MeasurementVectorType lowerBound,
00126 MeasurementVectorType upperBound) ;
00127
00129 IndexType& GetIndex(const MeasurementVectorType &measurement) ;
00130
00134 IndexType& GetIndex(const InstanceIdentifier &id) ;
00135
00138 bool IsIndexOutOfBound(const IndexType &index) const;
00139
00143 InstanceIdentifier GetInstanceIdentifier(const IndexType &index) const ;
00144
00146 unsigned int GetNumberOfInstances() const ;
00147
00149 unsigned int Size() const ;
00150
00152 unsigned int Size(const unsigned int &dimension) const
00153 { return static_cast< int >(m_Size[dimension]) ; }
00154
00156 SizeType GetSize() const
00157 { return m_Size ; }
00158
00160 SizeValueType GetSize(const unsigned int dimension) const
00161 {
00162 return m_Size[dimension] ;
00163 }
00164
00166 MeasurementType& GetBinMin(const unsigned int dimension,
00167 const unsigned long nbin)
00168 { return m_Min[dimension][nbin] ; }
00169
00171 MeasurementType& GetBinMax(const unsigned int dimension,
00172 const unsigned long nbin)
00173 { return m_Max[dimension][nbin] ; }
00174
00176 void SetBinMin(const unsigned int dimension, const unsigned long nbin,
00177 const MeasurementType min)
00178 { m_Min[dimension][nbin] = min ; }
00179
00181 void SetBinMax(const unsigned int dimension,
00182 unsigned long nbin, const MeasurementType max)
00183 { m_Max[dimension][nbin] = max ; }
00184
00187 MeasurementType& GetBinMinFromValue(const unsigned int dimension,
00188 const float value ) const ;
00189
00192 MeasurementType& GetBinMaxFromValue(const unsigned int dimension,
00193 const float value ) const ;
00194
00196 BinMinVectorType& GetDimensionMins(const unsigned int dimension) const
00197 { return m_Min[dimension] ; }
00198
00200 BinMaxVectorType& GetDimensionMaxs(const unsigned int dimension) const
00201 { return m_Max[dimension] ; }
00202
00204 BinMinContainerType& GetMins() const
00205 { return m_Min ; }
00206
00208 BinMaxContainerType& GetMaxs() const
00209 { return m_Max ; }
00210
00212 MeasurementVectorType& GetHistogramMinFromValue(const MeasurementVectorType
00213 &measurement) ;
00214
00216 MeasurementVectorType& GetHistogramMaxFromValue(const MeasurementVectorType
00217 &measurement) ;
00218
00220 MeasurementVectorType& GetHistogramMinFromIndex(const IndexType &index) ;
00221
00223 MeasurementVectorType& GetHistogramMaxFromIndex(const IndexType &index) ;
00224
00226 FrequencyType GetFrequency(const InstanceIdentifier &id) const
00227 { return m_FrequencyContainer->GetFrequency(id) ; }
00228
00230 FrequencyType GetFrequency(const IndexType &index) const ;
00231
00233 void SetFrequency(const FrequencyType value) ;
00234
00236 void SetFrequency(const InstanceIdentifier &id, const FrequencyType value)
00237 { m_FrequencyContainer->SetFrequency(id, value) ; }
00238
00240 void SetFrequency(const IndexType &index,
00241 const FrequencyType value) ;
00242
00244 void SetFrequency(const MeasurementVectorType &measurement,
00245 const FrequencyType value) ;
00246
00247
00250 void IncreaseFrequency(const InstanceIdentifier &id,
00251 const FrequencyType value)
00252 { m_FrequencyContainer->IncreaseFrequency(id, value) ; }
00253
00256 void IncreaseFrequency(const IndexType &index,
00257 const FrequencyType value) ;
00258
00261 void IncreaseFrequency(const MeasurementVectorType &measurement,
00262 const FrequencyType value) ;
00263
00265 MeasurementVectorType& GetMeasurementVector(const InstanceIdentifier &id) ;
00266
00268 MeasurementVectorType& GetMeasurementVector(const IndexType &index) ;
00269
00271 MeasurementType& GetMeasurement(const unsigned long n,
00272 const unsigned int dimension) const ;
00273
00275 FrequencyType GetFrequency(const unsigned long n,
00276 const unsigned int dimension) const ;
00277
00279 FrequencyType GetTotalFrequency(const unsigned int &dimension) const ;
00280
00295 double Quantile(const unsigned int dimension, const double &p) ;
00296
00298 class Iterator ;
00299 friend class Iterator ;
00300
00301 Iterator Begin()
00302 {
00303 Iterator iter(0, this) ;
00304 return iter ;
00305 }
00306
00307 Iterator End()
00308 {
00309 return Iterator(m_OffsetTable[VMeasurementVectorSize], this) ;
00310 }
00311
00312
00313 class Iterator
00314 {
00315 public:
00316 Iterator(){};
00317
00318 Iterator(Pointer histogram)
00319 {
00320 m_Id = 0 ;
00321 m_Histogram = histogram;
00322 }
00323
00324 Iterator(InstanceIdentifier id, Pointer histogram)
00325 : m_Id(id), m_Histogram(histogram)
00326 {}
00327
00328 FrequencyType GetFrequency() const
00329 {
00330 return m_Histogram->GetFrequency(m_Id) ;
00331 }
00332
00333 void SetFrequency(const FrequencyType value)
00334 {
00335 m_Histogram->SetFrequency(m_Id, value);
00336 }
00337
00338 InstanceIdentifier GetInstanceIdentifier() const
00339 { return m_Id ; }
00340
00341 MeasurementVectorType& GetMeasurementVector() const
00342 {
00343 return m_Histogram->GetMeasurementVector(m_Id) ;
00344 }
00345
00346 Iterator& operator++()
00347 {
00348 ++m_Id;
00349 return *this;
00350 }
00351
00352 bool operator!=(const Iterator& it)
00353 { return (m_Id != it.m_Id); }
00354
00355 bool operator==(const Iterator& it)
00356 { return (m_Id == it.m_Id); }
00357
00358 Iterator& operator=(const Iterator& it)
00359 {
00360 m_Id = it.m_Id;
00361 m_Histogram = it.m_Histogram ;
00362 return *this ;
00363 }
00364
00365 Iterator(const Iterator& it)
00366 {
00367 m_Id = it.m_Id;
00368 m_Histogram = it.m_Histogram ;
00369 }
00370
00371 private:
00372
00373 InstanceIdentifier m_Id;
00374
00375
00376 Pointer m_Histogram ;
00377 } ;
00378
00379 protected:
00380 Histogram() ;
00381 virtual ~Histogram() {}
00382 void PrintSelf(std::ostream& os, Indent indent) const;
00383
00384
00385 SizeType m_Size ;
00386
00387
00388 std::vector< std::vector<MeasurementType> > m_Min ;
00389
00390
00391 std::vector< std::vector<MeasurementType> > m_Max ;
00392
00393 private:
00394 Histogram(const Self&);
00395 void operator=(const Self&);
00396
00397 InstanceIdentifier m_OffsetTable[VMeasurementVectorSize + 1] ;
00398 FrequencyContainerPointer m_FrequencyContainer ;
00399 unsigned int m_NumberOfInstances ;
00400 MeasurementVectorType m_TempMeasurementVector ;
00401 IndexType m_TempIndex ;
00402 } ;
00403
00404 }
00405 }
00406
00407 #ifndef ITK_MANUAL_INSTANTIATION
00408 #include "itkHistogram.txx"
00409 #endif
00410
00411 #endif