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
00243
00244
00245
00246
00247
00248
00249
00250
00252 MeasurementVectorType& GetHistogramMinFromIndex(const IndexType &index) ;
00253
00255 MeasurementVectorType& GetHistogramMaxFromIndex(const IndexType &index) ;
00256
00258 FrequencyType GetFrequency(const InstanceIdentifier &id) const
00259 { return m_FrequencyContainer->GetFrequency(id) ; }
00260
00262 FrequencyType GetFrequency(const IndexType &index) const ;
00263
00265 void SetFrequency(const FrequencyType value) ;
00266
00269 bool SetFrequency(const InstanceIdentifier &id, const FrequencyType value)
00270 { return m_FrequencyContainer->SetFrequency(id, value) ; }
00271
00274 bool SetFrequency(const IndexType &index,
00275 const FrequencyType value) ;
00276
00279 bool SetFrequency(const MeasurementVectorType &measurement,
00280 const FrequencyType value) ;
00281
00282
00286 bool IncreaseFrequency(const InstanceIdentifier &id,
00287 const FrequencyType value)
00288 { return m_FrequencyContainer->IncreaseFrequency(id, value) ; }
00289
00293 bool IncreaseFrequency(const IndexType &index,
00294 const FrequencyType value) ;
00295
00299 bool IncreaseFrequency(const MeasurementVectorType &measurement,
00300 const FrequencyType value) ;
00301
00305 const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
00306
00308 const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const;
00309
00312 MeasurementType GetMeasurement(const unsigned long n,
00313 const unsigned int dimension) const ;
00314
00316 TotalFrequencyType GetTotalFrequency() const ;
00317
00319 FrequencyType GetFrequency(const unsigned long n,
00320 const unsigned int dimension) const ;
00321
00337 double Quantile(const unsigned int dimension, const double &p) const;
00338
00339 protected:
00340 void PrintSelf(std::ostream& os, Indent indent) const;
00341
00342 public:
00345 class Iterator
00346 {
00347 public:
00348 Iterator(){};
00349
00350 Iterator(Self * histogram)
00351 {
00352 m_Id = 0 ;
00353 m_Histogram = histogram;
00354 }
00355
00356 Iterator(InstanceIdentifier id, Self * histogram)
00357 : m_Id(id), m_Histogram(histogram)
00358 {}
00359
00360 FrequencyType GetFrequency() const
00361 {
00362 return m_Histogram->GetFrequency(m_Id) ;
00363 }
00364
00365 bool SetFrequency(const FrequencyType value)
00366 {
00367 return m_Histogram->SetFrequency(m_Id, value);
00368 }
00369
00370 InstanceIdentifier GetInstanceIdentifier() const
00371 { return m_Id ; }
00372
00373 const MeasurementVectorType & GetMeasurementVector() const
00374 {
00375 return m_Histogram->GetMeasurementVector(m_Id) ;
00376 }
00377
00378 Iterator& operator++()
00379 {
00380 ++m_Id;
00381 return *this;
00382 }
00383
00384 bool operator!=(const Iterator& it)
00385 { return (m_Id != it.m_Id); }
00386
00387 bool operator==(const Iterator& it)
00388 { return (m_Id == it.m_Id); }
00389
00390 Iterator& operator=(const Iterator& it)
00391 {
00392 m_Id = it.m_Id;
00393 m_Histogram = it.m_Histogram ;
00394 return *this ;
00395 }
00396
00397 Iterator(const Iterator& it)
00398 {
00399 m_Id = it.m_Id;
00400 m_Histogram = it.m_Histogram ;
00401 }
00402
00403 private:
00404
00405 InstanceIdentifier m_Id;
00406
00407
00408 Self* m_Histogram ;
00409 } ;
00410
00411
00412 class ConstIterator
00413 {
00414 public:
00415 ConstIterator(){};
00416
00417 ConstIterator(const Self * histogram)
00418 {
00419 m_Id = 0 ;
00420 m_Histogram = histogram;
00421 }
00422
00423 ConstIterator(InstanceIdentifier id, const Self * histogram)
00424 : m_Id(id), m_Histogram(histogram)
00425 {}
00426
00427 FrequencyType GetFrequency() const
00428 {
00429 return m_Histogram->GetFrequency(m_Id) ;
00430 }
00431
00432 InstanceIdentifier GetInstanceIdentifier() const
00433 { return m_Id ; }
00434
00435 const MeasurementVectorType & GetMeasurementVector() const
00436 {
00437 return m_Histogram->GetMeasurementVector(m_Id) ;
00438 }
00439
00440 ConstIterator& operator++()
00441 {
00442 ++m_Id;
00443 return *this;
00444 }
00445
00446 bool operator!=(const ConstIterator& it)
00447 { return (m_Id != it.m_Id); }
00448
00449 bool operator==(const ConstIterator& it)
00450 { return (m_Id == it.m_Id); }
00451
00452 ConstIterator& operator=(const ConstIterator& it)
00453 {
00454 m_Id = it.m_Id;
00455 m_Histogram = it.m_Histogram ;
00456 return *this ;
00457 }
00458
00459 ConstIterator(const ConstIterator & it)
00460 {
00461 m_Id = it.m_Id;
00462 m_Histogram = it.m_Histogram ;
00463 }
00464
00465 private:
00466
00467 InstanceIdentifier m_Id;
00468
00469
00470 const Self* m_Histogram ;
00471 } ;
00472
00473 Iterator Begin()
00474 {
00475 Iterator iter(0, this) ;
00476 return iter ;
00477 }
00478
00479 Iterator End()
00480 {
00481 return Iterator(m_OffsetTable[VMeasurementVectorSize], this) ;
00482 }
00483
00484 ConstIterator Begin() const
00485 {
00486 ConstIterator iter(0, this) ;
00487 return iter ;
00488 }
00489
00490 ConstIterator End() const
00491 {
00492 return ConstIterator(m_OffsetTable[VMeasurementVectorSize], this) ;
00493 }
00494
00495 virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s )
00496 {
00497 if( s!= VMeasurementVectorSize )
00498 {
00499 itkExceptionMacro( << "This Histogram class is meant to be used only for "
00500 << "fixed length vectors of length " << VMeasurementVectorSize <<
00501 ". Cannot set this to " << s);
00502 }
00503 }
00504 MeasurementVectorSizeType GetMeasurementVectorSize() const
00505 {
00506 return VMeasurementVectorSize;
00507 }
00508
00509
00510 protected:
00511 Histogram() ;
00512 virtual ~Histogram() {}
00513
00514
00515 SizeType m_Size ;
00516
00517 private:
00518 Histogram(const Self&);
00519 void operator=(const Self&);
00520
00521 InstanceIdentifier m_OffsetTable[VMeasurementVectorSize + 1] ;
00522 FrequencyContainerPointer m_FrequencyContainer ;
00523 unsigned int m_NumberOfInstances ;
00524
00525
00526 std::vector< std::vector<MeasurementType> > m_Min ;
00527
00528
00529 std::vector< std::vector<MeasurementType> > m_Max ;
00530
00531 mutable MeasurementVectorType m_TempMeasurementVector ;
00532 mutable IndexType m_TempIndex ;
00533
00534 bool m_ClipBinsAtEnds;
00535
00536 } ;
00537
00538 }
00539 }
00540
00541 #ifndef ITK_MANUAL_INSTANTIATION
00542 #include "itkHistogram.txx"
00543 #endif
00544
00545 #endif