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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkGaussianDensityFunction.txx
Language: C++
Date: $Date$
Version: $Revision$
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 __itkGaussianDensityFunction_txx
#define __itkGaussianDensityFunction_txx
#include "itkGaussianDensityFunction.h"
namespace itk {
namespace Statistics {
template < class TMeasurementVector >
GaussianDensityFunction< TMeasurementVector >
::GaussianDensityFunction()
{
m_Mean = 0;
m_Covariance = 0;
m_PreFactor = 0.0;
}
template < class TMeasurementVector >
void
GaussianDensityFunction< TMeasurementVector >
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Mean: ";
if ( m_Mean != 0 )
{
os << (*m_Mean) << std::endl;
}
else
{
os << " not set." << std::endl;
}
os << indent << "Covariance: " << std::endl;
if ( m_Covariance != 0 )
{
os << m_Covariance->GetVnlMatrix();
os << indent << "InverseCovariance: " << std::endl;
os << indent << m_InverseCovariance.GetVnlMatrix();
os << indent << "Prefactor: " << m_PreFactor << std::endl;
}
os << indent << " not set." << std::endl;
}
template < class TMeasurementVector >
void
GaussianDensityFunction< TMeasurementVector >
::SetCovariance(const CovarianceType* cov)
{
// Sanity check
if( cov->GetVnlMatrix().rows() != cov->GetVnlMatrix().cols() )
{
itkExceptionMacro( << "Covariance matrix must be square" );
}
if( this->GetMeasurementVectorSize() )
{
if( cov->GetVnlMatrix().rows() != this->GetMeasurementVectorSize() )
{
itkExceptionMacro( << "Length of measurement vectors in the sample must be"
<< " the same as the size of the covariance." );
}
}
else
{
this->SetMeasurementVectorSize( cov->GetVnlMatrix().rows() );
}
m_Covariance = cov;
m_IsCovarianceZero = m_Covariance->GetVnlMatrix().is_zero();
if ( !m_IsCovarianceZero )
{
// allocate the memory for m_InverseCovariance matrix
m_InverseCovariance.GetVnlMatrix() =
vnl_matrix_inverse< double >(m_Covariance->GetVnlMatrix());
// the determinant of the covaraince matrix
double det = vnl_determinant(m_Covariance->GetVnlMatrix());
// calculate coefficient C of multivariate gaussian
m_PreFactor = 1.0 / (vcl_sqrt(det) *
vcl_pow(vcl_sqrt(2.0 * vnl_math::pi), double(this->GetMeasurementVectorSize())));
}
}
template < class TMeasurementVector >
const typename GaussianDensityFunction< TMeasurementVector >::CovarianceType*
GaussianDensityFunction< TMeasurementVector >
::GetCovariance() const
{
return m_Covariance;
}
template < class TMeasurementVector >
inline double
GaussianDensityFunction< TMeasurementVector >
::Evaluate(const MeasurementVectorType &measurement) const
{
double temp;
const MeasurementVectorSizeType measurementVectorSize =
this->GetMeasurementVectorSize();
MeanType tempVector;
MeasurementVectorTraits::SetLength( tempVector, measurementVectorSize );
MeanType tempVector2;
MeasurementVectorTraits::SetLength( tempVector2, measurementVectorSize );
if ( !m_IsCovarianceZero )
{
// Compute |y - mean |
for ( unsigned int i = 0; i < measurementVectorSize; i++)
{
tempVector[i] = measurement[i] - (*m_Mean)[i];
}
// Compute |y - mean | * inverse(cov)
for (unsigned int i = 0; i < measurementVectorSize; i++)
{
temp = 0;
for (unsigned int j = 0; j < measurementVectorSize; j++)
{
temp += tempVector[j] * m_InverseCovariance.GetVnlMatrix().get(j, i);
}
tempVector2[i] = temp;
}
// Compute |y - mean | * inverse(cov) * |y - mean|^T
temp = 0;
for (unsigned int i = 0; i < measurementVectorSize; i++)
{
temp += tempVector2[i] * tempVector[i];
}
return m_PreFactor * vcl_exp(-0.5 * temp );
}
else
{
for ( unsigned int i = 0; i < measurementVectorSize; i++)
{
if ( (*m_Mean)[i] != (double) measurement[i] )
{
return 0;
}
}
return NumericTraits< double >::max();
}
}
} // end namespace Statistics
} // end of namespace itk
#endif
|