/usr/include/InsightToolkit/Review/Statistics/itkMahalanobisDistanceMetric.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 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 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkMahalanobisDistanceMetric.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 __itkMahalanobisDistanceMetric_txx
#define __itkMahalanobisDistanceMetric_txx
#include "itkMahalanobisDistanceMetric.h"
namespace itk {
namespace Statistics {
template < class TVector >
MahalanobisDistanceMetric< TVector >
::MahalanobisDistanceMetric():
m_Epsilon( 1e-100 ),
m_DoubleMax( 1e+20 )
{
MeasurementVectorSizeType size;
size = this->GetMeasurementVectorSize();
this->m_Covariance.set_size( size,size );
this->m_InverseCovariance.set_size( size,size );
m_Covariance.set_identity();
m_InverseCovariance.set_identity();
}
template < class TVector >
void
MahalanobisDistanceMetric< TVector >
::SetMean(const MeanVectorType & mean)
{
Superclass::SetOrigin( mean );
}
template < class TVector >
const typename
MahalanobisDistanceMetric< TVector >::MeanVectorType &
MahalanobisDistanceMetric< TVector >
::GetMean() const
{
return Superclass::GetOrigin();
}
template< class TVector >
void
MahalanobisDistanceMetric< TVector >
::SetMeasurementVectorSize( MeasurementVectorSizeType size )
{
this->Superclass::SetMeasurementVectorSize( size );
this->m_Covariance.set_size( size,size );
this->m_InverseCovariance.set_size( size,size );
this->m_Covariance.set_identity();
this->m_InverseCovariance.set_identity();
this->Modified();
}
template < class TVector >
void
MahalanobisDistanceMetric< TVector >
::SetCovariance(const CovarianceMatrixType &cov)
{
if( this->GetMeasurementVectorSize() != 0 )
{
if( cov.rows() != this->GetMeasurementVectorSize() ||
cov.cols() != this->GetMeasurementVectorSize())
{
itkExceptionMacro( << "Size of the covariance matrix must be same as the length of"
<< " the measurement vector.");
}
}
m_Covariance = cov;
this->CalculateInverseCovariance();
}
template < class TVector >
void
MahalanobisDistanceMetric< TVector >
::SetInverseCovariance(const CovarianceMatrixType &invcov)
{
if( this->GetMeasurementVectorSize() != 0 )
{
if( invcov.rows() != this->GetMeasurementVectorSize() ||
invcov.cols() != this->GetMeasurementVectorSize() )
{
itkExceptionMacro( << "Size of the covariance matrix xcmust be same as the length of"
<< " each measurement vector.");
}
}
// use the inverse computation
m_Covariance = invcov;
this->CalculateInverseCovariance();
m_Covariance = m_InverseCovariance;
m_InverseCovariance = invcov;
}
template < class TVector >
void
MahalanobisDistanceMetric< TVector >
::CalculateInverseCovariance()
{
// pack the cov matrix from in_model to tmp_cov_mat
double cov_sum = 0;
for(unsigned int band_x = 0; band_x < m_Covariance.cols(); band_x++)
{
for(unsigned int band_y = 0; band_y < m_Covariance.rows(); band_y++)
{
cov_sum += vnl_math_abs( m_Covariance[band_x][band_y] );
}
}
// check if it is a zero covariance, if it is, we make its
// inverse as an identity matrix with diagonal elements as
// a very large number; otherwise, inverse it
if( cov_sum < m_Epsilon )
{
m_InverseCovariance.set_size( m_Covariance.rows(), m_Covariance.cols() );
m_InverseCovariance.set_identity();
m_InverseCovariance *= m_DoubleMax;
}
else
{
// check if num_bands == 1, if it is, we just use 1 to divide it
if( m_Covariance.rows() < 2 )
{
m_InverseCovariance.set_size(1,1);
m_InverseCovariance[0][0] = 1.0 / m_Covariance[0][0];
}
else
{
m_InverseCovariance = vnl_matrix_inverse<double>(m_Covariance);
}
}// end inverse calculations
}
template < class TVector >
double
MahalanobisDistanceMetric< TVector >
::Evaluate(const MeasurementVectorType &measurement) const
{
vnl_matrix < double > tempVec;
vnl_matrix < double > tempMat;
tempVec.set_size( 1, this->GetMeasurementVectorSize());
tempMat.set_size( 1, this->GetMeasurementVectorSize());
// Compute |y - mean |
for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
{
tempVec[0][i] = measurement[i] - this->GetOrigin()[i];
}
// Compute |y - mean | * inverse(cov)
tempMat= tempVec * m_InverseCovariance;
// Compute |y - mean | * inverse(cov) * |y - mean|^T
double temp;
temp = vcl_sqrt( dot_product( tempMat.as_ref(), tempVec.as_ref()) );
return temp;
}
template< class TVector >
inline double
MahalanobisDistanceMetric< TVector >
::Evaluate(const MeasurementVectorType &x1, const MeasurementVectorType &x2) const
{
if( MeasurementVectorTraits::GetLength( x1 ) != this->GetMeasurementVectorSize() ||
MeasurementVectorTraits::GetLength( x2 ) != this->GetMeasurementVectorSize())
{
itkExceptionMacro( << "Size of the measurement vectors is not the same as the length of"
<< " the measurement vector set in the distance metric.");
}
vnl_matrix < double > tempVec;
vnl_matrix < double > tempMat;
tempVec.set_size( 1, this->GetMeasurementVectorSize());
tempMat.set_size( 1, this->GetMeasurementVectorSize());
// Compute |x1 - x2 |
for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
{
tempVec[0][i] = x1[i] - x2[i];
}
// Compute |x1 - x2 | * inverse(cov)
tempMat= tempVec * m_InverseCovariance;
// Compute |x1 - x2 | * inverse(cov) * |x1 - x2|^T
double temp;
temp = vcl_sqrt( dot_product( tempMat.as_ref(), tempVec.as_ref()) );
return temp;
}
template < class TVector >
void
MahalanobisDistanceMetric< TVector >
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Covariance: " << std::endl;
os << this->GetCovariance() << std::endl;
os << indent << "Inverse covariance: " << std::endl;
os << this->GetInverseCovariance() << std::endl;
os << indent << "Mean: " << std::endl;
os << this->GetMean() << std::endl;
os << indent << "Epsilon: " << std::endl;
os << this->GetEpsilon() << std::endl;
os << indent << "Double max: " << std::endl;
os << this->GetDoubleMax() << std::endl;
}
} // end namespace Statistics
} // end of namespace itk
#endif
|