orb_slam3_details/src/KeyFrameDatabase.cc

951 lines
37 KiB
C++
Raw Normal View History

2020-12-01 11:58:17 +08:00
/**
2022-03-28 21:20:28 +08:00
* This file is part of ORB-SLAM3
*
* Copyright (C) 2017-2021 Carlos Campos, Richard Elvira, Juan J. Gómez Rodríguez, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
* Copyright (C) 2014-2016 Raúl Mur-Artal, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
*
* ORB-SLAM3 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-SLAM3 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-SLAM3.
* If not, see <http://www.gnu.org/licenses/>.
*/
2020-12-01 11:58:17 +08:00
#include "KeyFrameDatabase.h"
#include "KeyFrame.h"
#include "Thirdparty/DBoW2/DBoW2/BowVector.h"
2022-03-28 21:20:28 +08:00
#include <mutex>
2020-12-01 11:58:17 +08:00
using namespace std;
namespace ORB_SLAM3
{
// 构造函数
2022-03-28 21:20:28 +08:00
KeyFrameDatabase::KeyFrameDatabase(const ORBVocabulary &voc) : mpVoc(&voc)
2020-12-01 11:58:17 +08:00
{
mvInvertedFile.resize(voc.size());
}
// 根据关键帧的BoW更新数据库的倒排索引
void KeyFrameDatabase::add(KeyFrame *pKF)
{
unique_lock<mutex> lock(mMutex);
// 为每一个word添加该KeyFrame
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = pKF->mBowVec.begin(), vend = pKF->mBowVec.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
mvInvertedFile[vit->first].push_back(pKF);
}
// 关键帧被删除后,更新数据库的倒排索引
2022-03-28 21:20:28 +08:00
void KeyFrameDatabase::erase(KeyFrame *pKF)
2020-12-01 11:58:17 +08:00
{
unique_lock<mutex> lock(mMutex);
// Erase elements in the Inverse File for the entry
// 每一个KeyFrame包含多个words遍历mvInvertedFile中的这些words然后在word中删除该KeyFrame
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = pKF->mBowVec.begin(), vend = pKF->mBowVec.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
// List of keyframes that share the word
2022-03-28 21:20:28 +08:00
list<KeyFrame *> &lKFs = mvInvertedFile[vit->first];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (pKF == *lit)
2020-12-01 11:58:17 +08:00
{
lKFs.erase(lit);
break;
}
}
}
}
// 清空关键帧数据库
void KeyFrameDatabase::clear()
{
mvInvertedFile.clear();
mvInvertedFile.resize(mpVoc->size());
}
2022-03-28 21:20:28 +08:00
void KeyFrameDatabase::clearMap(Map *pMap)
2020-12-01 11:58:17 +08:00
{
unique_lock<mutex> lock(mMutex);
// Erase elements in the Inverse File for the entry
2022-03-28 21:20:28 +08:00
for (std::vector<list<KeyFrame *>>::iterator vit = mvInvertedFile.begin(), vend = mvInvertedFile.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
// List of keyframes that share the word
2022-03-28 21:20:28 +08:00
list<KeyFrame *> &lKFs = *vit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend;)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
if (pMap == pKFi->GetMap())
2020-12-01 11:58:17 +08:00
{
lit = lKFs.erase(lit);
// Dont delete the KF because the class Map clean all the KF when it is destroyed
}
else
{
++lit;
}
}
}
}
/**
* @brief
* Step 1
2022-03-28 21:20:28 +08:00
* Step 280%
2020-12-01 11:58:17 +08:00
* Step 375%
* Step 4
* @param[in] pKF
* @param[in] minScore BoWminScore
* @return vector<KeyFrame*>
*/
2022-03-28 21:20:28 +08:00
vector<KeyFrame *> KeyFrameDatabase::DetectLoopCandidates(KeyFrame *pKF, float minScore)
2020-12-01 11:58:17 +08:00
{
// 取出与当前关键帧相连(>15个共视地图点的所有关键帧这些相连关键帧都是局部相连在闭环检测的时候将被剔除
// 相连关键帧定义见 KeyFrame::UpdateConnections()
2022-03-28 21:20:28 +08:00
set<KeyFrame *> spConnectedKeyFrames = pKF->GetConnectedKeyFrames();
2020-12-01 11:58:17 +08:00
// 用于保存可能与当前关键帧形成闭环的候选帧只要有相同的word且不属于局部相连共视
2022-03-28 21:20:28 +08:00
list<KeyFrame *> lKFsSharingWords;
2020-12-01 11:58:17 +08:00
// Search all keyframes that share a word with current keyframes
// Discard keyframes connected to the query keyframe
// Step 1找出和当前帧具有公共单词的所有关键帧不包括与当前帧连接也就是共视的关键帧
{
unique_lock<mutex> lock(mMutex);
// words是检测图像是否匹配的枢纽遍历该pKF的每一个word
// mBowVec 内部实际存储的是std::map<WordId, WordValue>
// WordId 和 WordValue 表示Word在叶子中的id 和权重
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = pKF->mBowVec.begin(), vend = pKF->mBowVec.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
// 提取所有包含该word的KeyFrame
2022-03-28 21:20:28 +08:00
list<KeyFrame *> &lKFs = mvInvertedFile[vit->first];
2020-12-01 11:58:17 +08:00
// 然后对这些关键帧展开遍历
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
if (pKFi->GetMap() == pKF->GetMap()) // For consider a loop candidate it a candidate it must be in the same map
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (pKFi->mnLoopQuery != pKF->mnId)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
// 还没有标记为pKF的闭环候选帧
pKFi->mnLoopWords = 0;
// 和当前关键帧共视的话不作为闭环候选帧
if (!spConnectedKeyFrames.count(pKFi))
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
// 没有共视就标记作为闭环候选关键帧放到lKFsSharingWords里
pKFi->mnLoopQuery = pKF->mnId;
2020-12-01 11:58:17 +08:00
lKFsSharingWords.push_back(pKFi);
}
}
2022-03-28 21:20:28 +08:00
pKFi->mnLoopWords++; // 记录pKFi与pKF具有相同word的个数
2020-12-01 11:58:17 +08:00
}
}
}
}
// 如果没有关键帧和这个关键帧具有相同的单词,那么就返回空
2022-03-28 21:20:28 +08:00
if (lKFsSharingWords.empty())
return vector<KeyFrame *>();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lScoreAndMatch;
2020-12-01 11:58:17 +08:00
// Only compare against those keyframes that share enough words
2022-03-28 21:20:28 +08:00
// Step 2统计上述所有闭环候选帧中与当前帧具有共同单词最多的单词数用来决定相对阈值
int maxCommonWords = 0;
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if ((*lit)->mnLoopWords > maxCommonWords)
maxCommonWords = (*lit)->mnLoopWords;
2020-12-01 11:58:17 +08:00
}
// 确定最小公共单词数为最大公共单词数目的0.8倍
2022-03-28 21:20:28 +08:00
int minCommonWords = maxCommonWords * 0.8f;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nscores = 0;
2020-12-01 11:58:17 +08:00
// Compute similarity score. Retain the matches whose score is higher than minScore
// Step 3遍历上述所有闭环候选帧挑选出共有单词数大于minCommonWords且单词匹配度大于minScore存入lScoreAndMatch
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
2020-12-01 11:58:17 +08:00
// pKF只和具有共同单词较多大于minCommonWords的关键帧进行比较
2022-03-28 21:20:28 +08:00
if (pKFi->mnLoopWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
nscores++;
// 用mBowVec来计算两者的相似度得分
2022-03-28 21:20:28 +08:00
float si = mpVoc->score(pKF->mBowVec, pKFi->mBowVec);
2020-12-01 11:58:17 +08:00
pKFi->mLoopScore = si;
2022-03-28 21:20:28 +08:00
if (si >= minScore)
lScoreAndMatch.push_back(make_pair(si, pKFi));
2020-12-01 11:58:17 +08:00
}
}
// 如果没有超过指定相似度阈值的,那么也就直接跳过去
2022-03-28 21:20:28 +08:00
if (lScoreAndMatch.empty())
return vector<KeyFrame *>();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lAccScoreAndMatch;
2020-12-01 11:58:17 +08:00
float bestAccScore = minScore;
// Lets now accumulate score by covisibility
// 单单计算当前帧和某一关键帧的相似性是不够的,这里将与关键帧相连(权值最高,共视程度最高)的前十个关键帧归为一组,计算累计得分
// Step 4计算上述候选帧对应的共视关键帧组的总得分得到最高组得分bestAccScore并以此决定阈值minScoreToRetain
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lScoreAndMatch.begin(), itend = lScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
vector<KeyFrame *> vpNeighs = pKFi->GetBestCovisibilityKeyFrames(10);
2020-12-01 11:58:17 +08:00
float bestScore = it->first; // 该组最高分数
float accScore = it->first; // 该组累计得分
2022-03-28 21:20:28 +08:00
KeyFrame *pBestKF = pKFi; // 该组最高分数对应的关键帧
for (vector<KeyFrame *>::iterator vit = vpNeighs.begin(), vend = vpNeighs.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKF2 = *vit;
2020-12-01 11:58:17 +08:00
// 只有pKF2也在闭环候选帧中且公共单词数超过最小要求才能贡献分数
2022-03-28 21:20:28 +08:00
if (pKF2->mnLoopQuery == pKF->mnId && pKF2->mnLoopWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
accScore += pKF2->mLoopScore;
2020-12-01 11:58:17 +08:00
// 统计得到组里分数最高的关键帧
2022-03-28 21:20:28 +08:00
if (pKF2->mLoopScore > bestScore)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pBestKF = pKF2;
2020-12-01 11:58:17 +08:00
bestScore = pKF2->mLoopScore;
}
}
}
2022-03-28 21:20:28 +08:00
lAccScoreAndMatch.push_back(make_pair(accScore, pBestKF));
2020-12-01 11:58:17 +08:00
// 记录所有组中组得分最高的组,用于确定相对阈值
2022-03-28 21:20:28 +08:00
if (accScore > bestAccScore)
bestAccScore = accScore;
2020-12-01 11:58:17 +08:00
}
// Return all those keyframes with a score higher than 0.75*bestScore
// 所有组中最高得分的0.75倍,作为最低阈值
2022-03-28 21:20:28 +08:00
float minScoreToRetain = 0.75f * bestAccScore;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
set<KeyFrame *> spAlreadyAddedKF;
vector<KeyFrame *> vpLoopCandidates;
2020-12-01 11:58:17 +08:00
vpLoopCandidates.reserve(lAccScoreAndMatch.size());
// Step 5只取组得分大于阈值的组得到组中分数最高的关键帧作为闭环候选关键帧
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lAccScoreAndMatch.begin(), itend = lAccScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (it->first > minScoreToRetain)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
2020-12-01 11:58:17 +08:00
// spAlreadyAddedKF 是为了防止重复添加
2022-03-28 21:20:28 +08:00
if (!spAlreadyAddedKF.count(pKFi))
2020-12-01 11:58:17 +08:00
{
vpLoopCandidates.push_back(pKFi);
spAlreadyAddedKF.insert(pKFi);
}
}
}
return vpLoopCandidates;
}
2022-03-28 21:20:28 +08:00
void KeyFrameDatabase::DetectCandidates(KeyFrame *pKF, float minScore, vector<KeyFrame *> &vpLoopCand, vector<KeyFrame *> &vpMergeCand)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
set<KeyFrame *> spConnectedKeyFrames = pKF->GetConnectedKeyFrames();
list<KeyFrame *> lKFsSharingWordsLoop, lKFsSharingWordsMerge;
2020-12-01 11:58:17 +08:00
// Search all keyframes that share a word with current keyframes
// Discard keyframes connected to the query keyframe
{
unique_lock<mutex> lock(mMutex);
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = pKF->mBowVec.begin(), vend = pKF->mBowVec.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<KeyFrame *> &lKFs = mvInvertedFile[vit->first];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
if (pKFi->GetMap() == pKF->GetMap()) // For consider a loop candidate it a candidate it must be in the same map
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (pKFi->mnLoopQuery != pKF->mnId)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pKFi->mnLoopWords = 0;
if (!spConnectedKeyFrames.count(pKFi))
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pKFi->mnLoopQuery = pKF->mnId;
2020-12-01 11:58:17 +08:00
lKFsSharingWordsLoop.push_back(pKFi);
}
}
pKFi->mnLoopWords++;
}
2022-03-28 21:20:28 +08:00
else if (!pKFi->GetMap()->IsBad())
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (pKFi->mnMergeQuery != pKF->mnId)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pKFi->mnMergeWords = 0;
if (!spConnectedKeyFrames.count(pKFi))
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pKFi->mnMergeQuery = pKF->mnId;
2020-12-01 11:58:17 +08:00
lKFsSharingWordsMerge.push_back(pKFi);
}
}
pKFi->mnMergeWords++;
}
}
}
}
2022-03-28 21:20:28 +08:00
if (lKFsSharingWordsLoop.empty() && lKFsSharingWordsMerge.empty())
2020-12-01 11:58:17 +08:00
return;
2022-03-28 21:20:28 +08:00
if (!lKFsSharingWordsLoop.empty())
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lScoreAndMatch;
2020-12-01 11:58:17 +08:00
// Only compare against those keyframes that share enough words
2022-03-28 21:20:28 +08:00
int maxCommonWords = 0;
for (list<KeyFrame *>::iterator lit = lKFsSharingWordsLoop.begin(), lend = lKFsSharingWordsLoop.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if ((*lit)->mnLoopWords > maxCommonWords)
maxCommonWords = (*lit)->mnLoopWords;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
int minCommonWords = maxCommonWords * 0.8f;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nscores = 0;
2020-12-01 11:58:17 +08:00
// Compute similarity score. Retain the matches whose score is higher than minScore
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFsSharingWordsLoop.begin(), lend = lKFsSharingWordsLoop.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if (pKFi->mnLoopWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
nscores++;
2022-03-28 21:20:28 +08:00
float si = mpVoc->score(pKF->mBowVec, pKFi->mBowVec);
2020-12-01 11:58:17 +08:00
pKFi->mLoopScore = si;
2022-03-28 21:20:28 +08:00
if (si >= minScore)
lScoreAndMatch.push_back(make_pair(si, pKFi));
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
if (!lScoreAndMatch.empty())
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lAccScoreAndMatch;
2020-12-01 11:58:17 +08:00
float bestAccScore = minScore;
// Lets now accumulate score by covisibility
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lScoreAndMatch.begin(), itend = lScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
vector<KeyFrame *> vpNeighs = pKFi->GetBestCovisibilityKeyFrames(10);
2020-12-01 11:58:17 +08:00
float bestScore = it->first;
float accScore = it->first;
2022-03-28 21:20:28 +08:00
KeyFrame *pBestKF = pKFi;
for (vector<KeyFrame *>::iterator vit = vpNeighs.begin(), vend = vpNeighs.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKF2 = *vit;
if (pKF2->mnLoopQuery == pKF->mnId && pKF2->mnLoopWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
accScore += pKF2->mLoopScore;
if (pKF2->mLoopScore > bestScore)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pBestKF = pKF2;
2020-12-01 11:58:17 +08:00
bestScore = pKF2->mLoopScore;
}
}
}
2022-03-28 21:20:28 +08:00
lAccScoreAndMatch.push_back(make_pair(accScore, pBestKF));
if (accScore > bestAccScore)
bestAccScore = accScore;
2020-12-01 11:58:17 +08:00
}
// Return all those keyframes with a score higher than 0.75*bestScore
2022-03-28 21:20:28 +08:00
float minScoreToRetain = 0.75f * bestAccScore;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
set<KeyFrame *> spAlreadyAddedKF;
2020-12-01 11:58:17 +08:00
vpLoopCand.reserve(lAccScoreAndMatch.size());
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lAccScoreAndMatch.begin(), itend = lAccScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (it->first > minScoreToRetain)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
if (!spAlreadyAddedKF.count(pKFi))
2020-12-01 11:58:17 +08:00
{
vpLoopCand.push_back(pKFi);
spAlreadyAddedKF.insert(pKFi);
}
}
}
}
}
2022-03-28 21:20:28 +08:00
if (!lKFsSharingWordsMerge.empty())
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
// cout << "BoW candidates: " << lKFsSharingWordsMerge.size() << endl;
list<pair<float, KeyFrame *>> lScoreAndMatch;
2020-12-01 11:58:17 +08:00
// Only compare against those keyframes that share enough words
2022-03-28 21:20:28 +08:00
int maxCommonWords = 0;
for (list<KeyFrame *>::iterator lit = lKFsSharingWordsMerge.begin(), lend = lKFsSharingWordsMerge.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if ((*lit)->mnMergeWords > maxCommonWords)
maxCommonWords = (*lit)->mnMergeWords;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
// cout << "Max common words: " << maxCommonWords << endl;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int minCommonWords = maxCommonWords * 0.8f;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nscores = 0;
2020-12-01 11:58:17 +08:00
// Compute similarity score. Retain the matches whose score is higher than minScore
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFsSharingWordsMerge.begin(), lend = lKFsSharingWordsMerge.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if (pKFi->mnMergeWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
nscores++;
2022-03-28 21:20:28 +08:00
float si = mpVoc->score(pKF->mBowVec, pKFi->mBowVec);
// cout << "KF score: " << si << endl;
2020-12-01 11:58:17 +08:00
pKFi->mMergeScore = si;
2022-03-28 21:20:28 +08:00
if (si >= minScore)
lScoreAndMatch.push_back(make_pair(si, pKFi));
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
// cout << "BoW candidates2: " << lScoreAndMatch.size() << endl;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if (!lScoreAndMatch.empty())
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lAccScoreAndMatch;
2020-12-01 11:58:17 +08:00
float bestAccScore = minScore;
// Lets now accumulate score by covisibility
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lScoreAndMatch.begin(), itend = lScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
vector<KeyFrame *> vpNeighs = pKFi->GetBestCovisibilityKeyFrames(10);
2020-12-01 11:58:17 +08:00
float bestScore = it->first;
float accScore = it->first;
2022-03-28 21:20:28 +08:00
KeyFrame *pBestKF = pKFi;
for (vector<KeyFrame *>::iterator vit = vpNeighs.begin(), vend = vpNeighs.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKF2 = *vit;
if (pKF2->mnMergeQuery == pKF->mnId && pKF2->mnMergeWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
accScore += pKF2->mMergeScore;
if (pKF2->mMergeScore > bestScore)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pBestKF = pKF2;
2020-12-01 11:58:17 +08:00
bestScore = pKF2->mMergeScore;
}
}
}
2022-03-28 21:20:28 +08:00
lAccScoreAndMatch.push_back(make_pair(accScore, pBestKF));
if (accScore > bestAccScore)
bestAccScore = accScore;
2020-12-01 11:58:17 +08:00
}
// Return all those keyframes with a score higher than 0.75*bestScore
2022-03-28 21:20:28 +08:00
float minScoreToRetain = 0.75f * bestAccScore;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// cout << "Min score to retain: " << minScoreToRetain << endl;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
set<KeyFrame *> spAlreadyAddedKF;
2020-12-01 11:58:17 +08:00
vpMergeCand.reserve(lAccScoreAndMatch.size());
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lAccScoreAndMatch.begin(), itend = lAccScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (it->first > minScoreToRetain)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
if (!spAlreadyAddedKF.count(pKFi))
2020-12-01 11:58:17 +08:00
{
vpMergeCand.push_back(pKFi);
spAlreadyAddedKF.insert(pKFi);
}
}
}
2022-03-28 21:20:28 +08:00
// cout << "Candidates: " << vpMergeCand.size() << endl;
2020-12-01 11:58:17 +08:00
}
}
//----
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = pKF->mBowVec.begin(), vend = pKF->mBowVec.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<KeyFrame *> &lKFs = mvInvertedFile[vit->first];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
pKFi->mnLoopQuery = -1;
pKFi->mnMergeQuery = -1;
2020-12-01 11:58:17 +08:00
}
}
}
2022-03-28 21:20:28 +08:00
void KeyFrameDatabase::DetectBestCandidates(KeyFrame *pKF, vector<KeyFrame *> &vpLoopCand, vector<KeyFrame *> &vpMergeCand, int nMinWords)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<KeyFrame *> lKFsSharingWords;
set<KeyFrame *> spConnectedKF;
2020-12-01 11:58:17 +08:00
// Search all keyframes that share a word with current frame
{
unique_lock<mutex> lock(mMutex);
spConnectedKF = pKF->GetConnectedKeyFrames();
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = pKF->mBowVec.begin(), vend = pKF->mBowVec.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<KeyFrame *> &lKFs = mvInvertedFile[vit->first];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
if (spConnectedKF.find(pKFi) != spConnectedKF.end())
2020-12-01 11:58:17 +08:00
{
continue;
}
2022-03-28 21:20:28 +08:00
if (pKFi->mnPlaceRecognitionQuery != pKF->mnId)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pKFi->mnPlaceRecognitionWords = 0;
pKFi->mnPlaceRecognitionQuery = pKF->mnId;
2020-12-01 11:58:17 +08:00
lKFsSharingWords.push_back(pKFi);
}
2022-03-28 21:20:28 +08:00
pKFi->mnPlaceRecognitionWords++;
2020-12-01 11:58:17 +08:00
}
}
}
2022-03-28 21:20:28 +08:00
if (lKFsSharingWords.empty())
2020-12-01 11:58:17 +08:00
return;
// Only compare against those keyframes that share enough words
2022-03-28 21:20:28 +08:00
int maxCommonWords = 0;
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if ((*lit)->mnPlaceRecognitionWords > maxCommonWords)
maxCommonWords = (*lit)->mnPlaceRecognitionWords;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
int minCommonWords = maxCommonWords * 0.8f;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if (minCommonWords < nMinWords)
2020-12-01 11:58:17 +08:00
{
minCommonWords = nMinWords;
}
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lScoreAndMatch;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nscores = 0;
2020-12-01 11:58:17 +08:00
// Compute similarity score.
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if (pKFi->mnPlaceRecognitionWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
nscores++;
2022-03-28 21:20:28 +08:00
float si = mpVoc->score(pKF->mBowVec, pKFi->mBowVec);
pKFi->mPlaceRecognitionScore = si;
lScoreAndMatch.push_back(make_pair(si, pKFi));
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
if (lScoreAndMatch.empty())
2020-12-01 11:58:17 +08:00
return;
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lAccScoreAndMatch;
2020-12-01 11:58:17 +08:00
float bestAccScore = 0;
// Lets now accumulate score by covisibility
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lScoreAndMatch.begin(), itend = lScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
vector<KeyFrame *> vpNeighs = pKFi->GetBestCovisibilityKeyFrames(10);
2020-12-01 11:58:17 +08:00
float bestScore = it->first;
float accScore = bestScore;
2022-03-28 21:20:28 +08:00
KeyFrame *pBestKF = pKFi;
for (vector<KeyFrame *>::iterator vit = vpNeighs.begin(), vend = vpNeighs.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKF2 = *vit;
if (pKF2->mnPlaceRecognitionQuery != pKF->mnId)
2020-12-01 11:58:17 +08:00
continue;
2022-03-28 21:20:28 +08:00
accScore += pKF2->mPlaceRecognitionScore;
if (pKF2->mPlaceRecognitionScore > bestScore)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pBestKF = pKF2;
2020-12-01 11:58:17 +08:00
bestScore = pKF2->mPlaceRecognitionScore;
}
}
2022-03-28 21:20:28 +08:00
lAccScoreAndMatch.push_back(make_pair(accScore, pBestKF));
if (accScore > bestAccScore)
bestAccScore = accScore;
2020-12-01 11:58:17 +08:00
}
// Return all those keyframes with a score higher than 0.75*bestScore
2022-03-28 21:20:28 +08:00
float minScoreToRetain = 0.75f * bestAccScore;
set<KeyFrame *> spAlreadyAddedKF;
2020-12-01 11:58:17 +08:00
vpLoopCand.reserve(lAccScoreAndMatch.size());
vpMergeCand.reserve(lAccScoreAndMatch.size());
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lAccScoreAndMatch.begin(), itend = lAccScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
const float &si = it->first;
2022-03-28 21:20:28 +08:00
if (si > minScoreToRetain)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
if (!spAlreadyAddedKF.count(pKFi))
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if (pKF->GetMap() == pKFi->GetMap())
2020-12-01 11:58:17 +08:00
{
vpLoopCand.push_back(pKFi);
}
else
{
vpMergeCand.push_back(pKFi);
}
spAlreadyAddedKF.insert(pKFi);
}
}
}
}
2022-03-28 21:20:28 +08:00
bool compFirst(const pair<float, KeyFrame *> &a, const pair<float, KeyFrame *> &b)
2020-12-01 11:58:17 +08:00
{
return a.first > b.first;
}
2021-09-03 14:24:01 +08:00
/**
* @brief NN
2022-03-28 21:20:28 +08:00
*
2021-09-03 14:24:01 +08:00
* @param[in] pKF ()
* @param[out] vpLoopCand
* @param[out] vpMergeCand
* @param[in] nNumCandidates ,
*/
2022-03-28 21:20:28 +08:00
void KeyFrameDatabase::DetectNBestCandidates(KeyFrame *pKF, vector<KeyFrame *> &vpLoopCand, vector<KeyFrame *> &vpMergeCand, int nNumCandidates)
2020-12-01 11:58:17 +08:00
{
2021-09-03 14:24:01 +08:00
// Step 1统计与当前关键帧有相同单词的关键帧
2022-03-28 21:20:28 +08:00
list<KeyFrame *> lKFsSharingWords;
// set<KeyFrame*> spInsertedKFsSharing;
// 当前关键帧的共视关键帧(避免将当前关键帧的共视关键帧加入回环检测)
set<KeyFrame *> spConnectedKF;
2020-12-01 11:58:17 +08:00
// Search all keyframes that share a word with current frame
{
unique_lock<mutex> lock(mMutex);
2021-09-03 14:24:01 +08:00
// 拿到当前关键帧的共视关键帧
2020-12-01 11:58:17 +08:00
spConnectedKF = pKF->GetConnectedKeyFrames();
2021-09-03 14:24:01 +08:00
// 遍历当前关键帧bow向量的每个单词
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = pKF->mBowVec.begin(), vend = pKF->mBowVec.end(); vit != vend; vit++)
{ // 拿到当前单词的逆向索引(所有有当前单词的关键帧)
list<KeyFrame *> &lKFs = mvInvertedFile[vit->first];
2021-09-03 14:24:01 +08:00
// 遍历每个有该单词的关键帧
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
2020-12-01 11:58:17 +08:00
/*if(spConnectedKF.find(pKFi) != spConnectedKF.end())
{
continue;
}*/
2021-09-03 14:24:01 +08:00
// 如果此关键帧没有被当前关键帧访问过(防止重复添加)
2022-03-28 21:20:28 +08:00
if (pKFi->mnPlaceRecognitionQuery != pKF->mnId)
{
2021-09-03 14:24:01 +08:00
// 初始化公共单词数为0
2022-03-28 21:20:28 +08:00
pKFi->mnPlaceRecognitionWords = 0;
2021-09-03 14:24:01 +08:00
// 如果该关键帧不是当前关键帧的共视关键帧
2022-03-28 21:20:28 +08:00
if (!spConnectedKF.count(pKFi))
2020-12-01 11:58:17 +08:00
{
2021-11-26 16:09:32 +08:00
// 标记该关键帧被当前关键帧访问到(也就是有公共单词)
2022-03-28 21:20:28 +08:00
pKFi->mnPlaceRecognitionQuery = pKF->mnId;
2021-09-03 14:24:01 +08:00
// 把当前关键帧添加到有公共单词的关键帧列表中
2020-12-01 11:58:17 +08:00
lKFsSharingWords.push_back(pKFi);
}
}
2021-09-03 14:24:01 +08:00
// 递增该关键帧与当前关键帧的公共单词数
2020-12-01 11:58:17 +08:00
pKFi->mnPlaceRecognitionWords++;
/*if(spInsertedKFsSharing.find(pKFi) == spInsertedKFsSharing.end())
{
lKFsSharingWords.push_back(pKFi);
spInsertedKFsSharing.insert(pKFi);
}*/
}
}
}
2021-09-03 14:24:01 +08:00
// 如果没有有公共单词的关键帧,直接返回
2022-03-28 21:20:28 +08:00
if (lKFsSharingWords.empty())
2020-12-01 11:58:17 +08:00
return;
// Only compare against those keyframes that share enough words
2021-11-26 16:09:32 +08:00
// Step 2 统计所有候选帧中与当前关键帧的公共单词数最多的单词数maxCommonWords,并筛选
2022-03-28 21:20:28 +08:00
int maxCommonWords = 0;
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if ((*lit)->mnPlaceRecognitionWords > maxCommonWords)
maxCommonWords = (*lit)->mnPlaceRecognitionWords;
2020-12-01 11:58:17 +08:00
}
2021-09-03 14:24:01 +08:00
// 取0.8倍为阀值
2022-03-28 21:20:28 +08:00
int minCommonWords = maxCommonWords * 0.8f;
2021-09-03 14:24:01 +08:00
// 这里的pair是 <相似度,候选帧的指针> : 记录所有大于minCommonWords的候选帧与当前关键帧的相似度
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lScoreAndMatch;
2021-09-03 14:24:01 +08:00
// 只是个统计变量,貌似没有用到
2022-03-28 21:20:28 +08:00
int nscores = 0;
2020-12-01 11:58:17 +08:00
// Compute similarity score.
2021-09-03 14:24:01 +08:00
// 对所有大于minCommonWords的候选帧计算相似度
// 遍历所有有公共单词的候选帧
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
2021-09-03 14:24:01 +08:00
// 如果当前帧的公共单词数大于minCommonWords
2022-03-28 21:20:28 +08:00
if (pKFi->mnPlaceRecognitionWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
nscores++; //未使用
2021-09-03 14:24:01 +08:00
// 计算相似度
2022-03-28 21:20:28 +08:00
float si = mpVoc->score(pKF->mBowVec, pKFi->mBowVec);
2021-09-03 14:24:01 +08:00
// 记录该候选帧与当前帧的相似度
2022-03-28 21:20:28 +08:00
pKFi->mPlaceRecognitionScore = si;
2021-09-03 14:24:01 +08:00
// 记录到容器里, 每个元素是<相似度,候选帧的指针>
2022-03-28 21:20:28 +08:00
lScoreAndMatch.push_back(make_pair(si, pKFi));
2020-12-01 11:58:17 +08:00
}
}
2021-09-03 14:24:01 +08:00
// 如果为空,直接返回,表示没有符合上述条件的关键帧
2022-03-28 21:20:28 +08:00
if (lScoreAndMatch.empty())
2020-12-01 11:58:17 +08:00
return;
2021-09-03 14:24:01 +08:00
// Step 3 : 用小组得分排序得到top3总分里最高分的关键帧,作为候选帧
// 统计以组为单位的累计相似度和组内相似度最高的关键帧, 每个pair为<小组总相似度,组内相似度最高的关键帧指针>
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lAccScoreAndMatch;
2020-12-01 11:58:17 +08:00
float bestAccScore = 0;
// Lets now accumulate score by covisibility
2021-09-03 14:24:01 +08:00
// 变量所有被lScoreAndMatch记录的pair <相似度,候选关键帧>
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lScoreAndMatch.begin(), itend = lScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2021-09-03 14:24:01 +08:00
// 候选关键帧
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
2021-09-03 14:24:01 +08:00
// 与候选关键帧共视关系最好的10个关键帧
2022-03-28 21:20:28 +08:00
vector<KeyFrame *> vpNeighs = pKFi->GetBestCovisibilityKeyFrames(10);
2021-09-03 14:24:01 +08:00
// 初始化最大相似度为该候选关键帧自己的相似度
2020-12-01 11:58:17 +08:00
float bestScore = it->first;
2021-09-03 14:24:01 +08:00
// 初始化小组累计得分为改候选关键帧自己的相似度
2020-12-01 11:58:17 +08:00
float accScore = bestScore;
2021-09-03 14:24:01 +08:00
// 初始化组内相似度最高的帧为该候选关键帧本身
2022-03-28 21:20:28 +08:00
KeyFrame *pBestKF = pKFi;
2021-09-03 14:24:01 +08:00
// 遍历与当前关键帧共视关系最好的10帧
2022-03-28 21:20:28 +08:00
for (vector<KeyFrame *>::iterator vit = vpNeighs.begin(), vend = vpNeighs.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKF2 = *vit;
2021-11-26 16:09:32 +08:00
// 如果该关键帧没有被当前关键帧访问过(也就是没有公共单词)则跳过
2022-03-28 21:20:28 +08:00
if (pKF2->mnPlaceRecognitionQuery != pKF->mnId)
2020-12-01 11:58:17 +08:00
continue;
2021-09-03 14:24:01 +08:00
// 累加小组总分
2022-03-28 21:20:28 +08:00
accScore += pKF2->mPlaceRecognitionScore;
2021-11-26 16:09:32 +08:00
// 如果大于组内最高分,则更新当前最高分记录
2022-03-28 21:20:28 +08:00
if (pKF2->mPlaceRecognitionScore > bestScore)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pBestKF = pKF2;
2020-12-01 11:58:17 +08:00
bestScore = pKF2->mPlaceRecognitionScore;
}
}
2021-09-03 14:24:01 +08:00
// 统计以组为单位的累计相似度和组内相似度最高的关键帧, 每个pair为<小组总相似度,组内相似度最高的关键帧指针>
2022-03-28 21:20:28 +08:00
lAccScoreAndMatch.push_back(make_pair(accScore, pBestKF));
2021-09-03 14:24:01 +08:00
// 统计最高得分, 这个bestAccSocre没有用到
2022-03-28 21:20:28 +08:00
if (accScore > bestAccScore)
bestAccScore = accScore;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
// cout << "Amount of candidates: " << lAccScoreAndMatch.size() << endl;
// 按相似度从大到小排序
2020-12-01 11:58:17 +08:00
lAccScoreAndMatch.sort(compFirst);
2021-09-03 14:24:01 +08:00
// 最后返回的变量, 记录回环的候选帧
2020-12-01 11:58:17 +08:00
vpLoopCand.reserve(nNumCandidates);
2021-09-03 14:24:01 +08:00
// 最后返回的变量, 记录融合候选帧
2020-12-01 11:58:17 +08:00
vpMergeCand.reserve(nNumCandidates);
2021-09-03 14:24:01 +08:00
// 避免重复添加
2022-03-28 21:20:28 +08:00
set<KeyFrame *> spAlreadyAddedKF;
// cout << "Candidates in score order " << endl;
// for(list<pair<float,KeyFrame*> >::iterator it=lAccScoreAndMatch.begin(), itend=lAccScoreAndMatch.end(); it!=itend; it++)
2020-12-01 11:58:17 +08:00
int i = 0;
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>>::iterator it = lAccScoreAndMatch.begin();
2021-11-26 16:09:32 +08:00
// 遍历lAccScoreAndMatch中所有的pair, 每个pair为<小组总相似度,组内相似度最高的关键帧指针>nNumCandidates默认为3
2022-03-28 21:20:28 +08:00
while (i < lAccScoreAndMatch.size() && (vpLoopCand.size() < nNumCandidates || vpMergeCand.size() < nNumCandidates))
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
// cout << "Accum score: " << it->first << endl;
// 拿到候选关键帧的指针
KeyFrame *pKFi = it->second;
if (pKFi->isBad())
2020-12-01 11:58:17 +08:00
continue;
2021-09-03 14:24:01 +08:00
// 如果没有被重复添加
2022-03-28 21:20:28 +08:00
if (!spAlreadyAddedKF.count(pKFi))
{
2021-09-03 14:24:01 +08:00
// 如果候选帧与当前关键帧在同一个地图里,且候选者数量还不足够
2022-03-28 21:20:28 +08:00
if (pKF->GetMap() == pKFi->GetMap() && vpLoopCand.size() < nNumCandidates)
2020-12-01 11:58:17 +08:00
{
2021-09-03 14:24:01 +08:00
// 添加到回环候选帧里
2020-12-01 11:58:17 +08:00
vpLoopCand.push_back(pKFi);
}
2021-09-03 14:24:01 +08:00
// 如果候选者与当前关键帧不再同一个地图里, 且候选者数量还不足够, 且候选者所在地图不是bad
2022-03-28 21:20:28 +08:00
else if (pKF->GetMap() != pKFi->GetMap() && vpMergeCand.size() < nNumCandidates && !pKFi->GetMap()->IsBad())
{
2021-09-03 14:24:01 +08:00
// 添加到融合候选帧里
2020-12-01 11:58:17 +08:00
vpMergeCand.push_back(pKFi);
}
2021-09-03 14:24:01 +08:00
// 防止重复添加
2020-12-01 11:58:17 +08:00
spAlreadyAddedKF.insert(pKFi);
}
i++;
it++;
}
}
2022-03-28 21:20:28 +08:00
vector<KeyFrame *> KeyFrameDatabase::DetectRelocalizationCandidates(Frame *F, Map *pMap)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<KeyFrame *> lKFsSharingWords;
2020-12-01 11:58:17 +08:00
// Search all keyframes that share a word with current frame
{
unique_lock<mutex> lock(mMutex);
2022-03-28 21:20:28 +08:00
for (DBoW2::BowVector::const_iterator vit = F->mBowVec.begin(), vend = F->mBowVec.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
list<KeyFrame *> &lKFs = mvInvertedFile[vit->first];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFs.begin(), lend = lKFs.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
if (pKFi->mnRelocQuery != F->mnId)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pKFi->mnRelocWords = 0;
pKFi->mnRelocQuery = F->mnId;
2020-12-01 11:58:17 +08:00
lKFsSharingWords.push_back(pKFi);
}
pKFi->mnRelocWords++;
}
}
}
2022-03-28 21:20:28 +08:00
if (lKFsSharingWords.empty())
return vector<KeyFrame *>();
2020-12-01 11:58:17 +08:00
// Only compare against those keyframes that share enough words
2022-03-28 21:20:28 +08:00
int maxCommonWords = 0;
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if ((*lit)->mnRelocWords > maxCommonWords)
maxCommonWords = (*lit)->mnRelocWords;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
int minCommonWords = maxCommonWords * 0.8f;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lScoreAndMatch;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nscores = 0;
2020-12-01 11:58:17 +08:00
// Compute similarity score.
2022-03-28 21:20:28 +08:00
for (list<KeyFrame *>::iterator lit = lKFsSharingWords.begin(), lend = lKFsSharingWords.end(); lit != lend; lit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = *lit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if (pKFi->mnRelocWords > minCommonWords)
2020-12-01 11:58:17 +08:00
{
nscores++;
2022-03-28 21:20:28 +08:00
float si = mpVoc->score(F->mBowVec, pKFi->mBowVec);
pKFi->mRelocScore = si;
lScoreAndMatch.push_back(make_pair(si, pKFi));
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
if (lScoreAndMatch.empty())
return vector<KeyFrame *>();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
list<pair<float, KeyFrame *>> lAccScoreAndMatch;
2020-12-01 11:58:17 +08:00
float bestAccScore = 0;
// Lets now accumulate score by covisibility
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lScoreAndMatch.begin(), itend = lScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
vector<KeyFrame *> vpNeighs = pKFi->GetBestCovisibilityKeyFrames(10);
2020-12-01 11:58:17 +08:00
float bestScore = it->first;
float accScore = bestScore;
2022-03-28 21:20:28 +08:00
KeyFrame *pBestKF = pKFi;
for (vector<KeyFrame *>::iterator vit = vpNeighs.begin(), vend = vpNeighs.end(); vit != vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKF2 = *vit;
if (pKF2->mnRelocQuery != F->mnId)
2020-12-01 11:58:17 +08:00
continue;
2022-03-28 21:20:28 +08:00
accScore += pKF2->mRelocScore;
if (pKF2->mRelocScore > bestScore)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
pBestKF = pKF2;
2020-12-01 11:58:17 +08:00
bestScore = pKF2->mRelocScore;
}
}
2022-03-28 21:20:28 +08:00
lAccScoreAndMatch.push_back(make_pair(accScore, pBestKF));
if (accScore > bestAccScore)
bestAccScore = accScore;
2020-12-01 11:58:17 +08:00
}
// Return all those keyframes with a score higher than 0.75*bestScore
2022-03-28 21:20:28 +08:00
float minScoreToRetain = 0.75f * bestAccScore;
set<KeyFrame *> spAlreadyAddedKF;
vector<KeyFrame *> vpRelocCandidates;
2020-12-01 11:58:17 +08:00
vpRelocCandidates.reserve(lAccScoreAndMatch.size());
2022-03-28 21:20:28 +08:00
for (list<pair<float, KeyFrame *>>::iterator it = lAccScoreAndMatch.begin(), itend = lAccScoreAndMatch.end(); it != itend; it++)
2020-12-01 11:58:17 +08:00
{
const float &si = it->first;
2022-03-28 21:20:28 +08:00
if (si > minScoreToRetain)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
KeyFrame *pKFi = it->second;
2020-12-01 11:58:17 +08:00
if (pKFi->GetMap() != pMap)
continue;
2022-03-28 21:20:28 +08:00
if (!spAlreadyAddedKF.count(pKFi))
2020-12-01 11:58:17 +08:00
{
vpRelocCandidates.push_back(pKFi);
spAlreadyAddedKF.insert(pKFi);
}
}
}
return vpRelocCandidates;
}
2022-03-28 21:20:28 +08:00
void KeyFrameDatabase::SetORBVocabulary(ORBVocabulary *pORBVoc)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
ORBVocabulary **ptr;
ptr = (ORBVocabulary **)(&mpVoc);
2020-12-01 11:58:17 +08:00
*ptr = pORBVoc;
mvInvertedFile.clear();
mvInvertedFile.resize(mpVoc->size());
}
2022-03-28 21:20:28 +08:00
} // namespace ORB_SLAM