00001
00002
00003 #ifndef VME_ENGINE_HPP
00004 #define VME_ENGINE_HPP
00005
00006 #include <list>
00007
00008 #include "kernel/jafarException.hpp"
00009 #include "kernel/timingTools.hpp"
00010 #include "kernel/dataLog.hpp"
00011 #include "kernel/keyValueFile.hpp"
00012
00013 #include "jmath/jblas.hpp"
00014
00015 #include "geom/t3d.hpp"
00016
00017 #include "image/Image.hpp"
00018 #include "camera/cameraPinhole.hpp"
00019
00020 #include "gfm/MatchingResult.hpp"
00021 #include "gfm/EngineTracking.hpp"
00022
00023 namespace jafar {
00024 namespace vme {
00025
00030 class Point3D {
00031
00032 public:
00033
00034 Point3D(double disparity_, jblas::vec3 const& x_);
00035
00036 Point3D(double disparity_,
00037 jblas::vec3 const& x_,
00038 jblas::sym_mat const& xCov_);
00039
00040 double disparity;
00041
00043 jblas::vec3 x;
00045 jblas::sym_mat xCov;
00046
00047 };
00048
00054 class Point3DPair {
00055
00056 public:
00057
00058 Point3DPair(Point3D const& first_, Point3D const& second_) :
00059 first(first_),
00060 second(second_)
00061 {}
00062
00064 Point3D const& first;
00066 Point3D const& second;
00068 double weight;
00070 double error;
00071
00072 };
00073
00078 class VmeEngineParams : public jafar::kernel::KeyValueFileLoad {
00079 public:
00080
00092 VmeEngineParams(bool enableWeight_ = true,
00093 bool enableOutliers_ = true,
00094 bool enableUncertainty_ = true,
00095 double minDisparity_ = 0.5, double maxStereoDeltaV_ = 1.0,
00096 std::size_t nbMin3DPointPairs_ = 3,
00097 int removeOutliersKmax_ = 4, int removeOutliersKmin_ = 3,
00098 double sigmaPix_ = 0.5, double sigmaDisp_ = 0.5) :
00099 enableWeight(enableWeight_),
00100 enableOutliers(enableOutliers_),
00101 enableUncertainty(enableUncertainty_),
00102 minDisparity(minDisparity_),
00103 maxStereoDeltaV(maxStereoDeltaV_),
00104 removeOutliersKmax(removeOutliersKmax_),
00105 removeOutliersKmin(removeOutliersKmin_),
00106 nbMin3DPointPairs(nbMin3DPointPairs_),
00107 sigmaPix(sigmaPix_),
00108 sigmaDisp(sigmaDisp_)
00109 {}
00110
00114 bool enableWeight;
00115
00117 bool enableOutliers;
00118
00120 bool enableUncertainty;
00121
00123 double minDisparity;
00124
00128 double maxStereoDeltaV;
00129
00131 int removeOutliersKmax;
00132
00134 int removeOutliersKmin;
00135
00137 std::size_t nbMin3DPointPairs;
00138
00140 double sigmaPix;
00141
00143 double sigmaDisp;
00144
00145 void check() const {
00146 JFR_TRACE_BEGIN;
00147 JFR_CHECK_PARAM(minDisparity > 0.0, minDisparity);
00148 JFR_CHECK_PARAM(maxStereoDeltaV > 0, maxStereoDeltaV);
00149 JFR_CHECK_PARAM(nbMin3DPointPairs >= 3, nbMin3DPointPairs);
00150 JFR_CHECK_PARAM(removeOutliersKmin >= 3, removeOutliersKmin);
00151 JFR_CHECK_PARAM(removeOutliersKmax >= removeOutliersKmin, removeOutliersKmax);
00152 JFR_CHECK_PARAM(sigmaPix >= 0.0, sigmaPix);
00153 JFR_CHECK_PARAM(sigmaDisp >= 0.0, sigmaDisp);
00154 JFR_TRACE_END("VmeEngineParams::check");
00155 };
00156
00157 virtual void loadKeyValueFile(jafar::kernel::KeyValueFile const& keyValueFile);
00158
00159 };
00160
00165 class VmeEngine : public jafar::kernel::DataLoggable {
00166
00167 private:
00168
00170 VmeEngineParams params;
00171
00172
00173
00175 double errorMean;
00177 double errorSigma;
00178
00179 gfm::MatchingResult m_trackingResult;
00180 gfm::MatchingResult m_stereoResult;
00181
00183 std::list<Point3D> point3DListPrev;
00184
00186 std::vector<Point3D*> point3DIndexPrev;
00187
00189 std::list<Point3D> point3DListCur;
00190
00192 std::vector<Point3D*> point3DIndexCur;
00193
00194 typedef std::list<Point3DPair> Point3DPairList;
00196 Point3DPairList me3DPointPairs;
00197
00199 std::size_t sizeMe3DPointPairs;
00200
00201 double sumMe3DPointPairsWeights;
00202
00204 jblas::mat33 R;
00205
00207 jblas::vec3 t;
00208
00209
00210
00211
00213 jblas::diag_mat uvdCov;
00214
00216 geom::T3D* m_t3d;
00217
00218
00219
00220 camera::StereoBench m_stereoBench;
00221
00222 gfm::EngineStereoTracking m_engineStereoTracking;
00223
00224
00225 unsigned int frameIndex;
00226
00228 int nbOutliers;
00229
00230 long elapsedTime;
00231 bool success;
00232
00237 void fillPoint3DList(const jafar::gfm::MatchingResult& stereoResult,
00238 std::list<Point3D>& point3DList,
00239 std::vector<Point3D*>& point3DIndex);
00240 void estimateMotion(bool doComputeErrors);
00241 bool removeOutliers(int k);
00242 void estimateMotionUncertainty();
00243
00244 protected:
00245
00246 virtual void writeLogHeader(jafar::kernel::DataLogger& log) const;
00247 virtual void writeLogData(jafar::kernel::DataLogger& log) const;
00248
00249 public:
00250
00251 VmeEngine(jafar::geom::T3D::Type t3dType, jafar::camera::StereoBench const& stereoBench);
00252
00253 ~VmeEngine();
00254
00255 void setStereoBench( jafar::camera::StereoBench const& stereoBench )
00256 {
00257 m_stereoBench = stereoBench;
00258 }
00259
00260
00261 std::size_t nbTracked3DPoint() {
00262 return sizeMe3DPointPairs;
00263 }
00264 int nb3DPointsOutliers() {
00265 return nbOutliers;
00266 }
00267
00268
00270 void setParams(VmeEngineParams const& params_) {
00271 params_.check();
00272 params = params_;
00273 uvdCov(0,0) = params.sigmaPix*params.sigmaPix;
00274 uvdCov(1,1) = params.sigmaPix*params.sigmaPix;
00275 uvdCov(2,2) = params.sigmaDisp*params.sigmaDisp;
00276 }
00277
00278 const jafar::gfm::MatchingResult& getStereoResult() const {return m_stereoResult;}
00279 const jafar::gfm::MatchingResult& getTrackResult() const {return m_trackingResult;}
00280
00281 jafar::geom::T3D& t3d() {return *m_t3d;}
00282
00283 long getElapsedTime() const {return elapsedTime;}
00284
00285 void init(const jafar::image::Image& imageLeft,
00286 const jafar::image::Image& imageRight,
00287 unsigned int frameIndex_ = 0);
00288
00289 void init(const jafar::gfm::MatchingResult& stereoResult,
00290 unsigned int frameIndex_ = 0);
00291
00292 void estimateMotion(const jafar::image::Image& imageLeft,
00293 const jafar::image::Image& imageRight,
00294 unsigned int frameIndex_ = 0);
00295
00296 void estimateMotion(const jafar::gfm::MatchingResult& trackingResult,
00297 const jafar::gfm::MatchingResult& stereoResult,
00298 unsigned int frameIndex_ = 0);
00299
00303 int count3DPointPairs() const {
00304 return sizeMe3DPointPairs;
00305 }
00306 };
00307
00308 }
00309 }
00310
00311 #endif // VME_ENGINE_HPP