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 #if !defined(_PLANAR_SFM_H)
00036 #define _PLANAR_SFM_H
00037 
00038 #include <posest/pe.h>
00039 #include <opencv2/core/core.hpp>
00040 #include <opencv2/features2d/features2d.hpp>
00041 
00042 namespace pe
00043 {
00044 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 void planarSFM(const cv::Mat& intrinsics, const std::vector<cv::KeyPoint>& set1, const std::vector<cv::KeyPoint>& set2, const std::vector<int>& indices,
00058                cv::Mat& H, cv::Mat& R, cv::Mat& T, double reprojectionError = 6.0);
00059 
00060 double SFMwithSBA(const cv::Mat& intrinsics, const std::vector<cv::KeyPoint>& points1, const std::vector<cv::KeyPoint>& points2,
00061         const std::vector<int>& indices, cv::Mat& rvec, cv::Mat& T, double reprojectionError);
00062 
00063 double SFMwithSBA(const cv::Mat& intrinsics, std::vector<cv::Point2f>& points1, std::vector<cv::Point2f>& points2,
00064         cv::Mat& rvec, cv::Mat& T, double reprojectionError);
00065 
00066 double SFM(const cv::Mat& intrinsics, const std::vector<cv::KeyPoint>& set1, const std::vector<cv::KeyPoint>& set2, const std::vector<int>& indices,
00067                 cv::Mat& R, cv::Mat& T, double reprojectionError = 6.0);
00068 
00069 double SFM(const cv::Mat& intrinsics, const std::vector<cv::Point2f>& points1, const std::vector<cv::Point2f>& points2,
00070         cv::Mat& R, cv::Mat& T, double reprojectionError = 6.0);
00071 
00072 double avgSampsonusError(const cv::Mat& essential, const std::vector<cv::Point2f>& points1, const std::vector<cv::Point2f>& points2,
00073                 double max_error = 1.0, bool verbose = false);
00074 
00075 cv::Mat calcEssentialMatrix(const cv::Mat& intrinsics_inv, const cv::Mat& R, const cv::Mat& T);
00076 
00077 inline cv::Point3f crossProduct(cv::Point3f& p1, cv::Point3f& p2)
00078 {
00079     cv::Point3f p3(p1.y*p2.z - p1.z*p2.y, p1.z*p2.x - p1.x*p2.z, p1.x*p2.y - p1.y*p2.x);
00080     return p3;
00081 };
00082 
00083 void dumpFltMat(const char* name, const cv::Mat& mat);
00084 
00085 void keyPoints2Point2f(const cv::vector<cv::KeyPoint>& src, std::vector<cv::Point2f>& dst);
00086 
00087 void reprojectPoints(const cv::Mat& intrinsics, const cv::Mat& R, const cv::Mat& T, const std::vector<cv::Point2f>& p1,
00088     const std::vector<cv::Point2f>& p2, std::vector<cv::Point3f>& p, std::vector<bool>& valid);
00089 
00090 void matchesFromIndices(const std::vector<cv::KeyPoint>& _set1, const std::vector<cv::KeyPoint>& _set2, const std::vector<int>& indices,
00091                         std::vector<cv::Point2f>& points1, std::vector<cv::Point2f>& points2);
00092 
00093 void matchesFromIndices(const std::vector<cv::KeyPoint>& src1, const std::vector<cv::KeyPoint>& src2, const std::vector<cv::DMatch>& indices,
00094     std::vector<cv::Point2f>& dst1, std::vector<cv::Point2f>& dst2);
00095 
00096 float calcScaledPointCloudDistance(const std::vector<cv::Point3f>& points1, const std::vector<cv::Point3f>& points2);
00097 
00098 void sba(const cv::Mat& intrinsics, cv::Mat& rvec, cv::Mat& tvec, std::vector<cv::Point3f>& points,
00099     const std::vector<cv::Point2f>& points1, const std::vector<cv::Point2f>& points2);
00100 
00101 template<class T>
00102 void filterVector(const std::vector<T>& src, const std::vector<bool>& valid, std::vector<T>& dst)
00103 {
00104     dst.clear();
00105     for(size_t i = 0; i < valid.size(); i++)
00106     {
00107         if(valid[i])
00108         {
00109             dst.push_back(src[i]);
00110         }
00111     }
00112 };
00113 
00114 template<class T>
00115 void filterVector(const std::vector<T>& src, const std::vector<int>& valid, std::vector<T>& dst)
00116 {
00117     dst.clear();
00118     for(size_t i = 0; i < valid.size(); i++)
00119     {
00120         if(valid[i])
00121         {
00122             dst.push_back(src[i]);
00123         }
00124     }
00125 };
00126 
00127 template<class T>
00128 void filterVector(std::vector<T>& v, const std::vector<bool>& valid)
00129 {
00130     std::vector<T> out;
00131     filterVector(v, valid, out);
00132     v = out;
00133 };
00134 
00135 void computeEpipolarInliers(const cv::Mat& essential, const std::vector<cv::Point2f>& points1, const std::vector<cv::Point2f>& points2,
00136     std::vector<bool>& inliers, double maxError = 3.0);
00137 
00138 void findNaNPoints(const std::vector<cv::Point3f>& points, std::vector<bool>& valid);
00139 
00140 void filterInliers(const std::vector<cv::Point3f>& obj_pts, const std::vector<cv::Point2f>& img1_pts,
00141     const std::vector<cv::Point2f>& img2_pts, const cv::Mat& R, const cv::Mat& T, const cv::Mat& intrinsics,
00142     double reprojectionError, std::vector<bool>& valid);
00143 
00144 void keyPoints2Point2f(const std::vector<cv::KeyPoint>& src, std::vector<cv::Point2f>& dst);
00145 
00146 template<class T>
00147 void vectorSubset(const std::vector<T>& src, const std::vector<int>& indices, std::vector<T>& dst)
00148 {
00149     dst.clear();
00150     for(size_t i = 0; i < indices.size(); i++)
00151     {
00152         dst.push_back(src[indices[i]]);
00153     }
00154 };
00155 
00156 void sample(int max_index, int count, std::vector<int>& sample_indices);
00157 
00158 cv::Point3f mult(const cv::Mat& M, const cv::Point3f& p);
00159 
00160 void calcRelativeRT(const cv::Mat& R1, const cv::Mat& T1, const cv::Mat& R2, const cv::Mat& T2,
00161                     cv::Mat& dR, cv::Mat& dT);
00162 
00163 }
00164 
00165 
00166 #endif //_PLANAR_SFM_H