00001
00002
00003 #ifndef HPM_ENGINE_HPP
00004 #define HPM_ENGINE_HPP
00005
00006 #include <string>
00007 #include <sstream>
00008 #include <iomanip>
00009
00010 #include "boost/tuple/tuple.hpp"
00011
00012 #include "kernel/jafarException.hpp"
00013 #include "kernel/timingTools.hpp"
00014 #include "kernel/dataLog.hpp"
00015
00016 #include "image/Image.hpp"
00017
00018 #include "gfm/Engine.hpp"
00019 #include "gfm/EngineTracking.hpp"
00020 #include "hpm/export.hpp"
00021
00022 namespace jafar {
00023 namespace hpm {
00024
00029 class Engine : public jafar::kernel::DataLoggable {
00030
00031 protected:
00032
00033
00034 jafar::image::Image imageRefRed;
00035
00036 jafar::image::Image imageMatchRed;
00037
00038 jafar::gfm::Engine* gfmEngine;
00039
00040
00041 mutable long elapsedTime;
00042
00043 mutable unsigned int nbMatchedPoints;
00044
00045 virtual void writeLogHeader(jafar::kernel::DataLogger& log) const;
00046 virtual void writeLogData(jafar::kernel::DataLogger& log) const;
00047
00048 public:
00049
00050 Engine(unsigned int imageWidth_, unsigned int imageHeight_,
00051 unsigned int reduction_ = 1);
00052
00053 jafar::image::Image const& getImageRef() const {return imageRefRed;}
00054 jafar::image::Image const& getImageMatch() const {return imageMatchRed;}
00055
00057 long getElapsedTime() const {return elapsedTime;}
00058
00066 template<typename TypeMatches>
00067 void computeMatch(jafar::image::Image const& imageRef_, jafar::image::Image const& imageMatch_,
00068 vecHarrisPoints& pointsRef, vecHarrisPoints& pointsMatch,
00069 TypeMatches& matches)
00070 {
00071
00072
00073 kernel::Chrono chrono;
00074
00075
00076 imageRef_.resize(imageRefRed);
00077 imageMatch_.resize(imageMatchRed);
00078
00079 gfm::MatchingResult gfmResult = gfmEngine->computeMatch(imageRefRed, imageMatchRed);
00080
00081 exportHarrisPoints( gfmResult.pointsRef() , pointsRef);
00082 exportHarrisPoints( gfmResult.pointsMatch() , pointsMatch);
00083 exportMatches(gfmResult, matches);
00084
00085 elapsedTime = chrono.elapsed();
00086 nbMatchedPoints = matches.size();
00087 }
00088
00089 };
00090
00091 class StereoTrackingEngine;
00092
00097 class TrackingEngine : public jafar::kernel::DataLoggable {
00098
00099 protected:
00100
00101 bool p_isInit;
00102
00103 jafar::gfm::EngineTracking* gfmEngineTracking;
00104
00105 jafar::image::Image imageRefRed;
00106
00107 jafar::image::Image imageMatchRed;
00108
00109 jafar::image::Image* imagePrevRed;
00110 jafar::image::Image* imageCurRed;
00111
00113 vecMatches matches;
00114
00116 mutable unsigned int nbTrackedPoints;
00117
00119 mutable long elapsedTime;
00120
00121 void writeLogHeader(jafar::kernel::DataLogger& log) const;
00122 void writeLogData(jafar::kernel::DataLogger& log) const;
00123
00124 public:
00125
00126 TrackingEngine(unsigned int imageWidth_, unsigned int imageHeight_,
00127 unsigned int reduction_ = 1);
00128
00129 jafar::image::Image const& getPreviousImage() const {return *imagePrevRed;}
00130 jafar::image::Image const& getCurrentImage() const {return *imageCurRed;}
00131
00133 long getElapsedTime() const { return elapsedTime;}
00134
00135 bool isInit() const {return p_isInit;}
00136
00137 vecMatches const& getMatches() const {return matches;}
00138 vecMatches const& getTrackingMatches() const {return getMatches();}
00139
00144 template<class VecPoints>
00145 void initTracking(jafar::image::Image const& imageInit_, VecPoints& pointsInit)
00146 {
00147 JFR_PRECOND(imageInit_.colorSpace() == JfrImage_CS_GRAY,
00148 "Engine::initTracking: invalid colorspace: " << imageInit_.colorSpace());
00149
00150 kernel::Chrono chrono;
00151 imagePrevRed = &imageRefRed;
00152 imageCurRed = &imageMatchRed;
00153
00154 matches.clear();
00155
00156 imageInit_.resize(*imageCurRed);
00157 gfmEngineTracking->initTracking(imageInit_);
00158 exportHarrisPoints( gfmEngineTracking->currentPoints(), pointsInit);
00159 p_isInit = true;
00160 }
00161
00168 template<class VecPoints>
00169 void track(jafar::image::Image const& imageCur_,
00170 VecPoints const& pointsPrev, VecPoints& pointsCur,
00171 bool doPropagateId = false)
00172 {
00173 JFR_PRECOND(isInit(),
00174 "TrackingEngine::track: tracking process is not initialized, call initTracking() method first");
00175 JFR_PRECOND(imageCur_.colorSpace() == JfrImage_CS_GRAY,
00176 "Engine::initTracking: invalid colorspace: " << imageCur_.colorSpace());
00177
00178
00179 kernel::Chrono chrono;
00180
00181
00182 jafar::image::Image* imageSwap = imagePrevRed;
00183 imagePrevRed = imageCurRed;
00184 imageCurRed = imageSwap;
00185
00186
00187 imageCur_.resize(*imageCurRed);
00188
00189 gfm::MatchingResult gfmResult = gfmEngineTracking->nextImage( *imageCurRed);
00190
00191 exportHarrisPoints( gfmResult.pointsMatch() , pointsCur);
00192 exportMatches(gfmResult, matches);
00193
00194 elapsedTime = chrono.elapsed();
00195
00196 if (doPropagateId)
00197 nbTrackedPoints = propagateId(matches, pointsPrev, pointsCur);
00198
00199 }
00200
00201 };
00202
00207 class StereoTrackingEngine : public jafar::kernel::DataLoggable {
00208
00209 protected:
00210
00211
00212 jafar::image::Image imageRefRed;
00213
00214 jafar::image::Image imageMatchRed;
00215
00216 jafar::image::Image* imagePrevRed;
00217 jafar::image::Image* imageCurRed;
00218
00219
00220 jafar::image::Image imageStereoRed;
00221
00223 jafar::gfm::EngineStereoTracking* gfmEngineStereoTracking;
00224
00226 vecMatches trackingMatches;
00228 vecMatches stereoMatches;
00229
00230 bool p_isInit;
00231
00233 mutable unsigned int nbTrackedPoints;
00234
00236 mutable unsigned int elapsedTime;
00237
00238
00239
00240
00241 void writeLogHeader(jafar::kernel::DataLogger& log) const;
00242 void writeLogData(jafar::kernel::DataLogger& log) const;
00243
00244 public:
00245
00246 StereoTrackingEngine(unsigned int imageWidth_, unsigned int imageHeight_,
00247 unsigned int reduction_ = 1);
00248
00249 jafar::image::Image const& getPreviousImage() const {return *imagePrevRed;}
00250 jafar::image::Image const& getCurrentImage() const {return *imageCurRed;}
00251 jafar::image::Image const& getStereoImage() const {return imageStereoRed;}
00252
00254 unsigned int getElapsedTime() const { return elapsedTime;}
00255
00256 bool isInit() const {return p_isInit;}
00257
00258 vecMatches const& getTrackingMatches() const {return trackingMatches;}
00259 vecMatches const& getStereoMatches() const {return stereoMatches;}
00260
00261 void initTracking(jafar::image::Image const& imageLeft_, jafar::image::Image const& imageRight_,
00262 vecStereoHarrisPoints& pointsLeft, vecStereoHarrisPoints& pointsRight);
00263
00264 void track(jafar::image::Image const& imageLeft_, jafar::image::Image const& imageRight_,
00265 vecStereoHarrisPoints const& pointsPrev, vecStereoHarrisPoints& pointsCur,
00266 vecStereoHarrisPoints& pointsStereo,
00267 bool doPropagateId = false);
00268
00269 };
00270
00271
00272 }
00273 }
00274
00275 #endif // HPM_ENGINE_HPP