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
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 > > Superclass ;
00091 typedef SmartPointer<Self> Pointer ;
00092 typedef SmartPointer<const Self> ConstPointer ;
00093
00095 itkTypeMacro(Histogram, Sample) ;
00096
00098 itkNewMacro(Self) ;
00099
00101 itkStaticConstMacro(MeasurementVectorSize, unsigned int,
00102 VMeasurementVectorSize);
00103
00104
00106 typedef TMeasurement MeasurementType ;
00107
00109 typedef typename Superclass::MeasurementVectorType MeasurementVectorType ;
00110 typedef typename Superclass::InstanceIdentifier InstanceIdentifier ;
00111 typedef MeasurementVectorType ValueType ;
00112 typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
00113
00115 typedef TFrequencyContainer FrequencyContainerType ;
00116 typedef typename FrequencyContainerType::Pointer FrequencyContainerPointer ;
00117
00119 typedef typename FrequencyContainerType::FrequencyType FrequencyType ;
00120 typedef typename FrequencyContainerType::TotalFrequencyType TotalFrequencyType ;
00121
00123 typedef itk::Index< VMeasurementVectorSize > IndexType;
00124 typedef typename IndexType::IndexValueType IndexValueType;
00125
00127 typedef itk::Size< VMeasurementVectorSize > SizeType ;
00128 typedef typename SizeType::SizeValueType SizeValueType ;
00129
00131 typedef std::vector< MeasurementType > BinMinVectorType ;
00132 typedef std::vector< MeasurementType > BinMaxVectorType ;
00133 typedef std::vector< BinMinVectorType > BinMinContainerType ;
00134 typedef std::vector< BinMaxVectorType > BinMaxContainerType ;
00135
00139 void Initialize(const SizeType &size) ;
00140
00141
00145 void Initialize(const SizeType &size, MeasurementVectorType& lowerBound,
00146 MeasurementVectorType& upperBound) ;
00147
00149 void SetToZero() ;
00150
00154 const IndexType & GetIndex(const MeasurementVectorType& measurement) const;
00155
00159 bool GetIndex(const MeasurementVectorType & measurement,
00160 IndexType & index ) const;
00161
00165 const IndexType & GetIndex(const InstanceIdentifier &id) const;
00166
00169 itkGetMacro(ClipBinsAtEnds, bool);
00170
00173 itkSetMacro(ClipBinsAtEnds, bool);
00174
00177 bool IsIndexOutOfBounds(const IndexType &index) const;
00178
00182 InstanceIdentifier GetInstanceIdentifier(const IndexType &index) const ;
00183
00185 unsigned int Size() const ;
00186
00188 SizeType GetSize() const
00189 { return m_Size ; }
00190
00192 SizeValueType GetSize(const unsigned int dimension) const
00193 {
00194 return m_Size[dimension] ;
00195 }
00196
00198 const MeasurementType& GetBinMin(const unsigned int dimension,
00199 const unsigned long nbin) const
00200 { return m_Min[dimension][nbin] ; }
00201
00203 const MeasurementType& GetBinMax(const unsigned int dimension,
00204 const unsigned long nbin) const
00205 { return m_Max[dimension][nbin] ; }
00206
00208 void SetBinMin(const unsigned int dimension, const unsigned long nbin,
00209 const MeasurementType min)
00210 { m_Min[dimension][nbin] = min ; }
00211
00213 void SetBinMax(const unsigned int dimension,
00214 unsigned long nbin, const MeasurementType max)
00215 { m_Max[dimension][nbin] = max ; }
00216
00219 const MeasurementType& GetBinMinFromValue(const unsigned int dimension,
00220 const float value ) const ;
00221
00224 const MeasurementType& GetBinMaxFromValue(const unsigned int dimension,
00225 const float value ) const ;
00226
00228 const BinMinVectorType& GetDimensionMins(const unsigned int dimension) const
00229 { return m_Min[dimension] ; }
00230
00232 const BinMaxVectorType& GetDimensionMaxs(const unsigned int dimension) const
00233 { return m_Max[dimension] ; }
00234
00236 const BinMinContainerType& GetMins() const
00237 { return m_Min ; }
00238
00240 const BinMaxContainerType& GetMaxs() const
00241 { return m_Max ; }
00242
00244
00245
00246
00247
00249
00250
00251
00253 MeasurementVectorType& GetHistogramMinFromIndex(const IndexType &index) ;
00254
00256 MeasurementVectorType& GetHistogramMaxFromIndex(const IndexType &index) ;
00257
00259 FrequencyType GetFrequency(const InstanceIdentifier &id) const
00260 { return m_FrequencyContainer->GetFrequency(id) ; }
00261
00263 FrequencyType GetFrequency(const IndexType &index) const ;
00264
00266 void SetFrequency(const FrequencyType value) ;
00267
00270 bool SetFrequency(const InstanceIdentifier &id, const FrequencyType value)
00271 { return m_FrequencyContainer->SetFrequency(id, value) ; }
00272
00275 bool SetFrequency(const IndexType &index,
00276 const FrequencyType value) ;
00277
00280 bool SetFrequency(const MeasurementVectorType &measurement,
00281 const FrequencyType value) ;
00282
00283
00287 bool IncreaseFrequency(const InstanceIdentifier &id,
00288 const FrequencyType value)
00289 { return m_FrequencyContainer->IncreaseFrequency(id, value) ; }
00290
00294 bool IncreaseFrequency(const IndexType &index,
00295 const FrequencyType value) ;
00296
00300 bool IncreaseFrequency(const MeasurementVectorType &measurement,
00301 const FrequencyType value) ;
00302
00306 const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
00307
00309 const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const;
00310
00313 MeasurementType GetMeasurement(const unsigned long n,
00314 const unsigned int dimension) const ;
00315
00317 TotalFrequencyType GetTotalFrequency() const ;
00318
00320 FrequencyType GetFrequency(const unsigned long n,
00321 const unsigned int dimension) const ;
00322
00338 double Quantile(const unsigned int dimension, const double &p) const;
00340
00341 protected:
00342 void PrintSelf(std::ostream& os, Indent indent) const;
00343
00344 public:
00347 class Iterator
00348 {
00349 public:
00350 Iterator(){};
00351
00352 Iterator(Self * histogram)
00353 {
00354 m_Id = 0 ;
00355 m_Histogram = histogram;
00356 }
00357
00358 Iterator(InstanceIdentifier id, Self * histogram)
00359 : m_Id(id), m_Histogram(histogram)
00360 {}
00361
00362 FrequencyType GetFrequency() const
00363 {
00364 return m_Histogram->GetFrequency(m_Id) ;
00365 }
00366
00367 bool SetFrequency(const FrequencyType value)
00368 {
00369 return m_Histogram->SetFrequency(m_Id, value);
00370 }
00371
00372 InstanceIdentifier GetInstanceIdentifier() const
00373 { return m_Id ; }
00374
00375 const MeasurementVectorType & GetMeasurementVector() const
00376 {
00377 return m_Histogram->GetMeasurementVector(m_Id) ;
00378 }
00379
00380 Iterator& operator++()
00381 {
00382 ++m_Id;
00383 return *this;
00384 }
00385
00386 bool operator!=(const Iterator& it)
00387 { return (m_Id != it.m_Id); }
00388
00389 bool operator==(const Iterator& it)
00390 { return (m_Id == it.m_Id); }
00391
00392 Iterator& operator=(const Iterator& it)
00393 {
00394 m_Id = it.m_Id;
00395 m_Histogram = it.m_Histogram ;
00396 return *this ;
00397 }
00398
00399 Iterator(const Iterator& it)
00400 {
00401 m_Id = it.m_Id;
00402 m_Histogram = it.m_Histogram ;
00403 }
00404
00405 private:
00406
00407 InstanceIdentifier m_Id;
00408
00409
00410 Self* m_Histogram ;
00411 } ;
00412
00413
00414 class ConstIterator
00415 {
00416 public:
00417 ConstIterator(){};
00418
00419 ConstIterator(const Self * histogram)
00420 {
00421 m_Id = 0 ;
00422 m_Histogram = histogram;
00423 }
00424
00425 ConstIterator(InstanceIdentifier id, const Self * histogram)
00426 : m_Id(id), m_Histogram(histogram)
00427 {}
00428
00429 FrequencyType GetFrequency() const
00430 {
00431 return m_Histogram->GetFrequency(m_Id) ;
00432 }
00433
00434 InstanceIdentifier GetInstanceIdentifier() const
00435 { return m_Id ; }
00436
00437 const MeasurementVectorType & GetMeasurementVector() const
00438 {
00439 return m_Histogram->GetMeasurementVector(m_Id) ;
00440 }
00441
00442 ConstIterator& operator++()
00443 {
00444 ++m_Id;
00445 return *this;
00446 }
00447
00448 bool operator!=(const ConstIterator& it)
00449 { return (m_Id != it.m_Id); }
00450
00451 bool operator==(const ConstIterator& it)
00452 { return (m_Id == it.m_Id); }
00453
00454 ConstIterator& operator=(const ConstIterator& it)
00455 {
00456 m_Id = it.m_Id;
00457 m_Histogram = it.m_Histogram ;
00458 return *this ;
00459 }
00460
00461 ConstIterator(const ConstIterator & it)
00462 {
00463 m_Id = it.m_Id;
00464 m_Histogram = it.m_Histogram ;
00465 }
00466
00467 private:
00468
00469 InstanceIdentifier m_Id;
00470
00471
00472 const Self* m_Histogram ;
00473 } ;
00474
00475 Iterator Begin()
00476 {
00477 Iterator iter(0, this) ;
00478 return iter ;
00479 }
00480
00481 Iterator End()
00482 {
00483 return Iterator(m_OffsetTable[VMeasurementVectorSize], this) ;
00484 }
00485
00486 ConstIterator Begin() const
00487 {
00488 ConstIterator iter(0, this) ;
00489 return iter ;
00490 }
00491
00492 ConstIterator End() const
00493 {
00494 return ConstIterator(m_OffsetTable[VMeasurementVectorSize], this) ;
00495 }
00496
00497 virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s )
00498 {
00499 if( s!= VMeasurementVectorSize )
00500 {
00501 itkExceptionMacro( << "This Histogram class is meant to be used only for "
00502 << "fixed length vectors of length " << VMeasurementVectorSize <<
00503 ". Cannot set this to " << s);
00504 }
00505 }
00506 MeasurementVectorSizeType GetMeasurementVectorSize() const
00507 {
00508 return VMeasurementVectorSize;
00509 }
00510
00511
00512 protected:
00513 Histogram() ;
00514 virtual ~Histogram() {}
00515
00516
00517 SizeType m_Size ;
00518
00519 private:
00520 Histogram(const Self&);
00521 void operator=(const Self&);
00522
00523 InstanceIdentifier m_OffsetTable[VMeasurementVectorSize + 1] ;
00524 FrequencyContainerPointer m_FrequencyContainer ;
00525 unsigned int m_NumberOfInstances ;
00526
00527
00528 std::vector< std::vector<MeasurementType> > m_Min ;
00529
00530
00531 std::vector< std::vector<MeasurementType> > m_Max ;
00532
00533 mutable MeasurementVectorType m_TempMeasurementVector ;
00534 mutable IndexType m_TempIndex ;
00535
00536 bool m_ClipBinsAtEnds;
00537
00538 } ;
00539
00540 }
00541 }
00542
00543 #ifndef ITK_MANUAL_INSTANTIATION
00544 #include "itkHistogram.txx"
00545 #endif
00546
00547 #endif
00548