18 #ifndef __itkPSMShapeEntropyFunction_hxx 19 #define __itkPSMShapeEntropyFunction_hxx 20 #include "itkPSMShapeEntropyFunction.h" 22 #include "vnl/algo/vnl_symmetric_eigensystem.h" 42 template <
unsigned int VDimension>
44 PSMShapeEntropyFunction<VDimension>
45 ::ComputeCovarianceMatrix()
49 const unsigned int num_samples = m_ShapeMatrix->cols();
50 const unsigned int num_dims = m_ShapeMatrix->rows();
53 if (m_PointsUpdate.rows() != num_dims || m_PointsUpdate.cols() != num_samples)
55 m_PointsUpdate.set_size(num_dims, num_samples);
58 vnl_matrix_type points_minus_mean(num_dims, num_samples);
59 vnl_vector_type means(num_dims);
62 for (
unsigned int j = 0; j < num_dims; j++)
65 for (
unsigned int i = 0; i < num_samples; i++)
67 total += m_ShapeMatrix->operator()(j, i);
69 means(j) = total/(double)num_samples;
73 for (
unsigned int j = 0; j < num_dims; j++)
75 for (
unsigned int i = 0; i < num_samples; i++)
77 points_minus_mean(j, i) = m_ShapeMatrix->operator()(j, i) - means(j);
81 vnl_matrix_type A = points_minus_mean.transpose()
82 * points_minus_mean * (1.0/((double)(num_samples-1)));
85 for (
unsigned int i = 0; i < num_samples; i++)
87 A(i, i) = A(i, i) + m_MinimumVariance;
91 vnl_symmetric_eigensystem<double> symEigen(A);
92 m_PointsUpdate = points_minus_mean * ((symEigen.pinverse()).transpose());
93 m_MinimumEigenValue = symEigen.D(0, 0);
95 m_CurrentEnergy = 0.0;
96 for (
unsigned int i = 1; i < num_samples; i++)
98 if (symEigen.D(i, i) < m_MinimumEigenValue)
100 m_MinimumEigenValue = symEigen.D(i, i);
102 m_CurrentEnergy += log(symEigen.D(i,i));
105 m_CurrentEnergy /= num_samples;
111 if (m_ShapePCAVariances.size() < num_samples)
113 m_ShapePCAVariances.resize(num_samples);
115 for (
int i = 0; i < (int)num_samples; i++)
117 int tmp = num_samples - i - 1;
118 m_ShapePCAVariances[i] = symEigen.D(tmp, tmp) - m_MinimumVariance;
122 template <
unsigned int VDimension>
126 double &maxdt,
double &energy)
const 130 energy = m_CurrentEnergy;
131 maxdt = m_MinimumEigenValue;
132 const unsigned int DomainsPerShape = m_ShapeMatrix->GetDomainsPerShape();
136 unsigned int k = ((d % DomainsPerShape) * PointsPerDomain * VDimension)
137 + (idx * VDimension);
138 for (
unsigned int i = 0; i< VDimension; i++)
140 gradE[i] = m_PointsUpdate(k + i, d / DomainsPerShape);
const TransformType & GetInverseTransform(unsigned int i) const
Superclass::VectorType VectorType
unsigned long int GetNumberOfParticles(unsigned int d=0) const
const TransformType & GetInversePrefixTransform(unsigned int i) const
A facade class that manages interactions with a particle system.
VectorType TransformVector(const VectorType &, const TransformType &) const
virtual VectorType Evaluate(unsigned int, unsigned int, const ParticleSystemType *, double &, double &) const
vnl_vector_fixed< double, VDimension > VectorType