KeyFrame是SLAM中的关键的帧,对于恢复地图非常重要,关键帧由普通帧构造,什么样的普通帧、以及什么时候插入关键帧,在tracking模块中实现。
KeyFrame.h
#ifndef KEYFRAME_H
#define KEYFRAME_H
#include "MapPoint.h"
#include "Thirdparty/DBoW2/DBoW2/BowVector.h"
#include "Thirdparty/DBoW2/DBoW2/FeatureVector.h"
#include "ORBVocabulary.h"
#include "ORBextractor.h"
#include "Frame.h"
#include "KeyFrameDatabase.h"
#include
namespace ORB_SLAM2
{
class Map;
class MapPoint;
class Frame;
class KeyFrameDatabase;
/* KeyFrame
* 关键帧,和普通的Frame不一样,但是可以由Frame来构造
* 许多数据会被三个线程同时访问,所以用锁的地方很普遍
*/
class KeyFrame
{
public:
KeyFrame(Frame &F, Map* pMap, KeyFrameDatabase* pKFDB);
// Pose functions
// 这里的set,get需要用到锁
void SetPose(const cv::Mat &Tcw);
cv::Mat GetPose();
cv::Mat GetPoseInverse();
cv::Mat GetCameraCenter();
cv::Mat GetStereoCenter();
cv::Mat GetRotation();
cv::Mat GetTranslation();
// Bag of Words Representation
void ComputeBoW();
// Covisibility graph functions
void AddConnection(KeyFrame* pKF, const int &weight);
void EraseConnection(KeyFrame* pKF);
void UpdateConnections();
void UpdateBestCovisibles();
std::set GetConnectedKeyFrames();
std::vector GetVectorCovisibleKeyFrames();
std::vector GetBestCovisibilityKeyFrames(const int &N);
std::vector GetCovisiblesByWeight(const int &w);
int GetWeight(KeyFrame* pKF);
// Spanning tree functions
void AddChild(KeyFrame* pKF);
void EraseChild(KeyFrame* pKF);
void ChangeParent(KeyFrame* pKF);
std::set GetChilds();
KeyFrame* GetParent();
bool hasChild(KeyFrame* pKF);
// Loop Edges
void AddLoopEdge(KeyFrame* pKF);
std::set GetLoopEdges();
// MapPoint observation functions
void AddMapPoint(MapPoint* pMP, const size_t &idx);
void EraseMapPointMatch(const size_t &idx);
void EraseMapPointMatch(MapPoint* pMP);
void ReplaceMapPointMatch(const size_t &idx, MapPoint* pMP);
std::set GetMapPoints();
std::vector GetMapPointMatches();
int TrackedMapPoints(const int &minObs);
MapPoint* GetMapPoint(const size_t &idx);
// KeyPoint functions
std::vector GetFeaturesInArea(const float &x, const float &y, const float &r) const;
cv::Mat UnprojectStereo(int i);
// Image
bool IsInImage(const float &x, const float &y) const;
// Enable/Disable bad flag changes
void SetNotErase();
void SetErase();
// Set/check bad flag
void SetBadFlag();
bool isBad();
// Compute Scene Depth (q=2 median). Used in monocular.
float ComputeSceneMedianDepth(const int q);
static bool weightComp( int a, int b)
{
return a>b;
}
static bool lId(KeyFrame* pKF1, KeyFrame* pKF2)
{
return pKF1->mnIdmnId;
}
// The following variables are accesed from only 1 thread or never change (no mutex needed).
public:
// nNextID名字改为nLastID更合适,表示上一个KeyFrame的ID号
static long unsigned int nNextId;
// 在nNextID的基础上加1就得到了mnID,为当前KeyFrame的ID号
long unsigned int mnId;
// 每个KeyFrame基本属性是它是一个Frame,KeyFrame初始化的时候需要Frame,
// mnFrameId记录了该KeyFrame是由哪个Frame初始化的
const long unsigned int mnFrameId;
const double mTimeStamp;
// Grid (to speed up feature matching)
// 和Frame类中的定义相同
const int mnGridCols;
const int mnGridRows;
const float mfGridElementWidthInv;
const float mfGridElementHeightInv;
// Variables used by the tracking
long unsigned int mnTrackReferenceForFrame;
long unsigned int mnFuseTargetForKF;
// Variables used by the local mapping
long unsigned int mnBALocalForKF;
long unsigned int mnBAFixedForKF;
// Variables used by the keyframe database
long unsigned int mnLoopQuery;
int mnLoopWords;
float mLoopScore;
long unsigned int mnRelocQuery;
int mnRelocWords;
float mRelocScore;
// Variables used by loop closing
cv::Mat mTcwGBA;
cv::Mat mTcwBefGBA;
long unsigned int mnBAGlobalForKF;
// Calibration parameters
const float fx, fy, cx, cy, invfx, invfy, mbf, mb, mThDepth;
// Number of KeyPoints
const int N;
// KeyPoints, stereo coordinate and descriptors (all associated by an index)
// 和Frame类中的定义相同
const std::vector mvKeys;
const std::vector mvKeysUn;
const std::vector mvuRight; // negative value for monocular points
const std::vector mvDepth; // negative value for monocular points
const cv::Mat mDescriptors;
//BoW
DBoW2::BowVector mBowVec; ///< Vector of words to represent images
DBoW2::FeatureVector mFeatVec; ///< Vector of nodes with indexes of local features
// Pose relative to parent (this is computed when bad flag is activated)
cv::Mat mTcp;
// Scale
const int mnScaleLevels;
const float mfScaleFactor;
const float mfLogScaleFactor;
const std::vector mvScaleFactors;// 尺度因子,scale^n,scale=1.2,n为层数
const std::vector mvLevelSigma2;// 尺度因子的平方
const std::vector mvInvLevelSigma2;
// Image bounds and calibration
const int mnMinX;
const int mnMinY;
const int mnMaxX;
const int mnMaxY;
const cv::Mat mK;
// The following variables need to be accessed trough a mutex to be thread safe.
protected:
// SE3 Pose and camera center
cv::Mat Tcw;
cv::Mat Twc;
cv::Mat Ow;
cv::Mat Cw; // Stereo middel point. Only for visualization
// MapPoints associated to keypoints
std::vector mvpMapPoints;
// BoW
KeyFrameDatabase* mpKeyFrameDB;
ORBVocabulary* mpORBvocabulary;
// Grid over the image to speed up feature matching
std::vector< std::vector > > mGrid;
// Covisibility Graph
std::map mConnectedKeyFrameWeights; ///< 与该关键帧连接的关键帧与权重
std::vector mvpOrderedConnectedKeyFrames; ///< 排序后的关键帧
std::vector mvOrderedWeights; ///< 排序后的权重(从大到小)
// Spanning Tree and Loop Edges
// std::set是集合,相比vector,进行插入数据这样的操作时会自动排序
bool mbFirstConnection;
KeyFrame* mpParent;
std::set mspChildrens;
std::set mspLoopEdges;
// Bad flags
bool mbNotErase;
bool mbToBeErased;
bool mbBad;
float mHalfBaseline; // Only for visualization
Map* mpMap;
std::mutex mMutexPose;
std::mutex mMutexConnections;
std::mutex mMutexFeatures;
};
} //namespace ORB_SLAM
#endif // KEYFRAME_H
KeyFrame.cpp
/**
* This file is part of ORB-SLAM2.
*
* Copyright (C) 2014-2016 Raúl Mur-Artal (University of Zaragoza)
* For more information see
*
* ORB-SLAM2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ORB-SLAM2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ORB-SLAM2. If not, see .
*/
#include "KeyFrame.h"
#include "Converter.h"
#include "ORBmatcher.h"
#include
namespace ORB_SLAM2
{
long unsigned int KeyFrame::nNextId=0;
KeyFrame::KeyFrame(Frame &F, Map *pMap, KeyFrameDatabase *pKFDB):
mnFrameId(F.mnId), mTimeStamp(F.mTimeStamp), mnGridCols(FRAME_GRID_COLS), mnGridRows(FRAME_GRID_ROWS),
mfGridElementWidthInv(F.mfGridElementWidthInv), mfGridElementHeightInv(F.mfGridElementHeightInv),
mnTrackReferenceForFrame(0), mnFuseTargetForKF(0), mnBALocalForKF(0), mnBAFixedForKF(0),
mnLoopQuery(0), mnLoopWords(0), mnRelocQuery(0), mnRelocWords(0), mnBAGlobalForKF(0),
fx(F.fx), fy(F.fy), cx(F.cx), cy(F.cy), invfx(F.invfx), invfy(F.invfy),
mbf(F.mbf), mb(F.mb), mThDepth(F.mThDepth), N(F.N), mvKeys(F.mvKeys), mvKeysUn(F.mvKeysUn),
mvuRight(F.mvuRight), mvDepth(F.mvDepth), mDescriptors(F.mDescriptors.clone()),
mBowVec(F.mBowVec), mFeatVec(F.mFeatVec), mnScaleLevels(F.mnScaleLevels), mfScaleFactor(F.mfScaleFactor),
mfLogScaleFactor(F.mfLogScaleFactor), mvScaleFactors(F.mvScaleFactors), mvLevelSigma2(F.mvLevelSigma2),
mvInvLevelSigma2(F.mvInvLevelSigma2), mnMinX(F.mnMinX), mnMinY(F.mnMinY), mnMaxX(F.mnMaxX),
mnMaxY(F.mnMaxY), mK(F.mK), mvpMapPoints(F.mvpMapPoints), mpKeyFrameDB(pKFDB),
mpORBvocabulary(F.mpORBvocabulary), mbFirstConnection(true), mpParent(NULL), mbNotErase(false),
mbToBeErased(false), mbBad(false), mHalfBaseline(F.mb/2), mpMap(pMap)
{
mnId=nNextId++;
mGrid.resize(mnGridCols);
for(int i=0; i vCurrentDesc = Converter::toDescriptorVector(mDescriptors);
// Feature vector associate features with nodes in the 4th level (from leaves up)
// We assume the vocabulary tree has 6 levels, change the 4 otherwise
mpORBvocabulary->transform(vCurrentDesc,mBowVec,mFeatVec,4);
}
}
void KeyFrame::SetPose(const cv::Mat &Tcw_)
{
unique_lock lock(mMutexPose);
Tcw_.copyTo(Tcw);
cv::Mat Rcw = Tcw.rowRange(0,3).colRange(0,3);
cv::Mat tcw = Tcw.rowRange(0,3).col(3);
cv::Mat Rwc = Rcw.t();
Ow = -Rwc*tcw;
Twc = cv::Mat::eye(4,4,Tcw.type());
Rwc.copyTo(Twc.rowRange(0,3).colRange(0,3));
Ow.copyTo(Twc.rowRange(0,3).col(3));
// center为相机坐标系(左目)下,立体相机中心的坐标
// 立体相机中心点坐标与左目相机坐标之间只是在x轴上相差mHalfBaseline,
// 因此可以看出,立体相机中两个摄像头的连线为x轴,正方向为左目相机指向右目相机
cv::Mat center = (cv::Mat_(4,1) << mHalfBaseline, 0 , 0, 1);
// 世界坐标系下,左目相机中心到立体相机中心的向量,方向由左目相机指向立体相机中心
Cw = Twc*center;
}
cv::Mat KeyFrame::GetPose()
{
unique_lock lock(mMutexPose);
return Tcw.clone();
}
cv::Mat KeyFrame::GetPoseInverse()
{
unique_lock lock(mMutexPose);
return Twc.clone();
}
cv::Mat KeyFrame::GetCameraCenter()
{
unique_lock lock(mMutexPose);
return Ow.clone();
}
cv::Mat KeyFrame::GetStereoCenter()
{
unique_lock lock(mMutexPose);
return Cw.clone();
}
cv::Mat KeyFrame::GetRotation()
{
unique_lock lock(mMutexPose);
return Tcw.rowRange(0,3).colRange(0,3).clone();
}
cv::Mat KeyFrame::GetTranslation()
{
unique_lock lock(mMutexPose);
return Tcw.rowRange(0,3).col(3).clone();
}
/**
* @brief 为关键帧之间添加连接
*
* 更新了mConnectedKeyFrameWeights
* @param pKF 关键帧
* @param weight 权重,该关键帧与pKF共同观测到的3d点数量
*/
void KeyFrame::AddConnection(KeyFrame *pKF, const int &weight)
{
{
unique_lock lock(mMutexConnections);
// std::map::count函数只可能返回0或1两种情况
if(!mConnectedKeyFrameWeights.count(pKF)) // count函数返回0,mConnectedKeyFrameWeights中没有pKF,之前没有连接
mConnectedKeyFrameWeights[pKF]=weight;
else if(mConnectedKeyFrameWeights[pKF]!=weight) // 之前连接的权重不一样
mConnectedKeyFrameWeights[pKF]=weight;
else
return;
}
UpdateBestCovisibles();
}
/**
* @brief 按照权重对连接的关键帧进行排序
*
* 更新后的变量存储在mvpOrderedConnectedKeyFrames和mvOrderedWeights中
*/
void KeyFrame::UpdateBestCovisibles()
{
unique_lock lock(mMutexConnections);
// http://stackoverflow.com/questions/3389648/difference-between-stdliststdpair-and-stdmap-in-c-stl
vector > vPairs;
vPairs.reserve(mConnectedKeyFrameWeights.size());
// 取出所有连接的关键帧,mConnectedKeyFrameWeights的类型为std::map,而vPairs变量将共视的3D点数放在前面,利于排序
for(map::iterator mit=mConnectedKeyFrameWeights.begin(), mend=mConnectedKeyFrameWeights.end(); mit!=mend; mit++)
vPairs.push_back(make_pair(mit->second,mit->first));
// 按照权重进行排序
sort(vPairs.begin(),vPairs.end());
list lKFs; // keyframe
list lWs; // weight
for(size_t i=0, iend=vPairs.size(); i(lKFs.begin(),lKFs.end());
mvOrderedWeights = vector(lWs.begin(), lWs.end());
}
/**
* @brief 得到与该关键帧连接的关键帧
* @return 连接的关键帧
*/
set KeyFrame::GetConnectedKeyFrames()
{
unique_lock lock(mMutexConnections);
set s;
for(map::iterator mit=mConnectedKeyFrameWeights.begin();mit!=mConnectedKeyFrameWeights.end();mit++)
s.insert(mit->first);
return s;
}
/**
* @brief 得到与该关键帧连接的关键帧(已按权值排序)
* @return 连接的关键帧
*/
vector KeyFrame::GetVectorCovisibleKeyFrames()
{
unique_lock lock(mMutexConnections);
return mvpOrderedConnectedKeyFrames;
}
/**
* @brief 得到与该关键帧连接的前N个关键帧(已按权值排序)
*
* 如果连接的关键帧少于N,则返回所有连接的关键帧
* @param N 前N个
* @return 连接的关键帧
*/
vector KeyFrame::GetBestCovisibilityKeyFrames(const int &N)
{
unique_lock lock(mMutexConnections);
if((int)mvpOrderedConnectedKeyFrames.size()(mvpOrderedConnectedKeyFrames.begin(),mvpOrderedConnectedKeyFrames.begin()+N);
}
/**
* @brief 得到与该关键帧连接的权重大于等于w的关键帧
* @param w 权重
* @return 连接的关键帧
*/
vector KeyFrame::GetCovisiblesByWeight(const int &w)
{
unique_lock lock(mMutexConnections);
if(mvpOrderedConnectedKeyFrames.empty())
return vector();
// http://www.cplusplus.com/reference/algorithm/upper_bound/
// 从mvOrderedWeights找出第一个大于w的那个迭代器
// 这里应该使用lower_bound,因为lower_bound是返回小于等于,而upper_bound只能返回第一个大于的
vector::iterator it = upper_bound(mvOrderedWeights.begin(),mvOrderedWeights.end(),w,KeyFrame::weightComp);
if(it==mvOrderedWeights.end() && *mvOrderedWeights.rbegin()();
else
{
int n = it-mvOrderedWeights.begin();
return vector(mvpOrderedConnectedKeyFrames.begin(), mvpOrderedConnectedKeyFrames.begin()+n);
}
}
/**
* @brief 得到该关键帧与pKF的权重
* @param pKF 关键帧
* @return 权重
*/
int KeyFrame::GetWeight(KeyFrame *pKF)
{
unique_lock lock(mMutexConnections);
if(mConnectedKeyFrameWeights.count(pKF))
return mConnectedKeyFrameWeights[pKF];
else
return 0;
}
/**
* @brief Add MapPoint to KeyFrame
* @param pMP MapPoint
* @param idx MapPoint在KeyFrame中的索引
*/
void KeyFrame::AddMapPoint(MapPoint *pMP, const size_t &idx)
{
unique_lock lock(mMutexFeatures);
mvpMapPoints[idx]=pMP;
}
void KeyFrame::EraseMapPointMatch(const size_t &idx)
{
unique_lock lock(mMutexFeatures);
mvpMapPoints[idx]=static_cast(NULL);
}
void KeyFrame::EraseMapPointMatch(MapPoint* pMP)
{
int idx = pMP->GetIndexInKeyFrame(this);
if(idx>=0)
mvpMapPoints[idx]=static_cast(NULL);
}
void KeyFrame::ReplaceMapPointMatch(const size_t &idx, MapPoint* pMP)
{
mvpMapPoints[idx]=pMP;
}
set KeyFrame::GetMapPoints()
{
unique_lock lock(mMutexFeatures);
set s;
for(size_t i=0, iend=mvpMapPoints.size(); iisBad())
s.insert(pMP);
}
return s;
}
/**
* @brief 关键帧中,大于等于minObs的MapPoints的数量
* minObs就是一个阈值,大于minObs就表示该MapPoint是一个高质量的MapPoint
* 一个高质量的MapPoint会被多个KeyFrame观测到,
* @param minObs 最小观测
*/
int KeyFrame::TrackedMapPoints(const int &minObs)
{
unique_lock lock(mMutexFeatures);
int nPoints=0;
const bool bCheckObs = minObs>0;
for(int i=0; iisBad())
{
if(bCheckObs)
{
// 该MapPoint是一个高质量的MapPoint
if(mvpMapPoints[i]->Observations()>=minObs)
nPoints++;
}
else
nPoints++;
}
}
}
return nPoints;
}
/**
* @brief Get MapPoint Matches
*
* 获取该关键帧的MapPoints
*/
vector KeyFrame::GetMapPointMatches()
{
unique_lock lock(mMutexFeatures);
return mvpMapPoints;
}
MapPoint* KeyFrame::GetMapPoint(const size_t &idx)
{
unique_lock lock(mMutexFeatures);
return mvpMapPoints[idx];
}
/**
* @brief 更新图的连接
*
* 1. 首先获得该关键帧的所有MapPoint点,统计观测到这些3d点的每个关键与其它所有关键帧之间的共视程度
* 对每一个找到的关键帧,建立一条边,边的权重是该关键帧与当前关键帧公共3d点的个数。
* 2. 并且该权重必须大于一个阈值,如果没有超过该阈值的权重,那么就只保留权重最大的边(与其它关键帧的共视程度比较高)
* 3. 对这些连接按照权重从大到小进行排序,以方便将来的处理
* 更新完covisibility图之后,如果没有初始化过,则初始化为连接权重最大的边(与其它关键帧共视程度最高的那个关键帧),类似于最大生成树
*/
void KeyFrame::UpdateConnections()
{
// 在没有执行这个函数前,关键帧只和MapPoints之间有连接关系,这个函数可以更新关键帧之间的连接关系
//===============1==================================
map KFcounter; // 关键帧-权重,权重为其它关键帧与当前关键帧共视3d点的个数
vector vpMP;
{
// 获得该关键帧的所有3D点
unique_lock lockMPs(mMutexFeatures);
vpMP = mvpMapPoints;
}
//For all map points in keyframe check in which other keyframes are they seen
//Increase counter for those keyframes
// 通过3D点间接统计可以观测到这些3D点的所有关键帧之间的共视程度
// 即统计每一个关键帧都有多少关键帧与它存在共视关系,统计结果放在KFcounter
for(vector::iterator vit=vpMP.begin(), vend=vpMP.end(); vit!=vend; vit++)
{
MapPoint* pMP = *vit;
if(!pMP)
continue;
if(pMP->isBad())
continue;
// 对于每一个MapPoint点,observations记录了可以观测到该MapPoint的所有关键帧
map observations = pMP->GetObservations();
for(map::iterator mit=observations.begin(), mend=observations.end(); mit!=mend; mit++)
{
// 除去自身,自己与自己不算共视
if(mit->first->mnId==mnId)
continue;
KFcounter[mit->first]++;
}
}
// This should not happen
if(KFcounter.empty())
return;
//===============2==================================
// If the counter is greater than threshold add connection
// In case no keyframe counter is over threshold add the one with maximum counter
int nmax=0;
KeyFrame* pKFmax=NULL;
int th = 15;
// vPairs记录与其它关键帧共视帧数大于th的关键帧
// pair将关键帧的权重写在前面,关键帧写在后面方便后面排序
vector > vPairs;
vPairs.reserve(KFcounter.size());
for(map::iterator mit=KFcounter.begin(), mend=KFcounter.end(); mit!=mend; mit++)
{
if(mit->second>nmax)
{
nmax=mit->second;
// 找到对应权重最大的关键帧(共视程度最高的关键帧)
pKFmax=mit->first;
}
if(mit->second>=th)
{
// 对应权重需要大于阈值,对这些关键帧建立连接
vPairs.push_back(make_pair(mit->second,mit->first));
// 更新KFcounter中该关键帧的mConnectedKeyFrameWeights
// 更新其它KeyFrame的mConnectedKeyFrameWeights,更新其它关键帧与当前帧的连接权重
(mit->first)->AddConnection(this,mit->second);
}
}
// 如果没有超过阈值的权重,则对权重最大的关键帧建立连接
if(vPairs.empty())
{
// 如果每个关键帧与它共视的关键帧的个数都少于th,
// 那就只更新与其它关键帧共视程度最高的关键帧的mConnectedKeyFrameWeights
// 这是对之前th这个阈值可能过高的一个补丁
vPairs.push_back(make_pair(nmax,pKFmax));
pKFmax->AddConnection(this,nmax);
}
// vPairs里存的都是相互共视程度比较高的关键帧和共视权重,由大到小
sort(vPairs.begin(),vPairs.end());
list lKFs;
list lWs;
for(size_t i=0; i lockCon(mMutexConnections);
// mspConnectedKeyFrames = spConnectedKeyFrames;
// 更新图的连接(权重)
mConnectedKeyFrameWeights = KFcounter;//更新该KeyFrame的mConnectedKeyFrameWeights,更新当前帧与其它关键帧的连接权重
mvpOrderedConnectedKeyFrames = vector(lKFs.begin(),lKFs.end());
mvOrderedWeights = vector(lWs.begin(), lWs.end());
// 更新生成树的连接
if(mbFirstConnection && mnId!=0)
{
// 初始化该关键帧的父关键帧为共视程度最高的那个关键帧
mpParent = mvpOrderedConnectedKeyFrames.front();
// 建立双向连接关系
mpParent->AddChild(this);
mbFirstConnection = false;
}
}
}
void KeyFrame::AddChild(KeyFrame *pKF)
{
unique_lock lockCon(mMutexConnections);
mspChildrens.insert(pKF);
}
void KeyFrame::EraseChild(KeyFrame *pKF)
{
unique_lock lockCon(mMutexConnections);
mspChildrens.erase(pKF);
}
void KeyFrame::ChangeParent(KeyFrame *pKF)
{
unique_lock lockCon(mMutexConnections);
mpParent = pKF;
pKF->AddChild(this);
}
set KeyFrame::GetChilds()
{
unique_lock lockCon(mMutexConnections);
return mspChildrens;
}
KeyFrame* KeyFrame::GetParent()
{
unique_lock lockCon(mMutexConnections);
return mpParent;
}
bool KeyFrame::hasChild(KeyFrame *pKF)
{
unique_lock lockCon(mMutexConnections);
return mspChildrens.count(pKF);
}
void KeyFrame::AddLoopEdge(KeyFrame *pKF)
{
unique_lock lockCon(mMutexConnections);
mbNotErase = true;
mspLoopEdges.insert(pKF);
}
set KeyFrame::GetLoopEdges()
{
unique_lock lockCon(mMutexConnections);
return mspLoopEdges;
}
void KeyFrame::SetNotErase()
{
unique_lock lock(mMutexConnections);
mbNotErase = true;
}
void KeyFrame::SetErase()
{
{
unique_lock lock(mMutexConnections);
if(mspLoopEdges.empty())
{
mbNotErase = false;
}
}
// 这个地方是不是应该:(!mbToBeErased),(wubo???)
// SetBadFlag函数就是将mbToBeErased置为true,mbToBeErased就表示该KeyFrame被擦除了
if(mbToBeErased)
{
SetBadFlag();
}
}
void KeyFrame::SetBadFlag()
{
{
unique_lock lock(mMutexConnections);
if(mnId==0)
return;
else if(mbNotErase)// mbNotErase表示不应该擦除该KeyFrame,于是把mbToBeErased置为true,表示已经擦除了,其实没有擦除
{
mbToBeErased = true;
return;
}
}
for(map::iterator mit = mConnectedKeyFrameWeights.begin(), mend=mConnectedKeyFrameWeights.end(); mit!=mend; mit++)
mit->first->EraseConnection(this);// 让其它的KeyFrame删除与自己的联系
for(size_t i=0; iEraseObservation(this);// 让与自己有联系的MapPoint删除与自己的联系
{
unique_lock lock(mMutexConnections);
unique_lock lock1(mMutexFeatures);
//清空自己与其它关键帧之间的联系
mConnectedKeyFrameWeights.clear();
mvpOrderedConnectedKeyFrames.clear();
// Update Spanning Tree
set sParentCandidates;
sParentCandidates.insert(mpParent);
// Assign at each iteration one children with a parent (the pair with highest covisibility weight)
// Include that children as new parent candidate for the rest
// 如果这个关键帧有自己的孩子关键帧,告诉这些子关键帧,它们的父关键帧不行了,赶紧找新的父关键帧
while(!mspChildrens.empty())
{
bool bContinue = false;
int max = -1;
KeyFrame* pC;
KeyFrame* pP;
// 遍历每一个子关键帧,让它们更新它们指向的父关键帧
for(set::iterator sit=mspChildrens.begin(), send=mspChildrens.end(); sit!=send; sit++)
{
KeyFrame* pKF = *sit;
if(pKF->isBad())
continue;
// Check if a parent candidate is connected to the keyframe
// 子关键帧遍历每一个与它相连的关键帧(共视关键帧)
vector vpConnected = pKF->GetVectorCovisibleKeyFrames();
for(size_t i=0, iend=vpConnected.size(); i::iterator spcit=sParentCandidates.begin(), spcend=sParentCandidates.end(); spcit!=spcend; spcit++)
{
// 如果该帧的子节点和父节点(祖孙节点)之间存在连接关系(共视)
// 举例:B-->A(B的父节点是A) C-->B(C的父节点是B) D--C(D与C相连) E--C(E与C相连) F--C(F与C相连) D-->A(D的父节点是A) E-->A(E的父节点是A)
// 现在B挂了,于是C在与自己相连的D、E、F节点中找到父节点指向A的D
// 此过程就是为了找到可以替换B的那个节点。
// 上面例子中,B为当前要设置为SetBadFlag的关键帧
// A为spcit,也即sParentCandidates
// C为pKF,pC,也即mspChildrens中的一个
// D、E、F为vpConnected中的变量,由于C与D间的权重 比 C与E间的权重大,因此D为pP
if(vpConnected[i]->mnId == (*spcit)->mnId)
{
int w = pKF->GetWeight(vpConnected[i]);
if(w>max)
{
pC = pKF;
pP = vpConnected[i];
max = w;
bContinue = true;
}
}
}
}
}
if(bContinue)
{
// 因为父节点死了,并且子节点找到了新的父节点,子节点更新自己的父节点
pC->ChangeParent(pP);
// 因为子节点找到了新的父节点并更新了父节点,那么该子节点升级,作为其它子节点的备选父节点
sParentCandidates.insert(pC);
// 该子节点处理完毕
mspChildrens.erase(pC);
}
else
break;
}
// If a children has no covisibility links with any parent candidate, assign to the original parent of this KF
// 如果还有子节点没有找到新的父节点
if(!mspChildrens.empty())
for(set::iterator sit=mspChildrens.begin(); sit!=mspChildrens.end(); sit++)
{
// 直接把父节点的父节点作为自己的父节点
(*sit)->ChangeParent(mpParent);
}
mpParent->EraseChild(this);
mTcp = Tcw*mpParent->GetPoseInverse();
mbBad = true;
}
mpMap->EraseKeyFrame(this);
mpKeyFrameDB->erase(this);
}
bool KeyFrame::isBad()
{
unique_lock lock(mMutexConnections);
return mbBad;
}
void KeyFrame::EraseConnection(KeyFrame* pKF)
{
bool bUpdate = false;
{
unique_lock lock(mMutexConnections);
if(mConnectedKeyFrameWeights.count(pKF))
{
mConnectedKeyFrameWeights.erase(pKF);
bUpdate=true;
}
}
if(bUpdate)
UpdateBestCovisibles();
}
// r为边长(半径)
vector KeyFrame::GetFeaturesInArea(const float &x, const float &y, const float &r) const
{
vector vIndices;
vIndices.reserve(N);
// floor向下取整,mfGridElementWidthInv为每个像素占多少个格子
const int nMinCellX = max(0,(int)floor((x-mnMinX-r)*mfGridElementWidthInv));
if(nMinCellX>=mnGridCols)
return vIndices;
// ceil向上取整
const int nMaxCellX = min((int)mnGridCols-1,(int)ceil((x-mnMinX+r)*mfGridElementWidthInv));
if(nMaxCellX<0)
return vIndices;
const int nMinCellY = max(0,(int)floor((y-mnMinY-r)*mfGridElementHeightInv));
if(nMinCellY>=mnGridRows)
return vIndices;
const int nMaxCellY = min((int)mnGridRows-1,(int)ceil((y-mnMinY+r)*mfGridElementHeightInv));
if(nMaxCellY<0)
return vIndices;
for(int ix = nMinCellX; ix<=nMaxCellX; ix++)
{
for(int iy = nMinCellY; iy<=nMaxCellY; iy++)
{
const vector vCell = mGrid[ix][iy];
for(size_t j=0, jend=vCell.size(); j=mnMinX && x=mnMinY && y0)
{
// 由2维图像反投影到相机坐标系
// mvDepth是在ComputeStereoMatches函数中求取的
// mvDepth对应的校正前的特征点,因此这里对校正前特征点反投影
// 可在Frame::UnprojectStereo中却是对校正后的特征点mvKeysUn反投影
// 在ComputeStereoMatches函数中应该对校正后的特征点求深度?? (wubo???)
const float u = mvKeys[i].pt.x;
const float v = mvKeys[i].pt.y;
const float x = (u-cx)*z*invfx;
const float y = (v-cy)*z*invfy;
cv::Mat x3Dc = (cv::Mat_(3,1) << x, y, z);
unique_lock lock(mMutexPose);
// 由相机坐标系转换到世界坐标系
// Twc为相机坐标系到世界坐标系的变换矩阵
// Twc.rosRange(0,3).colRange(0,3)取Twc矩阵的前3行与前3列
return Twc.rowRange(0,3).colRange(0,3)*x3Dc+Twc.rowRange(0,3).col(3);
}
else
return cv::Mat();
}
/**
* @brief 评估当前关键帧场景深度,q=2表示中值
* @param q q=2
* @return Median Depth
*/
float KeyFrame::ComputeSceneMedianDepth(const int q)
{
vector vpMapPoints;
cv::Mat Tcw_;
{
unique_lock lock(mMutexFeatures);
unique_lock lock2(mMutexPose);
vpMapPoints = mvpMapPoints;
Tcw_ = Tcw.clone();
}
vector vDepths;
vDepths.reserve(N);
cv::Mat Rcw2 = Tcw_.row(2).colRange(0,3);
Rcw2 = Rcw2.t();
float zcw = Tcw_.at(2,3);
for(int i=0; iGetWorldPos();
float z = Rcw2.dot(x3Dw)+zcw; // (R*x3Dw+t)的第三行,即z
vDepths.push_back(z);
}
}
sort(vDepths.begin(),vDepths.end());
return vDepths[(vDepths.size()-1)/q];
}
} //namespace ORB_SLAM