ITK  5.2.0
Insight Toolkit
itkMahalanobisDistanceMetric.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
46 template <typename TVector>
47 class ITK_TEMPLATE_EXPORT MahalanobisDistanceMetric : 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
78  SetMean(const MeanVectorType & mean);
79 
81  const MeanVectorType &
82  GetMean() const;
83 
88  void
89  SetCovariance(const CovarianceMatrixType & cov);
90 
92  itkGetConstReferenceMacro(Covariance, CovarianceMatrixType);
93 
96  void
97  SetInverseCovariance(const CovarianceMatrixType & invcov);
98 
100  itkGetConstReferenceMacro(InverseCovariance, CovarianceMatrixType);
101 
105  double
106  Evaluate(const MeasurementVectorType & measurement) const override;
107 
109  double
110  Evaluate(const MeasurementVectorType & x1, const MeasurementVectorType & x2) const override;
111 
113  itkSetMacro(Epsilon, double);
114  itkGetConstMacro(Epsilon, double);
116 
117  itkSetMacro(DoubleMax, double);
118  itkGetConstMacro(DoubleMax, double);
119 
120 protected:
122  ~MahalanobisDistanceMetric() override = default;
123  void
124  PrintSelf(std::ostream & os, Indent indent) const override;
125 
126 private:
128  CovarianceMatrixType m_Covariance; // covariance matrix
129 
130  // inverse covariance matrix which is automatically calculated
131  // when covariance matirx is set. This speed up the GetProbability()
133 
134  double m_Epsilon{ 1e-100 };
135  double m_DoubleMax{ 1e+20 };
136 
137  void
138  CalculateInverseCovariance();
139 };
140 } // end of namespace Statistics
141 } // end namespace itk
142 
143 #ifndef ITK_MANUAL_INSTANTIATION
144 # include "itkMahalanobisDistanceMetric.hxx"
145 #endif
146 
147 #endif
itk::Statistics::MahalanobisDistanceMetric::m_Covariance
CovarianceMatrixType m_Covariance
Definition: itkMahalanobisDistanceMetric.h:128
itkDistanceMetric.h
itk::Statistics::DistanceMetric
this class declares common interfaces for distance functions.
Definition: itkDistanceMetric.h:48
itk::Statistics::DistanceMetric::MeasurementVectorType
TVector MeasurementVectorType
Definition: itkDistanceMetric.h:58
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::MahalanobisDistanceMetric::m_InverseCovariance
CovarianceMatrixType m_InverseCovariance
Definition: itkMahalanobisDistanceMetric.h:132
itk::Statistics::DistanceMetric::MeasurementVectorSizeType
unsigned int MeasurementVectorSizeType
Definition: itkDistanceMetric.h:64
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::Statistics::MahalanobisDistanceMetric::m_Mean
MeanVectorType m_Mean
Definition: itkMahalanobisDistanceMetric.h:127
itk::Statistics::MahalanobisDistanceMetric::CovarianceMatrixType
vnl_matrix< double > CovarianceMatrixType
Definition: itkMahalanobisDistanceMetric.h:71
itkArray.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Statistics::MahalanobisDistanceMetric::MeanVectorType
typename Superclass::OriginType MeanVectorType
Definition: itkMahalanobisDistanceMetric.h:68
itk::Statistics::MahalanobisDistanceMetric
MahalanobisDistanceMetric class computes a Mahalanobis distance given a mean and covariance.
Definition: itkMahalanobisDistanceMetric.h:47
itk::Math::e
static constexpr double e
Definition: itkMath.h:54