gml_Gaussian2.h

00001 // gml_Gaussian2.h --
00002 //
00003 //  Declares and defines the "gml_TGaussian2" class. Instances of this class store
00004 //  the gaussian information (mean, covariance) about a set of 2 dimentional samples.
00005 //
00006 // Copyright (c) 1997-2004 The CLIPS-IMAG Laboratory.
00007 // 
00008 //   See the file "gml_LicenseTerms.txt" for information on usage and redistribution
00009 //   of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010 // 
00011 //   Created in March, 1997 (FB).
00012 //   Ported to GML in January, 2004 (JL).
00013 
00014 #ifndef __GML_GAUSSIAN2__
00015 #define __GML_GAUSSIAN2__
00016 
00017 // project includes
00018 #include "gml/base/gml_Errors.h"
00019 #include "gml/base/gml_Types.h"
00020 #include "gml/math/gml_ElMat2.h"
00021 
00022 template <class T> class gml_TGaussian2;
00023 
00024 
00025 /// 2D Gaussians with single-precision floting-point coordinates.
00026 
00027 typedef gml_TGaussian2 <Float32> gml_TGaussian2_Float32;
00028 
00029 /// 2D Gaussians with double-precision floting-point coordinates.
00030 
00031 typedef gml_TGaussian2 <Float64> gml_TGaussian2_Float64;
00032 
00033 /// 2D Gaussians with 32-bit integer coordinates.
00034 
00035 typedef gml_TGaussian2 <UInt32>  gml_TGaussian2_UInt32;
00036 
00037 /// 2D Gaussians with 64-bit integer coordinates.
00038 
00039 typedef gml_TGaussian2 <UInt64>  gml_TGaussian2_UInt64;
00040 
00041 
00042 /// gml_TGaussian2 --
00043 ///
00044 ///   Compute and maintain a gaussian representation (approximation) of a set
00045 ///   of 2-dimensionnal samples
00046 
00047 template <class T>
00048 class gml_TGaussian2 {
00049 public:
00050 
00051   /// Constructor: zero the fields
00052   gml_TGaussian2();
00053 
00054   /// Setup the structure
00055   gml_TError Init ();
00056 
00057   /// Zero the structure
00058   void Reset ();
00059   
00060   /// Do nothing
00061   void Dispose () {}
00062 
00063   /// Set <this> to be the exact copy of <gauss>.
00064   void Clone (gml_TGaussian2<T> *gauss);
00065 
00066   /// Adds the distribution of <gauss> in <this>.
00067   void Add (gml_TGaussian2<T> *gauss);
00068 
00069   /// Adds a sample in the set.
00070   /// <s> must point to an array of two T values.
00071   void AddSample (T * s);
00072 
00073   /// Adds a sample in the set.
00074   void AddSample (T x, T y);
00075 
00076   /// Adds a weighted sample in the set.
00077   void AddSample (T x, T y, T weight);
00078 
00079   /// Return the total number of samples added.
00080   T Samples () { return fCount; }
00081 
00082   /// Compute mean, covariance, and inverse covariance.
00083   /// Returns an error if the covariance matrix could not
00084   /// be inverted.
00085   gml_TError UpdateStats ();
00086 
00087   /// Returns the squared mahalanobis distance from (x, y) to the
00088   /// gaussian.
00089   /// @warning Call UpdateStats() before calling GetMahaSquared().
00090   T GetMahaSquared (T x, T y);
00091 
00092   /// Return the gaussian's value at (x, y).
00093   /// @warning Call UpdateStats() before calling Value().
00094   T Value (T x, T y);
00095 
00096   /// Calculates the mean of the gaussian and puts the result
00097   ///  in <m>. <m> must point to an array of two T values.
00098   void GetMean (T * m);
00099 
00100   /// Calculates the covariance matrix of the gaussian and puts the result
00101   ///  in <m>. <m> must point to an array of four T values.
00102   void GetVar (T * m);
00103 
00104   T   fCount;                   ///< number of samples added to set
00105   T   fSum[2];                  ///< sum of samples by components
00106   T   fSumSq[2];                ///< sum of samples squared by components
00107   T   fMixSum;                  ///< sum of component products
00108   
00109   // the following are computed by UpdateStats()
00110   
00111   T   fMean[2];
00112   T   fCovariance[4];
00113   T   fInvCovariance[4];
00114 };
00115 
00116 ////////////////////////////////////////////////////////////////////////////////
00117 //                                                                            //
00118 //                         inline function definition                         //
00119 //                                                                            //
00120 ////////////////////////////////////////////////////////////////////////////////
00121 
00122 //
00123 //    Constructor
00124 //
00125 
00126 template <class T>
00127 inline gml_TGaussian2<T>::gml_TGaussian2 ()
00128 {
00129   Reset();
00130 }
00131 
00132 
00133 //
00134 //    Init
00135 //
00136 
00137 template <class T>
00138 inline gml_TError gml_TGaussian2<T>::Init ()
00139 {
00140   Reset();
00141 
00142   return gml_cNoError;
00143 }
00144 
00145 
00146 //
00147 //    Reset
00148 //
00149 
00150 template <class T>
00151 inline void gml_TGaussian2<T>::Reset ()
00152 {
00153   fCount    = T(0);
00154   fSum[0]   = T(0);
00155   fSum[1]   = T(0);
00156   fSumSq[0] = T(0);
00157   fSumSq[1] = T(0);
00158   fMixSum   = T(0);
00159 }
00160 
00161 
00162 
00163 //
00164 //    Clone
00165 //
00166 template <class T> 
00167 inline void gml_TGaussian2<T>::Clone (gml_TGaussian2<T> *gauss)
00168 {
00169   *this = *gauss;
00170 }
00171 
00172 
00173 
00174 //
00175 //    Add
00176 //
00177 template <class T> 
00178 inline void gml_TGaussian2<T>::Add (gml_TGaussian2<T> *gauss)
00179 {
00180   fCount    += gauss->fCount;
00181   fSum[0]   += gauss->fSum[0];
00182   fSum[1]   += gauss->fSum[1];
00183   fSumSq[0] += gauss->fSumSq[0];
00184   fSumSq[1] += gauss->fSumSq[1];
00185   fMixSum   += gauss->fMixSum;
00186 }
00187 
00188 //
00189 //    AddSample
00190 //
00191 
00192 template <class T>
00193 inline void gml_TGaussian2<T>::AddSample (T * s)
00194 {
00195   fCount    += T(1);
00196   fSum[0]   += s[0];
00197   fSumSq[0] += s[0] * s[0];
00198   fSum[1]   += s[1];
00199   fSumSq[1] += s[1] * s[1];
00200   fMixSum   += s[1] * s[0];
00201 }
00202 
00203 //
00204 //    AddSample
00205 //
00206 
00207 template <class T>
00208 inline void gml_TGaussian2<T>::AddSample (T x, T y)
00209 {
00210   fCount    += T(1);
00211   fSum[0]   += x;
00212   fSumSq[0] += x * x;
00213   fSum[1]   += y;
00214   fSumSq[1] += y * y;
00215   fMixSum   += x * y;
00216 }
00217 
00218 //
00219 //    AddSample, weighted
00220 //
00221 
00222 template <class T>
00223 inline void gml_TGaussian2<T>::AddSample (T x, T y, T weight)
00224 {
00225   fCount    += weight;
00226   fSum[0]   += weight * x;
00227   fSumSq[0] += weight * x * x;
00228   fSum[1]   += weight * y;
00229   fSumSq[1] += weight * y * y;
00230   fMixSum   += weight * x * y;
00231 }
00232 
00233 //
00234 //    UpdateStats
00235 //
00236 
00237 template <class T>
00238 inline gml_TError gml_TGaussian2<T>::UpdateStats ()
00239 {
00240   GetMean (fMean);
00241   GetVar  (fCovariance);
00242 
00243   return gml_EM2_Invert (fCovariance, fInvCovariance);
00244 }
00245 
00246 //
00247 //    GetMahaSquared
00248 //
00249 
00250 template <class T>
00251 inline T gml_TGaussian2<T>::GetMahaSquared (T x, T y)
00252 {
00253   T xn, yn;
00254 
00255   xn = x - fMean[0];
00256   yn = y - fMean[1];
00257   return fInvCovariance[0] * xn * xn
00258        + fInvCovariance[1] * xn * yn * 2
00259        + fInvCovariance[3] * yn * yn;
00260 }
00261 
00262 //
00263 //    Value
00264 //
00265 
00266 template <class T>
00267 inline T gml_TGaussian2<T>::Value (T x, T y)
00268 {
00269   return Exp (- GetMahaSquared (x, y));
00270 }
00271 
00272 //
00273 //    GetMean
00274 //
00275 
00276 template <class T>
00277 inline void gml_TGaussian2<T>::GetMean (T * m)
00278 {
00279   m[0] = fSum[0] / fCount;
00280   m[1] = fSum[1] / fCount;
00281 }
00282 
00283 //
00284 //    GetVar
00285 //
00286 
00287 template <class T>
00288 inline void gml_TGaussian2<T>::GetVar (T * m)
00289 {
00290   T   mu0 = fSum[0] / fCount;
00291   T   mu1 = fSum[1] / fCount;
00292 
00293   m[0] = fSumSq[0] / fCount - mu0 * mu0;
00294   m[3] = fSumSq[1] / fCount - mu1 * mu1;
00295   m[1] = m[2] = fMixSum / fCount - mu0 * mu1;
00296 }
00297 
00298 #endif /* __GML_GAUSSIAN2__ */
00299 
Generated on Tue Jun 12 14:03:27 2007 for gml by Doxygen 1.5.2.