/usr/include/InsightToolkit/Review/Statistics/itkGaussianMembershipFunction.txx is in libinsighttoolkit3-dev 3.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkGaussianMembershipFunction.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 __itkGaussianMembershipFunction_txx
#define __itkGaussianMembershipFunction_txx
#include "itkGaussianMembershipFunction.h"
namespace itk {
namespace Statistics {
template < class TMeasurementVector >
GaussianMembershipFunction< TMeasurementVector >
::GaussianMembershipFunction()
{
m_PreFactor = 0.0;
m_Covariance.SetIdentity();
}
template < class TMeasurementVector >
void
GaussianMembershipFunction< TMeasurementVector >
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Mean: " << m_Mean << std::endl;
os << indent << "Covariance: " << std::endl;
os << m_Covariance.GetVnlMatrix();
os << indent << "InverseCovariance: " << std::endl;
os << indent << m_InverseCovariance.GetVnlMatrix();
os << indent << "Prefactor: " << m_PreFactor << std::endl;
}
template < class TMeasurementVector >
void
GaussianMembershipFunction< TMeasurementVector >
::SetMean( const MeanType & mean )
{
if( this->GetMeasurementVectorSize() )
{
MeasurementVectorTraits::Assert(mean, this->GetMeasurementVectorSize(),
"GaussianMembershipFunction::SetMean Size of measurement vectors in \
the sample must the same as the size of the mean." );
}
else
{
this->SetMeasurementVectorSize( mean.Size() );
}
if ( m_Mean != mean)
{
m_Mean = mean;
this->Modified();
}
}
template < class TMeasurementVector >
void
GaussianMembershipFunction< 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 >
inline double
GaussianMembershipFunction< 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();
}
}
template < class TVector >
typename GaussianMembershipFunction<TVector>::Pointer
GaussianMembershipFunction< TVector >
::Clone()
{
Pointer membershipFunction = GaussianMembershipFunction< TVector >::New();
membershipFunction->SetMeasurementVectorSize( this->GetMeasurementVectorSize() );
membershipFunction->SetMean( this->GetMean() );
membershipFunction->SetCovariance( this->GetCovariance() );
return membershipFunction;
}
} // end namespace Statistics
} // end of namespace itk
#endif
|