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
00122 typedef itk::Index< VMeasurementVectorSize > IndexType;
00123 typedef typename IndexType::IndexValueType IndexValueType;
00124
00126 typedef itk::Size< VMeasurementVectorSize > SizeType ;
00127 typedef typename SizeType::SizeValueType SizeValueType ;
00128
00130 typedef std::vector< MeasurementType > BinMinVectorType ;
00131 typedef std::vector< MeasurementType > BinMaxVectorType ;
00132 typedef std::vector< BinMinVectorType > BinMinContainerType ;
00133 typedef std::vector< BinMaxVectorType > BinMaxContainerType ;
00134
00138 void Initialize(const SizeType &size) ;
00139
00140
00144 void Initialize(const SizeType &size, MeasurementVectorType& lowerBound,
00145 MeasurementVectorType& upperBound) ;
00146
00148 void SetToZero() ;
00149
00153 const IndexType & GetIndex(const MeasurementVectorType& measurement) const;
00154
00158 bool GetIndex(const MeasurementVectorType & measurement,
00159 IndexType & index ) const;
00160
00164 const IndexType & GetIndex(const InstanceIdentifier &id) const;
00165
00168 itkGetMacro(ClipBinsAtEnds, bool);
00169
00172 itkSetMacro(ClipBinsAtEnds, bool);
00173
00176 bool IsIndexOutOfBounds(const IndexType &index) const;
00177
00181 InstanceIdentifier GetInstanceIdentifier(const IndexType &index) const ;
00182
00184 unsigned int Size() const ;
00185
00187 SizeType GetSize() const
00188 { return m_Size ; }
00189
00191 SizeValueType GetSize(const unsigned int dimension) const
00192 {
00193 return m_Size[dimension] ;
00194 }
00195
00197 const MeasurementType& GetBinMin(const unsigned int dimension,
00198 const unsigned long nbin) const
00199 { return m_Min[dimension][nbin] ; }
00200
00202 const MeasurementType& GetBinMax(const unsigned int dimension,
00203 const unsigned long nbin) const
00204 { return m_Max[dimension][nbin] ; }
00205
00207 void SetBinMin(const unsigned int dimension, const unsigned long nbin,
00208 const MeasurementType min)
00209 { m_Min[dimension][nbin] = min ; }
00210
00212 void SetBinMax(const unsigned int dimension,
00213 unsigned long nbin, const MeasurementType max)
00214 { m_Max[dimension][nbin] = max ; }
00215
00218 const MeasurementType& GetBinMinFromValue(const unsigned int dimension,
00219 const float value ) const ;
00220
00223 const MeasurementType& GetBinMaxFromValue(const unsigned int dimension,
00224 const float value ) const ;
00225
00227 const BinMinVectorType& GetDimensionMins(const unsigned int dimension) const
00228 { return m_Min[dimension] ; }
00229
00231 const BinMaxVectorType& GetDimensionMaxs(const unsigned int dimension) const
00232 { return m_Max[dimension] ; }
00233
00235 const BinMinContainerType& GetMins() const
00236 { return m_Min ; }
00237
00239 const BinMaxContainerType& GetMaxs() const
00240 { return m_Max ; }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00251 MeasurementVectorType& GetHistogramMinFromIndex(const IndexType &index) ;
00252
00254 MeasurementVectorType& GetHistogramMaxFromIndex(const IndexType &index) ;
00255
00257 FrequencyType GetFrequency(const InstanceIdentifier &id) const
00258 { return m_FrequencyContainer->GetFrequency(id) ; }
00259
00261 FrequencyType GetFrequency(const IndexType &index) const ;
00262
00264 void SetFrequency(const FrequencyType value) ;
00265
00268 bool SetFrequency(const InstanceIdentifier &id, const FrequencyType value)
00269 { return m_FrequencyContainer->SetFrequency(id, value) ; }
00270
00273 bool SetFrequency(const IndexType &index,
00274 const FrequencyType value) ;
00275
00278 bool SetFrequency(const MeasurementVectorType &measurement,
00279 const FrequencyType value) ;
00280
00281
00285 bool IncreaseFrequency(const InstanceIdentifier &id,
00286 const FrequencyType value)
00287 { return m_FrequencyContainer->IncreaseFrequency(id, value) ; }
00288
00292 bool IncreaseFrequency(const IndexType &index,
00293 const FrequencyType value) ;
00294
00298 bool IncreaseFrequency(const MeasurementVectorType &measurement,
00299 const FrequencyType value) ;
00300
00304 const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
00305
00307 const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const;
00308
00311 MeasurementType GetMeasurement(const unsigned long n,
00312 const unsigned int dimension) const ;
00313
00315 FrequencyType GetTotalFrequency() const ;
00316
00318 FrequencyType GetFrequency(const unsigned long n,
00319 const unsigned int dimension) const ;
00320
00336 double Quantile(const unsigned int dimension, const double &p) const;
00337
00338 protected:
00339 void PrintSelf(std::ostream& os, Indent indent) const;
00340
00341 public:
00344 class Iterator
00345 {
00346 public:
00347 Iterator(){};
00348
00349 Iterator(Self * histogram)
00350 {
00351 m_Id = 0 ;
00352 m_Histogram = histogram;
00353 }
00354
00355 Iterator(InstanceIdentifier id, Self * histogram)
00356 : m_Id(id), m_Histogram(histogram)
00357 {}
00358
00359 FrequencyType GetFrequency() const
00360 {
00361 return m_Histogram->GetFrequency(m_Id) ;
00362 }
00363
00364 bool SetFrequency(const FrequencyType value)
00365 {
00366 return m_Histogram->SetFrequency(m_Id, value);
00367 }
00368
00369 InstanceIdentifier GetInstanceIdentifier() const
00370 { return m_Id ; }
00371
00372 const MeasurementVectorType & GetMeasurementVector() const
00373 {
00374 return m_Histogram->GetMeasurementVector(m_Id) ;
00375 }
00376
00377 Iterator& operator++()
00378 {
00379 ++m_Id;
00380 return *this;
00381 }
00382
00383 bool operator!=(const Iterator& it)
00384 { return (m_Id != it.m_Id); }
00385
00386 bool operator==(const Iterator& it)
00387 { return (m_Id == it.m_Id); }
00388
00389 Iterator& operator=(const Iterator& it)
00390 {
00391 m_Id = it.m_Id;
00392 m_Histogram = it.m_Histogram ;
00393 return *this ;
00394 }
00395
00396 Iterator(const Iterator& it)
00397 {
00398 m_Id = it.m_Id;
00399 m_Histogram = it.m_Histogram ;
00400 }
00401
00402 private:
00403
00404 InstanceIdentifier m_Id;
00405
00406
00407 Self* m_Histogram ;
00408 } ;
00409
00410
00411 class ConstIterator
00412 {
00413 public:
00414 ConstIterator(){};
00415
00416 ConstIterator(const Self * histogram)
00417 {
00418 m_Id = 0 ;
00419 m_Histogram = histogram;
00420 }
00421
00422 ConstIterator(InstanceIdentifier id, const Self * histogram)
00423 : m_Id(id), m_Histogram(histogram)
00424 {}
00425
00426 FrequencyType GetFrequency() const
00427 {
00428 return m_Histogram->GetFrequency(m_Id) ;
00429 }
00430
00431 InstanceIdentifier GetInstanceIdentifier() const
00432 { return m_Id ; }
00433
00434 const MeasurementVectorType & GetMeasurementVector() const
00435 {
00436 return m_Histogram->GetMeasurementVector(m_Id) ;
00437 }
00438
00439 ConstIterator& operator++()
00440 {
00441 ++m_Id;
00442 return *this;
00443 }
00444
00445 bool operator!=(const ConstIterator& it)
00446 { return (m_Id != it.m_Id); }
00447
00448 bool operator==(const ConstIterator& it)
00449 { return (m_Id == it.m_Id); }
00450
00451 ConstIterator& operator=(const ConstIterator& it)
00452 {
00453 m_Id = it.m_Id;
00454 m_Histogram = it.m_Histogram ;
00455 return *this ;
00456 }
00457
00458 ConstIterator(const ConstIterator & it)
00459 {
00460 m_Id = it.m_Id;
00461 m_Histogram = it.m_Histogram ;
00462 }
00463
00464 private:
00465
00466 InstanceIdentifier m_Id;
00467
00468
00469 const Self* m_Histogram ;
00470 } ;
00471
00472 Iterator Begin()
00473 {
00474 Iterator iter(0, this) ;
00475 return iter ;
00476 }
00477
00478 Iterator End()
00479 {
00480 return Iterator(m_OffsetTable[VMeasurementVectorSize], this) ;
00481 }
00482
00483 ConstIterator Begin() const
00484 {
00485 ConstIterator iter(0, this) ;
00486 return iter ;
00487 }
00488
00489 ConstIterator End() const
00490 {
00491 return ConstIterator(m_OffsetTable[VMeasurementVectorSize], this) ;
00492 }
00493
00494 virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s )
00495 {
00496 if( s!= VMeasurementVectorSize )
00497 {
00498 itkExceptionMacro( << "This Histogram class is meant to be used only for "
00499 << "fixed length vectors of length " << VMeasurementVectorSize <<
00500 ". Cannot set this to " << s);
00501 }
00502 }
00503 MeasurementVectorSizeType GetMeasurementVectorSize() const
00504 {
00505 return VMeasurementVectorSize;
00506 }
00507
00508
00509 protected:
00510 Histogram() ;
00511 virtual ~Histogram() {}
00512
00513
00514 SizeType m_Size ;
00515
00516 private:
00517 Histogram(const Self&);
00518 void operator=(const Self&);
00519
00520 InstanceIdentifier m_OffsetTable[VMeasurementVectorSize + 1] ;
00521 FrequencyContainerPointer m_FrequencyContainer ;
00522 unsigned int m_NumberOfInstances ;
00523
00524
00525 std::vector< std::vector<MeasurementType> > m_Min ;
00526
00527
00528 std::vector< std::vector<MeasurementType> > m_Max ;
00529
00530 mutable MeasurementVectorType m_TempMeasurementVector ;
00531 mutable IndexType m_TempIndex ;
00532
00533 bool m_ClipBinsAtEnds;
00534
00535 } ;
00536
00537 }
00538 }
00539
00540 #ifndef ITK_MANUAL_INSTANTIATION
00541 #include "itkHistogram.txx"
00542 #endif
00543
00544 #endif