Orfeo Toolbox  3.16
itkMahalanobisDistanceMembershipFunction.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMahalanobisDistanceMembershipFunction.txx,v $
5  Language: C++
6  Date: $Date: 2009-03-04 15:23:57 $
7  Version: $Revision: 1.16 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkMahalanobisDistanceMembershipFunction_txx
18 #define __itkMahalanobisDistanceMembershipFunction_txx
19 
21 
22 namespace itk {
23 namespace Statistics {
24 
25 template < class TVector >
28  m_NumberOfSamples(0),
29  m_PreFactor(0),
30  m_Epsilon( 1e-100 ),
31  m_DoubleMax( 1e+20 )
32 {
33  m_Mean.fill( 0.0f );
34  m_Covariance.set_identity();
35  m_InverseCovariance.set_identity();
36  this->m_MeasurementVectorSize = 0;
37 }
38 
39 template< class TVector >
40 void
43 {
44  if( s == this->m_MeasurementVectorSize )
45  {
46  return;
47  }
48 
49  if( this->m_MeasurementVectorSize != 0 )
50  {
51  itkWarningMacro( << "Destructively resizing paramters of the DistanceToCentroidMembershipFunction." );
52  }
53  this->m_MeasurementVectorSize = s;
54  m_Mean.set_size( s );
55  this->Modified();
56 }
57 
58 template < class TVector >
59 void
62 {
63  if( this->m_MeasurementVectorSize != 0 )
64  {
65  if( mean.size() != this->m_MeasurementVectorSize )
66  {
67  itkExceptionMacro( << "Size of the centroid must be same as the length of"
68  << " each measurement vector.");
69  }
70  }
71  else
72  {
73  this->m_MeasurementVectorSize = mean.size();
74  }
75 
76  m_Mean = mean;
77 }
78 
79 
80 template < class TVector >
81 void
84 {
85  if( this->m_MeasurementVectorSize != 0 )
86  {
87  if( mean.Size() != this->m_MeasurementVectorSize )
88  {
89  itkExceptionMacro( << "Size of the centroid must be same as the length of"
90  << " each measurement vector.");
91  }
92  }
93  else
94  {
95  this->m_MeasurementVectorSize = mean.Size();
96  }
97 
98  m_Mean = dynamic_cast< MeanVectorType & >(const_cast< Array< double >& >(mean));
99 }
100 
101 
102 template < class TVector >
103 const typename
106 ::GetMean() const
107 {
108  return m_Mean;
109 }
110 
111 template < class TVector >
112 void
115 {
116  if( this->m_MeasurementVectorSize != 0 )
117  {
118  if( cov.rows() != this->m_MeasurementVectorSize ||
119  cov.cols() != this->m_MeasurementVectorSize )
120  {
121  itkExceptionMacro( << "Size of the centroid must be same as the length of"
122  << " each measurement vector.");
123  }
124  }
125  else
126  {
127  this->m_MeasurementVectorSize = cov.rows();
128  }
129 
130  m_Covariance = cov;
131  this->CalculateInverseCovariance();
132 }
133 
134 template < class TVector >
135 void
138 {
139  if( this->m_MeasurementVectorSize != 0 )
140  {
141  if( invcov.rows() != this->m_MeasurementVectorSize ||
142  invcov.cols() != this->m_MeasurementVectorSize )
143  {
144  itkExceptionMacro( << "Size of the centroid must be same as the length of"
145  << " each measurement vector.");
146  }
147  }
148  else
149  {
150  this->m_MeasurementVectorSize = invcov.rows();
151  }
152 
153  // use the inverse computation
154  m_Covariance = invcov;
155  this->CalculateInverseCovariance();
156  m_Covariance = m_InverseCovariance;
157  m_InverseCovariance = invcov;
158 }
159 
160 template < class TVector >
161 void
164 {
165 
166  // pack the cov matrix from in_model to tmp_cov_mat
167  double cov_sum = 0;
168  for(unsigned int band_x = 0; band_x < m_Covariance.cols(); band_x++)
169  {
170  for(unsigned int band_y = 0; band_y < m_Covariance.rows(); band_y++)
171  {
172  cov_sum += vnl_math_abs( m_Covariance[band_x][band_y] );
173  }
174  }
175  // check if it is a zero covariance, if it is, we make its
176  // inverse as an identity matrix with diagonal elements as
177  // a very large number; otherwise, inverse it
178  if( cov_sum < m_Epsilon )
179  {
180  m_InverseCovariance.set_size( m_Covariance.rows(), m_Covariance.cols() );
181  m_InverseCovariance.set_identity();
182  m_InverseCovariance *= m_DoubleMax;
183  }
184  else
185  {
186  // check if num_bands == 1, if it is, we just use 1 to divide it
187  if( m_Covariance.rows() < 2 )
188  {
189  m_InverseCovariance.set_size(1,1);
190  m_InverseCovariance[0][0] = 1.0 / m_Covariance[0][0];
191  }
192  else
193  {
194  m_InverseCovariance = vnl_matrix_inverse<double>(m_Covariance);
195  }
196  }// end inverse calculations
197 
198 }// CalculateInverseCovariance()
199 
200 template < class TVector >
201 double
203 ::Evaluate(const MeasurementVectorType &measurement) const
204 {
205 
206  double temp;
207  m_TempVec.set_size( 1, this->m_MeasurementVectorSize);
208  m_TempMat.set_size( 1, this->m_MeasurementVectorSize);
209 
210  // Compute |y - mean |
211  for ( unsigned int i = 0; i < this->m_MeasurementVectorSize; i++ )
212  {
213  m_TempVec[0][i] = measurement[i] - m_Mean[i];
214  }
215 
216  // Compute |y - mean | * inverse(cov)
217  m_TempMat= m_TempVec * m_InverseCovariance;
218 
219  // Compute |y - mean | * inverse(cov) * |y - mean|^T
220  temp = dot_product( m_TempMat.as_ref(), m_TempVec.as_ref() );
221 
222  return temp;
223 }
224 
225 template < class TVector >
226 void
228 ::PrintSelf(std::ostream& os, Indent indent) const
229 {
230  unsigned int i;
231  Superclass::PrintSelf(os,indent);
232 
233  if ( this->m_MeasurementVectorSize &&
234  m_Mean.size() == this->m_MeasurementVectorSize )
235  {
236  os << indent << "Mean: [";
237  for (i=0; (i + 1) < this->m_MeasurementVectorSize; i++)
238  {
239  os << m_Mean[i] << ", ";
240  }
241  os << m_Mean[i] << "]" << std::endl;
242  }
243  else
244  {
245  os << indent << "Mean: not set or size does not match" << std::endl;
246  }
247 
248  os << indent << "Number of Samples: " << m_NumberOfSamples << std::endl;
249  os << indent << "Covariance: " << std::endl;
250  os << m_Covariance << std::endl;
251  os << indent << "Inverse covariance: " << std::endl;
252  os << m_InverseCovariance << std::endl;
253 }
254 } // end namespace Statistics
255 } // end of namespace itk
256 
257 #endif

Generated at Sat Feb 2 2013 23:51:28 for Orfeo Toolbox with doxygen 1.8.1.1