00001 /*!@file BayesFilters/ParticleFilter.H particle Filter */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary 00034 // $HeadURL: $ 00035 // $Id: $ 00036 // 00037 00038 #ifndef ParticleFilter_H_DEFINED 00039 #define ParticleFilter_H_DEFINED 00040 00041 #include "Image/Image.H" 00042 #include "Image/MatrixOps.H" 00043 #include "Image/lapack.H" 00044 #include <stdio.h> 00045 #include <stdlib.h> 00046 00047 00048 class ParticleFilter 00049 { 00050 00051 public: 00052 struct Particle 00053 { 00054 Image<double> state; 00055 double weight; 00056 double cumlProb; //Used for selecting particles 00057 }; 00058 00059 ParticleFilter(int numStates, int numObservations, int numParticles); 00060 virtual ~ParticleFilter() {}; 00061 00062 //! The function to move from one state to another (need to be implemented) 00063 virtual Image<double> getNextState(const Image<double>& X) = 0; 00064 00065 //! The function to predict the observation from the current state 00066 virtual Image<double> getObservation(const Image<double>& X) = 0; 00067 00068 //! Predict the next state and covariance 00069 void predictState(); 00070 00071 int pickParticleToSample(void); 00072 00073 double getLikelihood(const Image<double>& z, const Image<double>& X); 00074 00075 00076 //! Predict the observations 00077 void predictObservation(); 00078 00079 //! Update the state and covariance given the observation z 00080 void update(const Image<double>& z); 00081 00082 00083 double gaussianRand(void) 00084 { 00085 static int next_gaussian = 0; 00086 static double saved_gaussian_value; 00087 00088 double fac, rsq, v1, v2; 00089 00090 if (next_gaussian == 0) { 00091 do { 00092 v1 = 2.0*uniformRandom()-1.0; 00093 v2 = 2.0*uniformRandom()-1.0; 00094 rsq = v1*v1+v2*v2; 00095 } while (rsq >= 1.0 || rsq == 0.0); 00096 fac = sqrt(-2.0*log(rsq)/rsq); 00097 saved_gaussian_value=v1*fac; 00098 next_gaussian=1; 00099 return v2*fac; 00100 } else { 00101 next_gaussian=0; 00102 return saved_gaussian_value; 00103 } 00104 } 00105 00106 double uniformRandom(void) 00107 { 00108 return (double) rand() / (double) RAND_MAX; 00109 } 00110 00111 00112 00113 00114 protected: 00115 int itsNumStates; 00116 int itsNumObservations; 00117 00118 std::vector<Particle> itsParticles; 00119 00120 private: 00121 double largestCumlProb; 00122 00123 00124 }; 00125 00126 // ###################################################################### 00127 /* So things look consistent in everyone's emacs... */ 00128 /* Local Variables: */ 00129 /* indent-tabs-mode: nil */ 00130 /* End: */ 00131 00132 #endif