LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiGaussian.h

00001 /*
00002  * Copyright (C) 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*--------------------------------------------------------------------
00025  * project ....: LTI-Lib: Image Processing and Computer Vision Library
00026  * file .......: ltiGaussian.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 18.3.2002
00030  * revisions ..: $Id: ltiGaussian.h,v 1.11 2006/02/08 12:23:36 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_GAUSSIAN_H_
00034 #define _LTI_GAUSSIAN_H_
00035 
00036 #include "ltiMath.h"
00037 #include "ltiMathFunction.h"
00038 #include "ltiVector.h"
00039 #include "ltiMatrix.h"
00040 
00041 namespace lti {
00042   /**
00043    * This function evaluates a multivariate gaussian with the form:
00044    * \f[ g(\mathbf{x}) = \frac{1}{(2\pi)^{n/2} |\boldsymbol{\Sigma}|^{1/2}}
00045    * \cdot \exp\left(-\frac{1}{2}(\mathbf{x}-\boldsymbol{\mu})^T
00046    * \boldsymbol{\Sigma}^{-1}(\mathbf{x}-\boldsymbol{\mu})\right)\f]
00047    *
00048    * The template value T is the type of the container elements (double or
00049    * float).
00050    */
00051   template<class T>
00052   class gaussian : public mathFunction {
00053   public:
00054     /**
00055      * the parameters for the class gaussian
00056      */
00057     class parameters : public mathFunction::parameters {
00058     public:
00059       /**
00060        * default constructor
00061        */
00062       parameters() 
00063         : mathFunction::parameters() {
00064         mean = vector<T>(3,T(0));
00065         covariance = matrix<T>(3,3,T(0));
00066         covariance.setIdentity();
00067       };
00068 
00069       /**
00070        * copy constructor
00071        * @param other the parameters object to be copied
00072        */
00073       parameters(const parameters& other)
00074         : mathFunction::parameters() {
00075         copy(other);
00076       };
00077 
00078       /**
00079        * destructor
00080        */
00081       ~parameters() {};
00082 
00083       /**
00084        * returns name of this type
00085        */
00086       const char* getTypeName() const {
00087         return "gaussian::parameters";
00088       };
00089 
00090       /**
00091        * copy the contents of a parameters object
00092        * @param other the parameters object to be copied
00093        * @return a reference to this parameters object
00094        */
00095       parameters& copy(const parameters& other) {
00096 #     ifndef _LTI_MSC_6
00097         // MS Visual C++ 6 is not able to compile this...
00098         mathFunction::parameters::copy(other);
00099 #     else
00100         // ...so we have to use this workaround.
00101         // Conditional on that, copy may not be virtual.
00102         mathFunction::parameters& (mathFunction::parameters::* p_copy)
00103           (const mathFunction::parameters&) =
00104           mathFunction::parameters::copy;
00105         (this->*p_copy)(other);
00106 #     endif
00107 
00108         mean.copy(other.mean);
00109         covariance.copy(other.covariance);
00110 
00111         return *this;
00112       };
00113 
00114       /**
00115        * copy the contents of a parameters object
00116        * @param other the parameters object to be copied
00117        * @return a reference to this parameters object
00118        */
00119       parameters& operator=(const parameters& other) {
00120         return copy(other);
00121       };
00122 
00123       /**
00124        * returns a pointer to a clone of the parameters
00125        */
00126       virtual functor::parameters* clone() const {
00127         return new parameters(*this);
00128       };
00129 
00130 #     ifndef _LTI_MSC_6
00131       /**
00132        * write the parameters in the given ioHandler
00133        * @param handler the ioHandler to be used
00134        * @param complete if true (the default) the enclosing begin/end will
00135        *        be also written, otherwise only the data block will be written.
00136        * @return true if write was successful
00137        */
00138       virtual bool write(ioHandler& handler,
00139                          const bool complete) const
00140 #     else
00141       /**
00142        * this function is required by MSVC only, as a workaround for a
00143        * very awful bug, which exists since MSVC V.4.0, and still by
00144        * V.6.0 with all bugfixes (so called "service packs") remains
00145        * there...  This method is also public due to another bug, so please
00146        * NEVER EVER call this method directly: use write() instead
00147        */
00148       bool writeMS(ioHandler& handler,
00149                    const bool complete) const
00150 #     endif
00151       {
00152         bool b = true;
00153         if (complete) {
00154           b = handler.writeBegin();
00155         }
00156 
00157         if (b) {
00158 
00159           lti::write(handler,"mean",mean);
00160           lti::write(handler,"covariance",covariance);
00161         }
00162 
00163 #     ifndef _LTI_MSC_6
00164         // This is the standard C++ code, which MS Visual C++ 6 is not able to
00165         // compile...
00166         b = b && mathFunction::parameters::write(handler,false);
00167 #     else
00168         bool
00169         (mathFunction::parameters::* p_writeMS)(ioHandler&,
00170                                                 const bool) const =
00171           mathFunction::parameters::writeMS;
00172         b = b && (this->*p_writeMS)(handler,false);
00173 #     endif
00174 
00175         if (complete) {
00176           b = b && handler.writeEnd();
00177         }
00178 
00179         return b;
00180       }
00181 
00182 #     ifdef _LTI_MSC_6
00183       /**
00184        * write the parameters in the given ioHandler
00185        * @param handler the ioHandler to be used
00186        * @param complete if true (the default) the enclosing begin/end will
00187        *        be also written, otherwise only the data block will be written.
00188        * @return true if write was successful
00189        */
00190       virtual bool write(ioHandler& handler,
00191                          const bool complete) const {
00192         // ...we need this workaround to cope with another really
00193         // awful MSVC bug.
00194         return writeMS(handler,complete);
00195       }
00196 #     endif
00197 
00198 #     ifndef _LTI_MSC_6
00199       /**
00200        * read the parameters from the given ioHandler
00201        * @param handler the ioHandler to be used
00202        * @param complete if true (the default) the enclosing begin/end will
00203        *        be also written, otherwise only the data block will be written.
00204        * @return true if write was successful
00205        */
00206       virtual bool read(ioHandler& handler,
00207                         const bool complete)
00208 #     else
00209       /**
00210        * this function is required by MSVC only, as a workaround for a
00211        * very awful bug, which exists since MSVC V.4.0, and still by
00212        * V.6.0 with all bugfixes (so called "service packs") remains
00213        * there...  This method is also public due to another bug, so please
00214        * NEVER EVER call this method directly: use read() instead
00215        */
00216       bool readMS(ioHandler& handler,
00217                   const bool complete)
00218 #     endif
00219       {
00220         bool b = true;
00221         if (complete) {
00222           b = handler.readBegin();
00223         }
00224 
00225         if (b) {
00226 
00227           lti::read(handler,"mean",mean);
00228           lti::read(handler,"covariance",covariance);
00229         }
00230 
00231 #     ifndef _LTI_MSC_6
00232         // This is the standard C++ code, which MS Visual C++ 6 is not able to
00233         // compile...
00234         b = b && mathFunction::parameters::read(handler,false);
00235 #     else
00236         bool (mathFunction::parameters::* p_readMS)(ioHandler&,const bool) =
00237           mathFunction::parameters::readMS;
00238         b = b && (this->*p_readMS)(handler,false);
00239 #     endif
00240 
00241         if (complete) {
00242           b = b && handler.readEnd();
00243         }
00244 
00245         return b;
00246       }
00247 
00248 #     ifdef _LTI_MSC_6
00249       /**
00250        * read the parameters from the given ioHandler
00251        * @param handler the ioHandler to be used
00252        * @param complete if true (the default) the enclosing begin/end will
00253        *        be also written, otherwise only the data block will be written.
00254        * @return true if write was successful
00255        */
00256       virtual bool read(ioHandler& handler,
00257                         const bool complete) {
00258         // ...we need this workaround to cope with another really awful MSVC
00259         // bug.
00260         return readMS(handler,complete);
00261       }
00262 #     endif
00263 
00264       /**
00265        * Set the mean and covariance
00266        */
00267       void setMeanAndCovariance(const vector<T>& mu,
00268                                 const matrix<T>& covar) {
00269         mean.copy(mu);
00270         covariance.copy(covar);
00271       }
00272 
00273       /**
00274        * For the one dimensional case, you can set the proper mean and
00275        * variance values using this method, which resizes properly the
00276        * mean and covariance attributes
00277        */
00278       void setMeanAndVariance(const T mu,const T sigma2) {
00279         mean.resize(1,mu,false,true);
00280         covariance.resize(1,1,sigma2,false,true);
00281       }
00282 
00283       // ------------------------------------------------
00284       // the parameters
00285       // ------------------------------------------------
00286 
00287       /**
00288        * Mean vector.  It must have the same dimension than the presented
00289        * vectors.
00290        *
00291        * Default value:  vector<T>(3,T(0))
00292        */
00293       vector<T> mean;
00294 
00295       /**
00296        * Covariance matrix.  Square matrix with the same number of
00297        * rows (or columns) than the dimension of the mean vector.
00298        *
00299        * Default value: 3x3 identity matrix
00300        */
00301       matrix<T> covariance;
00302 
00303     };
00304 
00305     /**
00306      * default constructor
00307      */
00308     gaussian();
00309 
00310     /**
00311      * Construct a 1D gaussian with the given mean and variance
00312      *
00313      * Note that the second parameter is the variance and not the standard
00314      * deviation.
00315      */
00316     gaussian(const T mean, const T variance);
00317 
00318     /**
00319      * construct a n-dimensional gaussian with the given mean vector and
00320      * covariance matrix.
00321      */
00322     gaussian(const vector<T>& mean, const matrix<T>& covar);
00323 
00324     /**
00325      * copy constructor
00326      * @param other the object to be copied
00327      */
00328     gaussian(const gaussian<T>& other);
00329 
00330     /**
00331      * destructor
00332      */
00333     virtual ~gaussian();
00334 
00335     /**
00336      * returns the name of this type ("gaussian")
00337      */
00338     virtual const char* getTypeName() const;
00339 
00340     /**
00341      * computes the gaussian for the given vector, and leave the result
00342      * in the dest parameter.
00343      * @param src vector<T> with the source data.
00344      * @param dest function value
00345      * @return true if apply successful or false otherwise.
00346      */
00347     bool apply(const vector<T>& src,T& dest) const;
00348 
00349     /**
00350      * alias for apply
00351      * @param vct vector<T> with the source data.
00352      * @return function value or negative if error occurred
00353      */
00354     T operator()(const vector<T>& vct);
00355 
00356     /**
00357      * Computes the gaussian for the given scalar, and leave the result
00358      * in the dest parameter.
00359      *
00360      * This is a shortcut for the 1D case.  If the mean and covariance
00361      * given in the parameters have more the one dimension, the result
00362      * will not be valid.
00363      * @param src input scalar
00364      * @param dest function value
00365      * @return true if apply successful or false otherwise.
00366      */
00367     bool apply(const T src,T& dest) const;
00368 
00369     /**
00370      * Alias for apply
00371      * @param x input scalar
00372      * @return function value
00373      */
00374     T operator()(const T x);
00375 
00376     /**
00377      * Shortcut to set the mean vector and covariance matrix
00378      */
00379     void setMeanAndCovariance(const vector<T>& mean,const matrix<T>& covar);
00380 
00381     /**
00382      * Shortcut to set a one dimensional gaussian with the mean value and
00383      * the variance.
00384      */
00385     void setMeanAndVariance(const T mean,const T variance);
00386 
00387     /**
00388      * copy data of "other" functor.
00389      * @param other the functor to be copied
00390      * @return a reference to this functor object
00391      */
00392     gaussian& copy(const gaussian& other);
00393 
00394     /**
00395      * alias for copy member
00396      * @param other the functor to be copied
00397      * @return a reference to this functor object
00398      */
00399     gaussian& operator=(const gaussian& other);
00400 
00401     /**
00402      * returns a pointer to a clone of this functor.
00403      */
00404     virtual functor* clone() const;
00405 
00406     /**
00407      * returns used parameters
00408      */
00409     const parameters& getParameters() const;
00410 
00411     /**
00412      * set functor's parameters.
00413      * This member makes a copy of <em>theParam</em>: the functor
00414      * will keep its own copy of the parameters!
00415      * @return true if successful, false otherwise
00416      */
00417     virtual bool updateParameters();
00418 
00419   protected:
00420     /**
00421      * inverse of covariance matrix
00422      */
00423     matrix<T> invCovariance;
00424 
00425     /**
00426      * mean vector
00427      */
00428     vector<T> meanVct;
00429 
00430     /**
00431      * determinant of the covariance matrix
00432      */
00433     T norm;
00434 
00435     /**
00436      * inverse of -2 times the variance for the 1D case
00437      */
00438     T invVarianceH;
00439 
00440     /**
00441      * mean value for the 1D case
00442      */
00443     T mean;
00444 
00445   };
00446 }
00447 
00448 #endif

Generated on Sat Apr 10 15:25:33 2010 for LTI-Lib by Doxygen 1.6.1