ORBSLAM2
代码
void LocalMapping::KeyFrameCulling()
{
// Check redundant keyframes (only local keyframes)
// A keyframe is considered redundant if the 90% of the MapPoints it sees, are seen
// in at least other 3 keyframes (in the same or finer scale)
// We only consider close stereo points
vector<KeyFrame*> vpLocalKeyFrames = mpCurrentKeyFrame->GetVectorCovisibleKeyFrames();//取出当前帧中的共视关键帧,一级关键帧
for(vector<KeyFrame*>::iterator vit=vpLocalKeyFrames.begin(), vend=vpLocalKeyFrames.end(); vit!=vend; vit++)//开始迭代这些共视关键帧
{
KeyFrame* pKF = *vit;//取出这些关键帧
if(pKF->mnId==0)//共视关键帧中如果有第一帧关键帧,那么久continue
continue;
const vector<MapPoint*> vpMapPoints = pKF->GetMapPointMatches();//取出这些共视帧中的地图点
int nObs = 3;//被观测次数
const int thObs=nObs;
int nRedundantObservations=0;//记录冗余观测点的数目
int nMPs=0;
for(size_t i=0, iend=vpMapPoints.size(); i<iend; i++)//开始遍历这些地图点
{
MapPoint* pMP = vpMapPoints[i];//取出这些地图点
if(pMP)
{
if(!pMP->isBad())
{
if(!mbMonocular)//对于非单目
{
if(pKF->mvDepth[i]>pKF->mThDepth || pKF->mvDepth[i]<0)//对于深度太远或者深度小于零就continue
continue;
}
nMPs++;
if(pMP->Observations()>thObs)//地图点的观测是否大于3次?
{
const int &scaleLevel = pKF->mvKeysUn[i].octave;//取出观测地图点的特征点所在的层数
const map<KeyFrame*, size_t> observations = pMP->GetObservations();//获取地图点的观测帧
int nObs=0;
for(map<KeyFrame*, size_t>::const_iterator mit=observations.begin(), mend=observations.end(); mit!=mend; mit++)//遍历地图点的观测帧
{
KeyFrame* pKFi = mit->first;//取出观测帧
if(pKFi==pKF)//如过观测帧中的观测帧于自己是同一帧
continue;
const int &scaleLeveli = pKFi->mvKeysUn[mit->second].octave;//获取地图点的观测帧的特征点所在的层数
if(scaleLeveli<=scaleLevel+1)//如果地图点的观测帧(二级??)的特征点小于观测帧(一级??)+1的尺度
{
nObs++;//观测+1
if(nObs>=thObs)//找到了观测次大于3,那就满足,break
break;
}
}
if(nObs>=thObs)
{
nRedundantObservations++;//冗余帧数就+1
}
}
}
}
}
if(nRedundantObservations>0.9*nMPs)//判断该关键帧的地图点是都大于0.9*有效地图点。这个关键帧就是冗余的
pKF->SetBadFlag();//当前帧的共视关键帧是否要删除
}
}