cmtkTemplateArray_Statistics.txx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2553 $
00026 //
00027 //  $LastChangedDate: 2010-11-06 15:14:57 -0700 (Sat, 06 Nov 2010) $
00028 //
00029 //  $LastChangedBy: torsten_at_home $
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 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines