00001
00002 #ifndef SAMS_BACKGROUND_HPP
00003 #define SAMS_BACKGROUND_HPP
00004
00005 #define USE_JMATH_SERIALIZATION
00006
00007 #include <string>
00008 #include <map>
00009 #include "boost/archive/binary_iarchive.hpp"
00010 #include "boost/archive/binary_oarchive.hpp"
00011 #include "boost/serialization/map.hpp"
00012 #include "boost/serialization/string.hpp"
00013 #include "jmath/jblas.hpp"
00014 #include "jmath/pca.hpp"
00015 #include "image/Image.hpp"
00016
00017 namespace jafar {
00018 namespace sams {
00019
00023 class BackgroundAnalysis {
00024 public:
00028 class Params {
00029 public:
00031 int template_width;
00033 int template_height;
00035 int nDims;
00037 double simThd;
00038
00039 Params(int tw_=11, int th_=11, int nd_=4, double sT_=0.75) :
00040 template_width(tw_), template_height(th_), nDims(nd_), simThd(sT_) {};
00041
00042 private:
00043 friend class boost::serialization::access;
00044 template <class Archive>
00045 void serialize(Archive & ar, const unsigned int version) {
00046 ar & BOOST_SERIALIZATION_NVP(template_width);
00047 ar & BOOST_SERIALIZATION_NVP(template_height);
00048 ar & BOOST_SERIALIZATION_NVP(nDims);
00049 ar & BOOST_SERIALIZATION_NVP(simThd);
00050 };
00051
00052 };
00053
00056 BackgroundAnalysis(Params params_=Params()) : params(params_) {};
00057
00061
00062
00063
00064
00065 ~BackgroundAnalysis() {
00066 std::map<std::string,Bkg>::iterator it = backgrounds.begin();
00067 for(; it != backgrounds.end(); it++)
00068 delete it->second.rec_img;
00069 };
00070
00074
00075
00079
00080
00083 inline std::vector<std::string> getLabels() const {
00084 std::vector<std::string> res;
00085 std::map<std::string, Bkg>::const_iterator it = backgrounds.begin();
00086 for(;it != backgrounds.end(); it++)
00087 res.push_back(it->first);
00088 return res;
00089 };
00090
00095 void learnNewBackground(std::string const & label_, std::vector<jafar::image::Image> const & vImg_);
00096
00101 void updateBackground(std::string const & label_, jafar::image::Image const & img_);
00102
00107 bool checkSimilarity(jafar::image::Image const & img_) const;
00108
00109
00110 private:
00112 Params params;
00113
00114
00115 struct Bkg {
00117 jafar::image::Image * rec_img;
00119 jafar::jmath::PCA pca;
00120
00121 template<class Archive>
00122 void serialize (Archive & ar, const unsigned int version) {
00123 ar & BOOST_SERIALIZATION_NVP(rec_img);
00124 ar & BOOST_SERIALIZATION_NVP(pca);
00125 }
00126 };
00127
00129 std::map<std::string, Bkg> backgrounds;
00130
00131 friend class boost::serialization::access;
00132 template<class Archive>
00133 void serialize(Archive & ar, const unsigned int version) {
00134 ar & BOOST_SERIALIZATION_NVP(params);
00135 ar & BOOST_SERIALIZATION_NVP(backgrounds);
00136 };
00137
00138
00139 jblas::vec img2vec(jafar::image::Image const & img_) const;
00140 jafar::image::Image * vec2img(jblas::vec const & vec_) const;
00141
00142 };
00143 }
00144 }
00145
00146 #endif // SAMS_BACKGROUND_HPP