ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkMahalanobisDistanceMetric.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkMahalanobisDistanceMetric_h
19 #define itkMahalanobisDistanceMetric_h
20 
21 #include "vnl/vnl_vector.h"
22 #include "vnl/vnl_vector_ref.h"
23 #include "vnl/vnl_transpose.h"
24 #include "vnl/vnl_matrix.h"
25 #include "vnl/algo/vnl_matrix_inverse.h"
26 #include "vnl/algo/vnl_determinant.h"
27 #include "itkArray.h"
28 
29 #include "itkDistanceMetric.h"
30 
31 namespace itk
32 {
33 namespace Statistics
34 {
45 template< typename TVector >
46 class ITK_TEMPLATE_EXPORT MahalanobisDistanceMetric:
47  public DistanceMetric< TVector >
48 {
49 public:
55 
58  itkNewMacro(Self);
60 
62  using MeasurementVectorType = typename Superclass::MeasurementVectorType;
63 
65  using MeasurementVectorSizeType = typename Superclass::MeasurementVectorSizeType;
66 
68  using MeanVectorType = typename Superclass::OriginType;
69 
71  using CovarianceMatrixType = vnl_matrix< double >;
72 
74  void SetMeasurementVectorSize(MeasurementVectorSizeType) override;
75 
77  void SetMean(const MeanVectorType & mean);
78 
80  const MeanVectorType & GetMean() const;
81 
86  void SetCovariance(const CovarianceMatrixType & cov);
87 
89  itkGetConstReferenceMacro(Covariance, CovarianceMatrixType);
90 
93  void SetInverseCovariance(const CovarianceMatrixType & invcov);
94 
96  itkGetConstReferenceMacro(InverseCovariance, CovarianceMatrixType);
97 
101  double Evaluate(const MeasurementVectorType & measurement) const override;
102 
104  double Evaluate(const MeasurementVectorType & x1, const MeasurementVectorType & x2) const override;
105 
107  itkSetMacro(Epsilon, double);
108  itkGetConstMacro(Epsilon, double);
110 
111  itkSetMacro(DoubleMax, double);
112  itkGetConstMacro(DoubleMax, double);
113 
114 protected:
116  ~MahalanobisDistanceMetric() override = default;
117  void PrintSelf(std::ostream & os, Indent indent) const override;
118 
119 private:
121  CovarianceMatrixType m_Covariance; // covariance matrix
122 
123  // inverse covariance matrix which is automatically calculated
124  // when covariace matirx is set. This speed up the GetProbability()
126 
127  double m_Epsilon{1e-100};
128  double m_DoubleMax{1e+20};
129 
130  void CalculateInverseCovariance();
131 };
132 } // end of namespace Statistics
133 } // end namespace itk
134 
135 #ifndef ITK_MANUAL_INSTANTIATION
136 #include "itkMahalanobisDistanceMetric.hxx"
137 #endif
138 
139 #endif
Light weight base class for most itk classes.
this class declares common interfaces for distance functions.
static constexpr double e
The base of the natural logarithm or Euler&#39;s number
Definition: itkMath.h:53
Control indentation during Print() invocation.
Definition: itkIndent.h:49
MahalanobisDistanceMetric class computes a Mahalanobis distance given a mean and covariance.