Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #include <posest/pe.h>
00037 #include <sba/sba.h>
00038 #include <Eigen/SVD>
00039 #include <Eigen/LU>
00040 #include <iostream>
00041 
00042 
00043 using namespace Eigen;
00044 using namespace sba;
00045 using namespace frame_common;
00046 using namespace std;
00047 
00048 namespace pe
00049 {
00050   PoseEstimator::PoseEstimator(int NRansac, bool LMpolish, double mind,
00051                                    double maxidx, double maxidd) : testMode(false)
00052   {
00053     polish = LMpolish;
00054     numRansac = NRansac;
00055     minMatchDisp = mind;
00056     maxInlierXDist2 = maxidx*maxidx;
00057     maxInlierDDist2 = maxidd*maxidd;
00058     rot.setIdentity();
00059     trans.setZero();
00060     
00061     
00062     matcher = cv::Ptr<cv::DescriptorMatcher>(new cv::BFMatcher(cv::NORM_L2));
00063     wx = 92; wy = 48;
00064     windowed = true;
00065   }
00066 
00067 void PoseEstimator::matchFrames(const fc::Frame& f0, const fc::Frame& f1, std::vector<cv::DMatch>& fwd_matches)
00068   {
00069     cv::Mat mask;
00070     if (windowed)
00071       mask = cv::windowedMatchingMask(f0.kpts, f1.kpts, wx, wy);
00072 
00073     matcher->clear();
00074     matcher->match(f0.dtors, f1.dtors, fwd_matches, mask); 
00075   }
00076 
00077   
00078   
00079   
00080   
00081   
00082   
00083   
00084   
00085 
00086   int PoseEstimator::estimate(const Frame &f0, const Frame &f1)
00087   {
00088     
00089     matches.clear();
00090     inliers.clear();
00091 
00092     
00093     std::vector<cv::DMatch> fwd_matches, rev_matches;
00094     matchFrames(f0, f1, fwd_matches);
00095     matchFrames(f1, f0, rev_matches);
00096     
00097 
00098     
00099     for (int i = 0; i < (int)fwd_matches.size(); ++i) {
00100       if (fwd_matches[i].trainIdx >= 0)
00101         matches.push_back( cv::DMatch(i, fwd_matches[i].trainIdx, 0.f) );
00102     }
00103     for (int i = 0; i < (int)rev_matches.size(); ++i) {
00104       if (rev_matches[i].trainIdx >= 0 && i != fwd_matches[rev_matches[i].trainIdx].trainIdx)
00105         matches.push_back( cv::DMatch(rev_matches[i].trainIdx, i, 0.f) );
00106     }
00107     
00108     
00109     
00110     return estimate(f0, f1, matches);
00111   }
00112 
00113   void PoseEstimator::setMatcher(const cv::Ptr<cv::DescriptorMatcher>& new_matcher)
00114   {
00115     matcher = new_matcher;
00116   }
00117   
00118   void PoseEstimator::setTestMode(bool mode)
00119   {
00120     testMode = mode;
00121   };
00122 }