1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkHistogram.h,v $
Language: C++
Date: $Date: 2009-03-04 15:23:50 $
Version: $Revision: 1.50 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkHistogram_h
#define __itkHistogram_h
#include <vector>
#include "itkIndex.h"
#include "itkSize.h"
#include "itkFixedArray.h"
#include "itkSample.h"
#include "itkDenseFrequencyContainer.h"
#include "itkSparseFrequencyContainer.h"
namespace itk {
namespace Statistics {
/**
* Due to a bug in MSVC, an enum value cannot be accessed out of a template
* parameter until the template class opens. In order for templated classes
* to access the dimension of an image template parameter in defining their
* own dimension, this class is needed as a work-around.
*/
template <typename THistogram>
struct GetHistogramDimension
{
itkStaticConstMacro(HistogramDimension, unsigned int, THistogram::MeasurementVectorSize);
};
/** \class Histogram
* \brief This class stores measurement vectors in the context of n-dimensional histogram.
*
* Histogram represents an ND histogram. Histogram bins can be
* regularly or irregularly spaced. The storage for the histogram is
* managed via the FrequencyContainer specified by the template
* argument. The default frequency container is a
* DenseFrequencyContainer. A SparseFrequencyContainer can be used as
* an alternative.
*
* Frequencies of a bin (SetFrequency(), IncreaseFrequency()) can be
* specified by measurement, index, or instance identifier.
*
* Measurements can be queried by bin index or instance
* identifier. In this case, the measurement returned in the centroid
* of the histogram bin.
*
* The Initialize() method is used to specified the number of bins for
* each dimension of the histogram. An overloaded version also allows
* for regularly spaced bins to defined. To define irregularly sized
* bins, use the SetBinMin()/SetBinMax() methods.
*
* If you do not know the length of the measurement vector at compile time, you
* can set the second template parameter to 0. This can conveniently be obtained
* from MeasurementVectorTraits. For instance, instantiate a histogram as
* below:
*
* \code
* typedef Histogram< THistogramMeasurement, typename
* MeasurementVectorTraits< MeasurementVectorType >::MeasurementVectorLength,
* TFrequencyContainer > HistogramType;
* \endcode
*
* \sa Sample, DenseFrequencyContainer, SparseFrequencyContainer
*/
template < class TMeasurement = float, unsigned int VMeasurementVectorSize = 1,
class TFrequencyContainer = DenseFrequencyContainer >
class ITK_EXPORT Histogram
: public Sample < FixedArray< TMeasurement, VMeasurementVectorSize > >
{
public:
/** Standard typedefs */
typedef Histogram Self;
typedef Sample< FixedArray< TMeasurement, VMeasurementVectorSize > >
Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Run-time type information (and related methods). */
itkTypeMacro(Histogram, Sample);
/** standard New() method support */
itkNewMacro(Self);
/** Dimension of a measurement vector */
itkStaticConstMacro(MeasurementVectorSize, unsigned int,
VMeasurementVectorSize);
/** type of an element of a measurement vector */
typedef TMeasurement MeasurementType;
/** Common sample class typedefs */
typedef typename Superclass::MeasurementVectorType MeasurementVectorType;
typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
typedef MeasurementVectorType ValueType;
typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
/** frequency container typedef */
typedef TFrequencyContainer FrequencyContainerType;
typedef typename FrequencyContainerType::Pointer FrequencyContainerPointer;
/** Frequency and TotalFrequency value type from superclass */
typedef typename FrequencyContainerType::FrequencyType FrequencyType;
typedef typename FrequencyContainerType::TotalFrequencyType TotalFrequencyType;
/** Index typedef support. An index is used to access pixel values. */
typedef itk::Index< VMeasurementVectorSize > IndexType;
typedef typename IndexType::IndexValueType IndexValueType;
/** size array type */
typedef itk::Size< VMeasurementVectorSize > SizeType;
typedef typename SizeType::SizeValueType SizeValueType;
/** bin min max value storage types */
typedef std::vector< MeasurementType > BinMinVectorType;
typedef std::vector< MeasurementType > BinMaxVectorType;
typedef std::vector< BinMinVectorType > BinMinContainerType;
typedef std::vector< BinMaxVectorType > BinMaxContainerType;
/** Initialize the histogram, generating the offset table and
* preparing the frequency container. Subclasses should call this
* method in their Initialize() method. */
void Initialize(const SizeType &size);
/** Initialize the histogram using equal size bins. To assign bin's
* min and max values along each dimension use SetBinMin() and
* SetBinMax() functions. */
void Initialize(const SizeType &size, MeasurementVectorType& lowerBound,
MeasurementVectorType& upperBound);
/** Initialize the values of the histogram bins to zero */
void SetToZero();
/** Get the index of a measurement value from the histogram.
* \deprecated Use GetIndex(const MeasurementVectorType &
* measurement, IndexType & index ) const instead. */
const IndexType & GetIndex(const MeasurementVectorType& measurement) const;
/** Get the index of histogram corresponding to the specified
* measurement value. Returns true if index is valid and false if
* the measurement is outside the histogram */
bool GetIndex(const MeasurementVectorType & measurement,
IndexType & index ) const;
/** Get the index that is uniquely labelled by an instance identifier
* The corresponding id is the offset of the index
* This method uses ImageBase::ComputeIndex() method */
const IndexType & GetIndex(const InstanceIdentifier &id) const;
/** Is set to false if the bins at edges of the histogram extend to
* +/- infinity. */
itkGetMacro(ClipBinsAtEnds, bool);
/** Set to false to have the bins at edges of the histogram extend to
* +/- infinity. */
itkSetMacro(ClipBinsAtEnds, bool);
/** Returns true if the given index is out of bound meaning one of index
* is not between [0, last index] */
bool IsIndexOutOfBounds(const IndexType &index) const;
/** Get the instance identifier of the bin that is indexed by the
* index. The corresponding instance identifier is the offset of the index
* This method uses ImageBase::ComputeIndex() method */
InstanceIdentifier GetInstanceIdentifier(const IndexType &index) const;
/** Returns the number of instances (bins or cells) in this container */
unsigned int Size() const;
/** Get the size (N-dimensional) of the histogram */
SizeType GetSize() const
{ return m_Size; }
/** Get the size of histogram along a specified dimension */
SizeValueType GetSize(const unsigned int dimension) const
{
return m_Size[dimension];
}
/** Get the minimum value of nth bin of dimension d */
const MeasurementType& GetBinMin(const unsigned int dimension,
const unsigned long nbin) const
{ return m_Min[dimension][nbin]; }
/** Get the maximum value of nth bin of dimension d */
const MeasurementType& GetBinMax(const unsigned int dimension,
const unsigned long nbin) const
{ return m_Max[dimension][nbin]; }
/** Set the minimum value of nth bin of dimension d */
void SetBinMin(const unsigned int dimension, const unsigned long nbin,
const MeasurementType min)
{ m_Min[dimension][nbin] = min; }
/** Set the maximum value of nth bin of dimension d */
void SetBinMax(const unsigned int dimension,
unsigned long nbin, const MeasurementType max)
{ m_Max[dimension][nbin] = max; }
/** Get the minimum of the bin along dimension d corresponding to a
* particular measurement. */
const MeasurementType& GetBinMinFromValue(const unsigned int dimension,
const float value ) const;
/** Get the maximum of the bin along dimension d corresponding to a
* particular measurement. */
const MeasurementType& GetBinMaxFromValue(const unsigned int dimension,
const float value ) const;
/** Get the vector of bin minimums along a dimension */
const BinMinVectorType& GetDimensionMins(const unsigned int dimension) const
{ return m_Min[dimension]; }
/** Get the vector of maximums along a dimension */
const BinMaxVectorType& GetDimensionMaxs(const unsigned int dimension) const
{ return m_Max[dimension]; }
/** Get the minimums of the bins */
const BinMinContainerType& GetMins() const
{ return m_Min; }
/** Method the maximums of the bins */
const BinMaxContainerType& GetMaxs() const
{ return m_Max; }
/** Get the minimums of the bin corresponding to a particular index */
MeasurementVectorType& GetHistogramMinFromIndex(const IndexType &index);
/** Get the maximums of the bin corresponding to a particular index */
MeasurementVectorType& GetHistogramMaxFromIndex(const IndexType &index);
/** Get the frequency of an instance indentifier */
FrequencyType GetFrequency(const InstanceIdentifier &id) const
{ return m_FrequencyContainer->GetFrequency(id); }
/** Get the frequency of an index */
FrequencyType GetFrequency(const IndexType &index) const;
/** Set all the bins in the histogram to a specified frequency */
void SetFrequency(const FrequencyType value);
/** Set the frequency of an instance identifier. Returns false if the bin is
* out of bounds. */
bool SetFrequency(const InstanceIdentifier &id, const FrequencyType value)
{ return m_FrequencyContainer->SetFrequency(id, value); }
/** Set the frequency of an index. Returns false if the bin is
* out of bounds. */
bool SetFrequency(const IndexType &index,
const FrequencyType value);
/** Set the frequency of a measurement. Returns false if the bin is
* out of bounds. */
bool SetFrequency(const MeasurementVectorType &measurement,
const FrequencyType value);
/** Increase the frequency of an instance identifier.
* Frequency is increased by the specified value. Returns false if
* the bin is out of bounds. */
bool IncreaseFrequency(const InstanceIdentifier &id,
const FrequencyType value)
{ return m_FrequencyContainer->IncreaseFrequency(id, value); }
/** Increase the frequency of an index. Frequency is
* increased by the specified value. Returns false if the bin is out
* of bounds. */
bool IncreaseFrequency(const IndexType &index,
const FrequencyType value);
/** Increase the frequency of a measurement. Frequency is
* increased by the specified value. Returns false if the
* measurement is outside the bounds of the histogram. */
bool IncreaseFrequency(const MeasurementVectorType &measurement,
const FrequencyType value);
/** Get the measurement of an instance identifier. This is the
* centroid of the bin.
*/
const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
/** Get the measurement of an index. This is the centroid of the bin. */
const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const;
/** Get the measurement a bin along a specified dimension. This is
* the midpoint of the bin along that dimension. */
MeasurementType GetMeasurement(const unsigned long n,
const unsigned int dimension) const;
/** Get the total frequency in the histogram */
TotalFrequencyType GetTotalFrequency() const;
/** Get the frequency of a dimension's nth element. */
FrequencyType GetFrequency(const unsigned long n,
const unsigned int dimension) const;
/** Get the pth percentile value for a dimension.
*
* Let assume n = the index of the bin where the p-th percentile value is,
* min = min value of the dimension of the bin,
* max = max value of the dimension of the bin,
* interval = max - min ,
* pp = cumlated proportion until n-1 bin;
* and pb = frequency of the bin / total frequency of the dimension.
*
* If p is less than 0.5,
* the percentile value =
* min + ((p - pp ) / pb) * interval
* If p is greater than or equal to 0.5
* the percentile value =
* max - ((pp - p) / pb) * interval */
double Quantile(const unsigned int dimension, const double &p) const;
protected:
void PrintSelf(std::ostream& os, Indent indent) const;
public:
/** iterator support */
class Iterator
{
public:
Iterator(){};
Iterator(Self * histogram)
{
m_Id = 0;
m_Histogram = histogram;
}
Iterator(InstanceIdentifier id, Self * histogram)
: m_Id(id), m_Histogram(histogram) {}
FrequencyType GetFrequency() const
{
return m_Histogram->GetFrequency(m_Id);
}
bool SetFrequency(const FrequencyType value)
{
return m_Histogram->SetFrequency(m_Id, value);
}
InstanceIdentifier GetInstanceIdentifier() const
{ return m_Id; }
const MeasurementVectorType & GetMeasurementVector() const
{
return m_Histogram->GetMeasurementVector(m_Id);
}
Iterator& operator++()
{
++m_Id;
return *this;
}
bool operator!=(const Iterator& it)
{ return (m_Id != it.m_Id); }
bool operator==(const Iterator& it)
{ return (m_Id == it.m_Id); }
Iterator& operator=(const Iterator& it)
{
m_Id = it.m_Id;
m_Histogram = it.m_Histogram;
return *this;
}
Iterator(const Iterator& it)
{
m_Id = it.m_Id;
m_Histogram = it.m_Histogram;
}
private:
// Iterator pointing DenseFrequencyContainer
InstanceIdentifier m_Id;
// Pointer of DenseFrequencyContainer
Self* m_Histogram;
}; // end of iterator class
// Const Iterator
class ConstIterator
{
public:
ConstIterator(){};
ConstIterator(const Self * histogram)
{
m_Id = 0;
m_Histogram = histogram;
}
ConstIterator(InstanceIdentifier id, const Self * histogram)
: m_Id(id), m_Histogram(histogram) {}
FrequencyType GetFrequency() const
{
return m_Histogram->GetFrequency(m_Id);
}
InstanceIdentifier GetInstanceIdentifier() const
{ return m_Id; }
const MeasurementVectorType & GetMeasurementVector() const
{
return m_Histogram->GetMeasurementVector(m_Id);
}
ConstIterator& operator++()
{
++m_Id;
return *this;
}
bool operator!=(const ConstIterator& it)
{ return (m_Id != it.m_Id); }
bool operator==(const ConstIterator& it)
{ return (m_Id == it.m_Id); }
ConstIterator& operator=(const ConstIterator& it)
{
m_Id = it.m_Id;
m_Histogram = it.m_Histogram;
return *this;
}
ConstIterator(const ConstIterator & it)
{
m_Id = it.m_Id;
m_Histogram = it.m_Histogram;
}
private:
// ConstIterator pointing DenseFrequencyContainer
InstanceIdentifier m_Id;
// Pointer of DenseFrequencyContainer
const Self* m_Histogram;
}; // end of iterator class
Iterator Begin()
{
Iterator iter(0, this);
return iter;
}
Iterator End()
{
return Iterator(m_OffsetTable[VMeasurementVectorSize], this);
}
ConstIterator Begin() const
{
ConstIterator iter(0, this);
return iter;
}
ConstIterator End() const
{
return ConstIterator(m_OffsetTable[VMeasurementVectorSize], this);
}
virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s )
{
if( s!= VMeasurementVectorSize )
{
itkExceptionMacro( << "This Histogram class is meant to be used only for "
<< "fixed length vectors of length " << VMeasurementVectorSize <<
". Cannot set this to " << s);
}
}
MeasurementVectorSizeType GetMeasurementVectorSize() const
{
return VMeasurementVectorSize;
}
protected:
Histogram();
virtual ~Histogram() {}
// The number of bins for each dimension
SizeType m_Size;
private:
Histogram(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
InstanceIdentifier m_OffsetTable[VMeasurementVectorSize + 1];
FrequencyContainerPointer m_FrequencyContainer;
unsigned int m_NumberOfInstances;
// lower bound of each bin
std::vector< std::vector<MeasurementType> > m_Min;
// upper bound of each bin
std::vector< std::vector<MeasurementType> > m_Max;
mutable MeasurementVectorType m_TempMeasurementVector;
mutable IndexType m_TempIndex;
bool m_ClipBinsAtEnds;
};
} // end of namespace Statistics
} // end of namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkHistogram.txx"
#endif
#endif
|