Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 namespace
00034 cmtk
00035 {
00036 
00039 
00040 template<class T> size_t
00041 TemplateArray<T>::GetStatistics
00042 ( Types::DataItem& mean, Types::DataItem& variance ) const 
00043 {
00044   size_t Count = 0;
00045   Types::DataItem Sum = 0, SumOfSquares = 0;
00046   for ( size_t i = 0; i < DataSize; ++i ) 
00047     {
00048     if ( !this->PaddingFlag || (this->Data[i] != this->Padding) ) 
00049       {
00050       ++Count;
00051       Sum += this->Data[i];
00052       SumOfSquares += MathUtil::Square<Types::DataItem>( this->Data[i] );
00053       }
00054     }
00055   
00056   if ( Count ) 
00057     {
00058     mean = Sum / Count;
00059     variance = (SumOfSquares - 2*mean*Sum)/Count + MathUtil::Square(mean);
00060     } 
00061   else
00062     {
00063     variance = mean = 0;
00064     }
00065   
00066   return Count;
00067 }
00068 
00069 template<class T>
00070 Histogram<unsigned int>::SmartPtr
00071 TemplateArray<T>::GetHistogram( const unsigned int numberOfBins, const bool centeredBins ) const
00072 {
00073   Histogram<unsigned int>::SmartPtr histogram( new Histogram<unsigned int>( numberOfBins ) );
00074 
00075   if ( centeredBins )
00076     histogram->SetRangeCentered( Types::DataItemRange( this->GetRangeTemplate() ) );
00077   else
00078     histogram->SetRange( Types::DataItemRange( this->GetRangeTemplate() ) );
00079   
00080   for ( size_t idx = 0; idx < DataSize; ++idx )
00081     if ( !this->PaddingFlag || (this->Data[idx] != this->Padding) )
00082       histogram->Increment( histogram->ValueToBin( this->Data[idx] ) );
00083   
00084   return histogram;
00085 }
00086 
00087 }