ORB_SLAM2原理及代码解析:Initializer::Initialize() 函数
1 作用
2 参数及其含义
3 代码
(1)声明:Initializer.h
// Computes in parallel a fundamental matrix and a homography// Selects a model and tries to recover the motion and the structure from motionbool Initialize(const Frame &CurrentFrame, const vector<int> &vMatches12,cv::Mat &R21, cv::Mat &t21, vector<cv::Point3f> &vP3D, vector<bool> &vbTriangulated);
(2)定义:Initializer.cc
bool Initializer::Initialize(const Frame &CurrentFrame, const vector<int> &vMatches12, cv::Mat &R21, cv::Mat &t21,vector<cv::Point3f> &vP3D, vector<bool> &vbTriangulated) {// Fill structures with current keypoints and matches with reference frame// Reference Frame: 1, Current Frame: 2mvKeys2 = CurrentFrame.mvKeysUn;mvMatches12.clear();mvMatches12.reserve(mvKeys2.size());mvbMatched1.resize(mvKeys1.size());for(size_t i=0, iend=vMatches12.size();i<iend; i++){if(vMatches12[i]>=0){mvMatches12.push_back(make_pair(i,vMatches12[i]));mvbMatched1[i]=true;}elsemvbMatched1[i]=false;}const int N = mvMatches12.size();// Indices for minimum set selectionvector<size_t> vAllIndices;vAllIndices.reserve(N);vector<size_t> vAvailableIndices;for(int i=0; i<N; i++){vAllIndices.push_back(i);}// Generate sets of 8 points for each RANSAC iterationmvSets = vector< vector<size_t> >(mMaxIterations,vector<size_t>(8,0));DUtils::Random::SeedRandOnce(0);for(int it=0; it<mMaxIterations; it++){vAvailableIndices = vAllIndices;// Select a minimum setfor(size_t j=0; j<8; j++){int randi = DUtils::Random::RandomInt(0,vAvailableIndices.size()-1);int idx = vAvailableIndices[randi];mvSets[it][j] = idx;vAvailableIndices[randi] = vAvailableIndices.back();vAvailableIndices.pop_back();}}// Launch threads to compute in parallel a fundamental matrix and a homographyvector<bool> vbMatchesInliersH, vbMatchesInliersF;float SH, SF;cv::Mat H, F;thread threadH(&Initializer::FindHomography,this,ref(vbMatchesInliersH), ref(SH), ref(H));thread threadF(&Initializer::FindFundamental,this,ref(vbMatchesInliersF), ref(SF), ref(F));// Wait until both threads have finishedthreadH.join();threadF.join();// Compute ratio of scoresfloat RH = SH/(SH+SF);// Try to reconstruct from homography or fundamental depending on the ratio (0.40-0.45)if(RH>0.40)return ReconstructH(vbMatchesInliersH,H,mK,R21,t21,vP3D,vbTriangulated,1.0,50);else //if(pF_HF>0.6)return ReconstructF(vbMatchesInliersF,F,mK,R21,t21,vP3D,vbTriangulated,1.0,50);return false; }