orb_slam3_details/src/ORBmatcher.cc

2406 lines
99 KiB
C++
Raw Normal View History

2020-12-01 11:58:17 +08:00
/**
* This file is part of ORB-SLAM3
*
2022-03-28 21:20:28 +08:00
* 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.
2020-12-01 11:58:17 +08:00
* 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/>.
*/
#include "ORBmatcher.h"
#include<limits.h>
#include<opencv2/core/core.hpp>
#include "Thirdparty/DBoW2/DBoW2/FeatureVector.h"
#include<stdint-gcc.h>
using namespace std;
namespace ORB_SLAM3
{
2022-03-28 21:20:28 +08:00
const int ORBmatcher::TH_HIGH = 100;
const int ORBmatcher::TH_LOW = 50;
const int ORBmatcher::HISTO_LENGTH = 30;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 构造函数,参数默认值为0.6,true
2022-03-28 21:20:28 +08:00
ORBmatcher::ORBmatcher(float nnratio, bool checkOri): mfNNratio(nnratio), mbCheckOrientation(checkOri)
{
}
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 用于Tracking::SearchLocalPoints中匹配更多地图点
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchByProjection(Frame &F, const vector<MapPoint*> &vpMapPoints, const float th, const bool bFarPoints, const float thFarPoints)
{
int nmatches=0, left = 0, right = 0;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 如果 th=1 (RGBD 相机或者刚刚进行过重定位), 需要扩大范围搜索
2022-03-28 21:20:28 +08:00
const bool bFactor = th!=1.0;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 1 遍历有效的局部地图点
2022-03-28 21:20:28 +08:00
for(size_t iMP=0; iMP<vpMapPoints.size(); iMP++)
{
MapPoint* pMP = vpMapPoints[iMP];
if(!pMP->mbTrackInView && !pMP->mbTrackInViewR)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(bFarPoints && pMP->mTrackDepth>thFarPoints)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(pMP->isBad())
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(pMP->mbTrackInView)
{
2022-04-07 23:28:17 +08:00
// 通过距离预测的金字塔层数,该层数相对于当前的帧
2022-03-28 21:20:28 +08:00
const int &nPredictedLevel = pMP->mnTrackScaleLevel;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// The size of the window will depend on the viewing direction
2022-04-07 23:28:17 +08:00
// Step 2 设定搜索搜索窗口的大小。取决于视角, 若当前视角和平均视角夹角较小时, r取一个较小的值
2022-03-28 21:20:28 +08:00
float r = RadiusByViewingCos(pMP->mTrackViewCos);
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 如果需要扩大范围搜索则乘以阈值th
2022-03-28 21:20:28 +08:00
if(bFactor)
r*=th;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 3 通过投影点以及搜索窗口和预测的尺度进行搜索, 找出搜索半径内的候选匹配点索引
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices =
2022-04-07 23:28:17 +08:00
F.GetFeaturesInArea(pMP->mTrackProjX,pMP->mTrackProjY, // 该地图点投影到一帧上的坐标
r*F.mvScaleFactors[nPredictedLevel], // 认为搜索窗口的大小和该特征点被追踪到时所处的尺度也有关系
nPredictedLevel-1,nPredictedLevel); // 搜索的图层范围
2022-03-28 21:20:28 +08:00
2022-04-07 23:28:17 +08:00
// 没找到候选的,就放弃对当前点的匹配
2022-03-28 21:20:28 +08:00
if(!vIndices.empty()){
const cv::Mat MPdescriptor = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 最优的次优的描述子距离和index
2022-03-28 21:20:28 +08:00
int bestDist=256;
int bestLevel= -1;
int bestDist2=256;
int bestLevel2 = -1;
int bestIdx =-1 ;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Get best and second matches with near keypoints
2022-04-07 23:28:17 +08:00
// Step 4 寻找候选匹配点中的最佳和次佳匹配点
2022-03-28 21:20:28 +08:00
for(vector<size_t>::const_iterator vit=vIndices.begin(), vend=vIndices.end(); vit!=vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
const size_t idx = *vit;
2022-04-07 23:28:17 +08:00
// 如果Frame中的该兴趣点已经有对应的MapPoint了,则退出该次循环
2022-03-28 21:20:28 +08:00
if(F.mvpMapPoints[idx])
if(F.mvpMapPoints[idx]->Observations()>0)
continue;
2022-04-07 23:28:17 +08:00
//如果是双目数据
2022-03-28 21:20:28 +08:00
if(F.Nleft == -1 && F.mvuRight[idx]>0)
{
2022-04-07 23:28:17 +08:00
//计算在X轴上的投影误差
2022-03-28 21:20:28 +08:00
const float er = fabs(pMP->mTrackProjXR-F.mvuRight[idx]);
2022-04-07 23:28:17 +08:00
//超过阈值,说明这个点不行,丢掉.
//这里的阈值定义是以给定的搜索范围r为参考,然后考虑到越近的点(nPredictedLevel越大), 相机运动时对其产生的影响也就越大,
//因此需要扩大其搜索空间.
//当给定缩放倍率为1.2的时候, mvScaleFactors 中的数据是: 1 1.2 1.2^2 1.2^3 ...
2022-03-28 21:20:28 +08:00
if(er>r*F.mvScaleFactors[nPredictedLevel])
continue;
}
const cv::Mat &d = F.mDescriptors.row(idx);
2022-04-07 23:28:17 +08:00
// 计算地图点和候选投影点的描述子距离
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(MPdescriptor,d);
2022-04-07 23:28:17 +08:00
// 寻找描述子距离最小和次小的特征点和索引
2022-03-28 21:20:28 +08:00
if(dist<bestDist)
{
bestDist2=bestDist;
bestDist=dist;
bestLevel2 = bestLevel;
bestLevel = (F.Nleft == -1) ? F.mvKeysUn[idx].octave
: (idx < F.Nleft) ? F.mvKeys[idx].octave
2022-04-07 23:28:17 +08:00
: F.mvKeysRight[idx - F.Nleft].octave;
2022-03-28 21:20:28 +08:00
bestIdx=idx;
}
else if(dist<bestDist2)
{
bestLevel2 = (F.Nleft == -1) ? F.mvKeysUn[idx].octave
2022-04-07 23:28:17 +08:00
: (idx < F.Nleft) ? F.mvKeys[idx].octave
: F.mvKeysRight[idx - F.Nleft].octave;
2022-03-28 21:20:28 +08:00
bestDist2=dist;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
// Apply ratio to second match (only if best and second are in the same scale level)
2022-04-07 23:28:17 +08:00
// Step 5 筛选最佳匹配点
// 最佳匹配距离还需要满足在设定阈值内
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_HIGH)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// 条件1bestLevel==bestLevel2 表示 最佳和次佳在同一金字塔层级
// 条件2bestDist>mfNNratio*bestDist2 表示最佳和次佳距离不满足阈值比例。理论来说 bestDist/bestDist2 越小越好
2022-03-28 21:20:28 +08:00
if(bestLevel==bestLevel2 && bestDist>mfNNratio*bestDist2)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(bestLevel!=bestLevel2 || bestDist<=mfNNratio*bestDist2){
F.mvpMapPoints[bestIdx]=pMP;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(F.Nleft != -1 && F.mvLeftToRightMatch[bestIdx] != -1){ //Also match with the stereo observation at right camera
F.mvpMapPoints[F.mvLeftToRightMatch[bestIdx] + F.Nleft] = pMP;
nmatches++;
right++;
}
2020-12-01 11:58:17 +08:00
nmatches++;
2022-03-28 21:20:28 +08:00
left++;
2020-12-01 11:58:17 +08:00
}
}
}
}
2022-03-28 21:20:28 +08:00
if(F.Nleft != -1 && pMP->mbTrackInViewR){
const int &nPredictedLevel = pMP->mnTrackScaleLevelR;
if(nPredictedLevel != -1){
float r = RadiusByViewingCos(pMP->mTrackViewCosR);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices =
F.GetFeaturesInArea(pMP->mTrackProjXR,pMP->mTrackProjYR,r*F.mvScaleFactors[nPredictedLevel],nPredictedLevel-1,nPredictedLevel,true);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices.empty())
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat MPdescriptor = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist=256;
int bestLevel= -1;
int bestDist2=256;
int bestLevel2 = -1;
int bestIdx =-1 ;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Get best and second matches with near keypoints
for(vector<size_t>::const_iterator vit=vIndices.begin(), vend=vIndices.end(); vit!=vend; vit++)
{
const size_t idx = *vit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(F.mvpMapPoints[idx + F.Nleft])
if(F.mvpMapPoints[idx + F.Nleft]->Observations()>0)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &d = F.mDescriptors.row(idx + F.Nleft);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(MPdescriptor,d);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist<bestDist)
{
bestDist2=bestDist;
bestDist=dist;
bestLevel2 = bestLevel;
bestLevel = F.mvKeysRight[idx].octave;
bestIdx=idx;
}
else if(dist<bestDist2)
{
bestLevel2 = F.mvKeysRight[idx].octave;
bestDist2=dist;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
// Apply ratio to second match (only if best and second are in the same scale level)
if(bestDist<=TH_HIGH)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if(bestLevel==bestLevel2 && bestDist>mfNNratio*bestDist2)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(F.Nleft != -1 && F.mvRightToLeftMatch[bestIdx] != -1){ //Also match with the stereo observation at right camera
F.mvpMapPoints[F.mvRightToLeftMatch[bestIdx]] = pMP;
nmatches++;
left++;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
F.mvpMapPoints[bestIdx + F.Nleft]=pMP;
2020-12-01 11:58:17 +08:00
nmatches++;
2022-03-28 21:20:28 +08:00
right++;
2020-12-01 11:58:17 +08:00
}
}
}
}
2022-03-28 21:20:28 +08:00
return nmatches;
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
// 根据观察的视角来计算匹配的时的搜索窗口大小
2022-03-28 21:20:28 +08:00
float ORBmatcher::RadiusByViewingCos(const float &viewCos)
{
2022-04-07 23:28:17 +08:00
// 当视角相差小于3.6°对应cos(3.6°)=0.998搜索范围是2.5否则是4
2022-03-28 21:20:28 +08:00
if(viewCos>0.998)
return 2.5;
else
return 4.0;
}
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
/**
* @brief
*
* @param[in] pKF
* @param[in] F
* @param[in] vpMapPointMatches FNULL
* @return int
*/
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchByBoW(KeyFrame* pKF,Frame &F, vector<MapPoint*> &vpMapPointMatches)
{
2022-04-07 23:28:17 +08:00
// 获取该关键帧的地图点
2022-03-28 21:20:28 +08:00
const vector<MapPoint*> vpMapPointsKF = pKF->GetMapPointMatches();
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 和普通帧F特征点的索引一致
2022-03-28 21:20:28 +08:00
vpMapPointMatches = vector<MapPoint*>(F.N,static_cast<MapPoint*>(NULL));
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 取出关键帧的词袋特征向量
2022-03-28 21:20:28 +08:00
const DBoW2::FeatureVector &vFeatVecKF = pKF->mFeatVec;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nmatches=0;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 特征点角度旋转差统计用的直方图
2022-03-28 21:20:28 +08:00
vector<int> rotHist[HISTO_LENGTH];
for(int i=0;i<HISTO_LENGTH;i++)
rotHist[i].reserve(500);
2022-04-07 23:28:17 +08:00
// 将0~360的数转换到0~HISTO_LENGTH的系数
//! 原作者代码是 const float factor = 1.0f/HISTO_LENGTH; 是错误的,更改为下面代码
// const float factor = HISTO_LENGTH/360.0f;
2022-03-28 21:20:28 +08:00
const float factor = 1.0f/HISTO_LENGTH;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// We perform the matching over ORB that belong to the same vocabulary node (at a certain level)
2022-04-07 23:28:17 +08:00
// 将属于同一节点(特定层)的ORB特征进行匹配
2022-03-28 21:20:28 +08:00
DBoW2::FeatureVector::const_iterator KFit = vFeatVecKF.begin();
DBoW2::FeatureVector::const_iterator Fit = F.mFeatVec.begin();
DBoW2::FeatureVector::const_iterator KFend = vFeatVecKF.end();
DBoW2::FeatureVector::const_iterator Fend = F.mFeatVec.end();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
while(KFit != KFend && Fit != Fend)
{
2022-04-07 23:28:17 +08:00
// Step 1分别取出属于同一node的ORB特征点(只有属于同一node才有可能是匹配点)
// first 元素就是node id遍历
if(KFit->first == Fit->first)
2022-03-28 21:20:28 +08:00
{
2022-04-07 23:28:17 +08:00
// second 是该node内存储的feature index
2022-03-28 21:20:28 +08:00
const vector<unsigned int> vIndicesKF = KFit->second;
const vector<unsigned int> vIndicesF = Fit->second;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 2遍历KF中属于该node的特征点
2022-03-28 21:20:28 +08:00
for(size_t iKF=0; iKF<vIndicesKF.size(); iKF++)
{
2022-04-07 23:28:17 +08:00
// 关键帧该节点中特征点的索引
2022-03-28 21:20:28 +08:00
const unsigned int realIdxKF = vIndicesKF[iKF];
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 取出KF中该特征对应的地图点
2022-03-28 21:20:28 +08:00
MapPoint* pMP = vpMapPointsKF[realIdxKF];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(!pMP)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(pMP->isBad())
continue;
2022-04-07 23:28:17 +08:00
// 取出关键帧KF中该特征对应的描述子
const cv::Mat &dKF= pKF->mDescriptors.row(realIdxKF);
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
int bestDist1=256; // 最好的距离(最小距离)
2022-03-28 21:20:28 +08:00
int bestIdxF =-1 ;
2022-04-07 23:28:17 +08:00
int bestDist2=256; // 次好距离(倒数第二小距离)
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist1R=256;
int bestIdxFR =-1 ;
int bestDist2R=256;
2022-04-07 23:28:17 +08:00
// Step 3遍历F中属于该node的特征点寻找最佳匹配点
2022-03-28 21:20:28 +08:00
for(size_t iF=0; iF<vIndicesF.size(); iF++)
{
if(F.Nleft == -1){
2022-04-07 23:28:17 +08:00
// 这里的realIdxF是指普通帧该节点中特征点的索引
2022-03-28 21:20:28 +08:00
const unsigned int realIdxF = vIndicesF[iF];
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 如果地图点存在,说明这个点已经被匹配过了,不再匹配,加快速度
2022-03-28 21:20:28 +08:00
if(vpMapPointMatches[realIdxF])
continue;
2022-04-07 23:28:17 +08:00
// 取出普通帧F中该特征对应的描述子
2022-03-28 21:20:28 +08:00
const cv::Mat &dF = F.mDescriptors.row(realIdxF);
2022-04-07 23:28:17 +08:00
// 计算描述子的距离
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(dKF,dF);
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 遍历,记录最佳距离、最佳距离对应的索引、次佳距离等
// 如果 dist < bestDist1 < bestDist2更新bestDist1 bestDist2
2022-03-28 21:20:28 +08:00
if(dist<bestDist1)
{
bestDist2=bestDist1;
bestDist1=dist;
bestIdxF=realIdxF;
}
2022-04-07 23:28:17 +08:00
// 如果bestDist1 < dist < bestDist2更新bestDist2
2022-03-28 21:20:28 +08:00
else if(dist<bestDist2)
{
bestDist2=dist;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
else{
const unsigned int realIdxF = vIndicesF[iF];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vpMapPointMatches[realIdxF])
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &dF = F.mDescriptors.row(realIdxF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(dKF,dF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(realIdxF < F.Nleft && dist<bestDist1){
bestDist2=bestDist1;
bestDist1=dist;
bestIdxF=realIdxF;
}
else if(realIdxF < F.Nleft && dist<bestDist2){
bestDist2=dist;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(realIdxF >= F.Nleft && dist<bestDist1R){
bestDist2R=bestDist1R;
bestDist1R=dist;
bestIdxFR=realIdxF;
}
else if(realIdxF >= F.Nleft && dist<bestDist2R){
bestDist2R=dist;
}
2020-12-01 11:58:17 +08:00
}
}
2022-04-07 23:28:17 +08:00
// Step 4根据阈值 和 角度投票剔除误匹配
// Step 4.1:第一关筛选:匹配距离必须小于设定阈值
2022-03-28 21:20:28 +08:00
if(bestDist1<=TH_LOW)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// Step 4.2:第二关筛选:最佳匹配比次佳匹配明显要好,那么最佳匹配才真正靠谱
2022-03-28 21:20:28 +08:00
if(static_cast<float>(bestDist1)<mfNNratio*static_cast<float>(bestDist2))
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// Step 4.3:记录成功匹配特征点的对应的地图点(来自关键帧)
2022-03-28 21:20:28 +08:00
vpMapPointMatches[bestIdxF]=pMP;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 这里的realIdxKF是当前遍历到的关键帧的特征点id
2020-12-01 11:58:17 +08:00
const cv::KeyPoint &kp =
(!pKF->mpCamera2) ? pKF->mvKeysUn[realIdxKF] :
(realIdxKF >= pKF -> NLeft) ? pKF -> mvKeysRight[realIdxKF - pKF -> NLeft]
: pKF -> mvKeys[realIdxKF];
2022-04-07 23:28:17 +08:00
// Step 4.4:计算匹配点旋转角度差所在的直方图
2020-12-01 11:58:17 +08:00
if(mbCheckOrientation)
{
cv::KeyPoint &Fkp =
2022-03-28 21:20:28 +08:00
(!pKF->mpCamera2 || F.Nleft == -1) ? F.mvKeys[bestIdxF] :
(bestIdxF >= F.Nleft) ? F.mvKeysRight[bestIdxF - F.Nleft]
2022-04-07 23:28:17 +08:00
: F.mvKeys[bestIdxF];
// 所有的特征点的角度变化应该是一致的,通过直方图统计得到最准确的角度变化值
2020-12-01 11:58:17 +08:00
float rot = kp.angle-Fkp.angle;
if(rot<0.0)
rot+=360.0f;
2022-04-07 23:28:17 +08:00
int bin = round(rot*factor);// 将rot分配到bin组, 四舍五入, 其实就是离散到对应的直方图组中
2020-12-01 11:58:17 +08:00
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
2022-03-28 21:20:28 +08:00
rotHist[bin].push_back(bestIdxF);
2020-12-01 11:58:17 +08:00
}
nmatches++;
}
2022-03-28 21:20:28 +08:00
if(bestDist1R<=TH_LOW)
{
if(static_cast<float>(bestDist1R)<mfNNratio*static_cast<float>(bestDist2R) || true)
{
vpMapPointMatches[bestIdxFR]=pMP;
const cv::KeyPoint &kp =
(!pKF->mpCamera2) ? pKF->mvKeysUn[realIdxKF] :
(realIdxKF >= pKF -> NLeft) ? pKF -> mvKeysRight[realIdxKF - pKF -> NLeft]
: pKF -> mvKeys[realIdxKF];
if(mbCheckOrientation)
{
cv::KeyPoint &Fkp =
(!F.mpCamera2) ? F.mvKeys[bestIdxFR] :
(bestIdxFR >= F.Nleft) ? F.mvKeysRight[bestIdxFR - F.Nleft]
2022-04-07 23:28:17 +08:00
: F.mvKeys[bestIdxFR];
2022-03-28 21:20:28 +08:00
float rot = kp.angle-Fkp.angle;
if(rot<0.0)
rot+=360.0f;
int bin = round(rot*factor);
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
rotHist[bin].push_back(bestIdxFR);
}
nmatches++;
}
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
KFit++;
Fit++;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
else if(KFit->first < Fit->first)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// 对齐
2022-03-28 21:20:28 +08:00
KFit = vFeatVecKF.lower_bound(Fit->first);
}
else
{
2022-04-07 23:28:17 +08:00
// 对齐
2022-03-28 21:20:28 +08:00
Fit = F.mFeatVec.lower_bound(KFit->first);
2020-12-01 11:58:17 +08:00
}
}
2022-04-07 23:28:17 +08:00
// Step 5 根据方向剔除误匹配的点
2022-03-28 21:20:28 +08:00
if(mbCheckOrientation)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// index
2022-03-28 21:20:28 +08:00
int ind1=-1;
int ind2=-1;
int ind3=-1;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 筛选出在旋转角度差落在在直方图区间内数量最多的前三个bin的索引
2022-03-28 21:20:28 +08:00
ComputeThreeMaxima(rotHist,HISTO_LENGTH,ind1,ind2,ind3);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for(int i=0; i<HISTO_LENGTH; i++)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// 如果特征点的旋转角度变化量属于这三个组,则保留
2022-03-28 21:20:28 +08:00
if(i==ind1 || i==ind2 || i==ind3)
continue;
2022-04-07 23:28:17 +08:00
// 剔除掉不在前三的匹配对,因为他们不符合“主流旋转方向”
2022-03-28 21:20:28 +08:00
for(size_t j=0, jend=rotHist[i].size(); j<jend; j++)
{
vpMapPointMatches[rotHist[i][j]]=static_cast<MapPoint*>(NULL);
nmatches--;
}
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
return nmatches;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchByProjection(KeyFrame* pKF, Sophus::Sim3f &Scw, const vector<MapPoint*> &vpPoints,
vector<MapPoint*> &vpMatched, int th, float ratioHamming)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
// Get Calibration Parameters for later projection
const float &fx = pKF->fx;
const float &fy = pKF->fy;
const float &cx = pKF->cx;
const float &cy = pKF->cy;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Decompose Scw
// Step 1 分解Sim变换矩阵
//? 为什么要剥离尺度信息?
2022-03-28 21:20:28 +08:00
Sophus::SE3f Tcw = Sophus::SE3f(Scw.rotationMatrix(),Scw.translation()/Scw.scale());
2022-04-07 23:28:17 +08:00
Eigen::Vector3f Ow = Tcw.inverse().translation(); // 世界坐标系下相机光心坐标
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Set of MapPoints already found in the KeyFrame
2022-04-07 23:28:17 +08:00
// 使用set类型记录前面已经成功的匹配关系避免重复匹配。并去除其中无效匹配关系NULL
2022-03-28 21:20:28 +08:00
set<MapPoint*> spAlreadyFound(vpMatched.begin(), vpMatched.end());
spAlreadyFound.erase(static_cast<MapPoint*>(NULL));
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nmatches=0;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// For each Candidate MapPoint Project and Match
2022-04-07 23:28:17 +08:00
// Step 2 遍历闭环KF及其共视KF的所有地图点不考虑当前KF已经匹配的地图点投影到当前KF
2022-03-28 21:20:28 +08:00
for(int iMP=0, iendMP=vpPoints.size(); iMP<iendMP; iMP++)
{
MapPoint* pMP = vpPoints[iMP];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Discard Bad MapPoints and already found
2022-04-07 23:28:17 +08:00
// Step 2.1 丢弃坏点跳过当前KF已经匹配上的地图点
2022-03-28 21:20:28 +08:00
if(pMP->isBad() || spAlreadyFound.count(pMP))
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Get 3D Coords.
2022-04-07 23:28:17 +08:00
// Step 2.2 投影到当前KF的图像坐标并判断是否有效
2022-03-28 21:20:28 +08:00
Eigen::Vector3f p3Dw = pMP->GetWorldPos();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Transform into Camera Coords.
Eigen::Vector3f p3Dc = Tcw * p3Dw;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be positive
2022-04-07 23:28:17 +08:00
// 深度值必须为正
2022-03-28 21:20:28 +08:00
if(p3Dc(2)<0.0)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Project into Image
const Eigen::Vector2f uv = pKF->mpCamera->project(p3Dc);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Point must be inside the image
2022-04-07 23:28:17 +08:00
// 在图像范围内
2022-03-28 21:20:28 +08:00
if(!pKF->IsInImage(uv(0),uv(1)))
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be inside the scale invariance region of the point
2022-04-07 23:28:17 +08:00
// 判断距离是否在有效距离内
2022-03-28 21:20:28 +08:00
const float maxDistance = pMP->GetMaxDistanceInvariance();
const float minDistance = pMP->GetMinDistanceInvariance();
2022-04-07 23:28:17 +08:00
// 地图点到相机光心的向量
2022-03-28 21:20:28 +08:00
Eigen::Vector3f PO = p3Dw-Ow;
const float dist = PO.norm();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist<minDistance || dist>maxDistance)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Viewing angle must be less than 60 deg
2022-04-07 23:28:17 +08:00
// 观察角度小于60°
2022-03-28 21:20:28 +08:00
Eigen::Vector3f Pn = pMP->GetNormal();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(PO.dot(Pn)<0.5*dist)
continue;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 根据当前这个地图点距离当前KF光心的距离,预测该点在当前KF中的尺度(图层)
2022-03-28 21:20:28 +08:00
int nPredictedLevel = pMP->PredictScale(dist,pKF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a radius
2022-04-07 23:28:17 +08:00
// 根据尺度确定搜索半径
2022-03-28 21:20:28 +08:00
const float radius = th*pKF->mvScaleFactors[nPredictedLevel];
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 2.3 搜索候选匹配点
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices = pKF->GetFeaturesInArea(uv(0),uv(1),radius);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices.empty())
2020-12-01 11:58:17 +08:00
continue;
2022-03-28 21:20:28 +08:00
// Match to the most similar keypoint in the radius
const cv::Mat dMP = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist = 256;
int bestIdx = -1;
2022-04-07 23:28:17 +08:00
// Step 2.4 遍历候选匹配点,找到最佳匹配点
2022-03-28 21:20:28 +08:00
for(vector<size_t>::const_iterator vit=vIndices.begin(), vend=vIndices.end(); vit!=vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
const size_t idx = *vit;
if(vpMatched[idx])
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int &kpLevel= pKF->mvKeysUn[idx].octave;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 不在一个尺度也不行
2022-03-28 21:20:28 +08:00
if(kpLevel<nPredictedLevel-1 || kpLevel>nPredictedLevel)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &dKF = pKF->mDescriptors.row(idx);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(dMP,dKF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist<bestDist)
{
bestDist = dist;
bestIdx = idx;
}
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
// 该MapPoint与bestIdx对应的特征点匹配成功
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_LOW*ratioHamming)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
vpMatched[bestIdx]=pMP;
2020-12-01 11:58:17 +08:00
nmatches++;
}
2022-03-28 21:20:28 +08:00
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
// Step 3 返回新的成功匹配的点对的数目
2022-03-28 21:20:28 +08:00
return nmatches;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchByProjection(KeyFrame* pKF, Sophus::Sim3<float> &Scw, const std::vector<MapPoint*> &vpPoints, const std::vector<KeyFrame*> &vpPointsKFs,
std::vector<MapPoint*> &vpMatched, std::vector<KeyFrame*> &vpMatchedKF, int th, float ratioHamming)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
// Get Calibration Parameters for later projection
const float &fx = pKF->fx;
const float &fy = pKF->fy;
const float &cx = pKF->cx;
const float &cy = pKF->cy;
Sophus::SE3f Tcw = Sophus::SE3f(Scw.rotationMatrix(),Scw.translation()/Scw.scale());
Eigen::Vector3f Ow = Tcw.inverse().translation();
// Set of MapPoints already found in the KeyFrame
set<MapPoint*> spAlreadyFound(vpMatched.begin(), vpMatched.end());
spAlreadyFound.erase(static_cast<MapPoint*>(NULL));
int nmatches=0;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// For each Candidate MapPoint Project and Match
for(int iMP=0, iendMP=vpPoints.size(); iMP<iendMP; iMP++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
MapPoint* pMP = vpPoints[iMP];
KeyFrame* pKFi = vpPointsKFs[iMP];
// Discard Bad MapPoints and already found
if(pMP->isBad() || spAlreadyFound.count(pMP))
2020-12-01 11:58:17 +08:00
continue;
2022-03-28 21:20:28 +08:00
// Get 3D Coords.
Eigen::Vector3f p3Dw = pMP->GetWorldPos();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Transform into Camera Coords.
Eigen::Vector3f p3Dc = Tcw * p3Dw;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be positive
if(p3Dc(2)<0.0)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Project into Image
const float invz = 1/p3Dc(2);
const float x = p3Dc(0)*invz;
const float y = p3Dc(1)*invz;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float u = fx*x+cx;
const float v = fy*y+cy;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Point must be inside the image
if(!pKF->IsInImage(u,v))
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be inside the scale invariance region of the point
const float maxDistance = pMP->GetMaxDistanceInvariance();
const float minDistance = pMP->GetMinDistanceInvariance();
Eigen::Vector3f PO = p3Dw-Ow;
const float dist = PO.norm();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist<minDistance || dist>maxDistance)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Viewing angle must be less than 60 deg
Eigen::Vector3f Pn = pMP->GetNormal();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(PO.dot(Pn)<0.5*dist)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nPredictedLevel = pMP->PredictScale(dist,pKF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a radius
const float radius = th*pKF->mvScaleFactors[nPredictedLevel];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices = pKF->GetFeaturesInArea(u,v,radius);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices.empty())
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Match to the most similar keypoint in the radius
const cv::Mat dMP = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist = 256;
int bestIdx = -1;
for(vector<size_t>::const_iterator vit=vIndices.begin(), vend=vIndices.end(); vit!=vend; vit++)
{
const size_t idx = *vit;
if(vpMatched[idx])
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int &kpLevel= pKF->mvKeysUn[idx].octave;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(kpLevel<nPredictedLevel-1 || kpLevel>nPredictedLevel)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &dKF = pKF->mDescriptors.row(idx);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(dMP,dKF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist<bestDist)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
bestDist = dist;
bestIdx = idx;
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_LOW*ratioHamming)
{
vpMatched[bestIdx] = pMP;
vpMatchedKF[bestIdx] = pKFi;
nmatches++;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
return nmatches;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchForInitialization(Frame &F1, Frame &F2, vector<cv::Point2f> &vbPrevMatched, vector<int> &vnMatches12, int windowSize)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
int nmatches=0;
2022-04-07 23:28:17 +08:00
// F1中特征点和F2中匹配关系注意是按照F1特征点数目分配空间
2022-03-28 21:20:28 +08:00
vnMatches12 = vector<int>(F1.mvKeysUn.size(),-1);
2022-04-07 23:28:17 +08:00
// Step 1 构建旋转直方图HISTO_LENGTH = 30
2022-03-28 21:20:28 +08:00
vector<int> rotHist[HISTO_LENGTH];
2022-04-07 23:28:17 +08:00
// 每个bin里预分配500个因为使用的是vector不够的话可以自动扩展容量
2022-03-28 21:20:28 +08:00
for(int i=0;i<HISTO_LENGTH;i++)
rotHist[i].reserve(500);
2022-04-07 23:28:17 +08:00
//! 原作者代码是 const float factor = 1.0f/HISTO_LENGTH; 是错误的,更改为下面代码
// const float factor = HISTO_LENGTH/360.0f;
2022-03-28 21:20:28 +08:00
const float factor = 1.0f/HISTO_LENGTH;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 匹配点对距离注意是按照F2特征点数目分配空间
2022-03-28 21:20:28 +08:00
vector<int> vMatchedDistance(F2.mvKeysUn.size(),INT_MAX);
2022-04-07 23:28:17 +08:00
// 从帧2到帧1的反向匹配注意是按照F2特征点数目分配空间
2022-03-28 21:20:28 +08:00
vector<int> vnMatches21(F2.mvKeysUn.size(),-1);
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 遍历帧1中的所有特征点
2022-03-28 21:20:28 +08:00
for(size_t i1=0, iend1=F1.mvKeysUn.size(); i1<iend1; i1++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
cv::KeyPoint kp1 = F1.mvKeysUn[i1];
int level1 = kp1.octave;
2022-04-07 23:28:17 +08:00
// 只使用原始图像上提取的特征点
2022-03-28 21:20:28 +08:00
if(level1>0)
2020-12-01 11:58:17 +08:00
continue;
2022-04-07 23:28:17 +08:00
// Step 2 在半径窗口内搜索当前帧F2中所有的候选匹配特征点
// vbPrevMatched 输入的是参考帧 F1的特征点
// windowSize = 100输入最大最小金字塔层级 均为0
2022-03-28 21:20:28 +08:00
vector<size_t> vIndices2 = F2.GetFeaturesInArea(vbPrevMatched[i1].x,vbPrevMatched[i1].y, windowSize,level1,level1);
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 没有候选特征点,跳过
2022-03-28 21:20:28 +08:00
if(vIndices2.empty())
continue;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 取出参考帧F1中当前遍历特征点对应的描述子
2022-03-28 21:20:28 +08:00
cv::Mat d1 = F1.mDescriptors.row(i1);
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
int bestDist = INT_MAX; //最佳描述子匹配距离,越小越好
int bestDist2 = INT_MAX; //次佳描述子匹配距离
int bestIdx2 = -1; //最佳候选特征点在F2中的index
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 3 遍历搜索搜索窗口中的所有潜在的匹配候选点,找到最优的和次优的
2022-03-28 21:20:28 +08:00
for(vector<size_t>::iterator vit=vIndices2.begin(); vit!=vIndices2.end(); vit++)
{
size_t i2 = *vit;
2022-04-07 23:28:17 +08:00
// 取出候选特征点对应的描述子
2022-03-28 21:20:28 +08:00
cv::Mat d2 = F2.mDescriptors.row(i2);
2022-04-07 23:28:17 +08:00
// 计算两个特征点描述子距离
2022-03-28 21:20:28 +08:00
int dist = DescriptorDistance(d1,d2);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vMatchedDistance[i2]<=dist)
continue;
2022-04-07 23:28:17 +08:00
// 如果当前匹配距离更小,更新最佳次佳距离
2022-03-28 21:20:28 +08:00
if(dist<bestDist)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
bestDist2=bestDist;
bestDist=dist;
bestIdx2=i2;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
else if(dist<bestDist2)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
bestDist2=dist;
}
}
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 4 对最优次优结果进行检查,满足阈值、最优/次优比例,删除重复匹配
// 即使算出了最佳描述子匹配距离,也不一定保证配对成功。要小于设定阈值
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_LOW)
{
2022-04-07 23:28:17 +08:00
// 最佳距离比次佳距离要小于设定的比例,这样特征点辨识度更高
2022-03-28 21:20:28 +08:00
if(bestDist<(float)bestDist2*mfNNratio)
{
2022-04-07 23:28:17 +08:00
// 如果找到的候选特征点对应F1中特征点已经匹配过了说明发生了重复匹配将原来的匹配也删掉
2022-03-28 21:20:28 +08:00
if(vnMatches21[bestIdx2]>=0)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
vnMatches12[vnMatches21[bestIdx2]]=-1;
nmatches--;
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
// 次优的匹配关系,双向建立
// vnMatches12保存参考帧F1和F2匹配关系index保存是F1对应特征点索引值保存的是匹配好的F2特征点索引
2022-03-28 21:20:28 +08:00
vnMatches12[i1]=bestIdx2;
vnMatches21[bestIdx2]=i1;
vMatchedDistance[bestIdx2]=bestDist;
2020-12-01 11:58:17 +08:00
nmatches++;
2022-04-07 23:28:17 +08:00
// Step 5 计算匹配点旋转角度差所在的直方图
2020-12-01 11:58:17 +08:00
if(mbCheckOrientation)
{
2022-04-07 23:28:17 +08:00
// 计算匹配特征点的角度差,这里单位是角度°,不是弧度
2022-03-28 21:20:28 +08:00
float rot = F1.mvKeysUn[i1].angle-F2.mvKeysUn[bestIdx2].angle;
2020-12-01 11:58:17 +08:00
if(rot<0.0)
rot+=360.0f;
2022-04-07 23:28:17 +08:00
// 前面factor = HISTO_LENGTH/360.0f
// bin = rot / 360.of * HISTO_LENGTH 表示当前rot被分配在第几个直方图bin
2020-12-01 11:58:17 +08:00
int bin = round(rot*factor);
2022-04-07 23:28:17 +08:00
// 如果bin 满了又是一个轮回
2020-12-01 11:58:17 +08:00
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
2022-03-28 21:20:28 +08:00
rotHist[bin].push_back(i1);
2020-12-01 11:58:17 +08:00
}
}
}
}
2022-04-07 23:28:17 +08:00
// Step 6 筛除旋转直方图中“非主流”部分
2022-03-28 21:20:28 +08:00
if(mbCheckOrientation)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
int ind1=-1;
int ind2=-1;
int ind3=-1;
2022-04-07 23:28:17 +08:00
// 筛选出在旋转角度差落在在直方图区间内数量最多的前三个bin的索引
2022-03-28 21:20:28 +08:00
ComputeThreeMaxima(rotHist,HISTO_LENGTH,ind1,ind2,ind3);
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
for(int i=0; i<HISTO_LENGTH; i++)
{
if(i==ind1 || i==ind2 || i==ind3)
continue;
2022-04-07 23:28:17 +08:00
// 剔除掉不在前三的匹配对,因为他们不符合“主流旋转方向”
2022-03-28 21:20:28 +08:00
for(size_t j=0, jend=rotHist[i].size(); j<jend; j++)
{
int idx1 = rotHist[i][j];
if(vnMatches12[idx1]>=0)
{
vnMatches12[idx1]=-1;
nmatches--;
}
}
}
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
}
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
//Update prev matched
2022-04-07 23:28:17 +08:00
// Step 7 将最后通过筛选的匹配好的特征点保存到vbPrevMatched
2022-03-28 21:20:28 +08:00
for(size_t i1=0, iend1=vnMatches12.size(); i1<iend1; i1++)
if(vnMatches12[i1]>=0)
vbPrevMatched[i1]=F2.mvKeysUn[vnMatches12[i1]].pt;
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
return nmatches;
}
2021-08-09 19:34:51 +08:00
2022-04-07 23:28:17 +08:00
/*
* @brief
* @details bowpKFFnode
* node
*
* @param pKF1 KeyFrame1
* @param pKF2 KeyFrame2
* @param vpMatches12 pKF2pKF1MapPointvpMatches12[i]nullipKF1
* @return
*/
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchByBoW(KeyFrame *pKF1, KeyFrame *pKF2, vector<MapPoint *> &vpMatches12)
{
2022-04-07 23:28:17 +08:00
// Step 1 分别取出两个关键帧的特征点、BoW 向量、地图点、描述子
2022-03-28 21:20:28 +08:00
const vector<cv::KeyPoint> &vKeysUn1 = pKF1->mvKeysUn;
const DBoW2::FeatureVector &vFeatVec1 = pKF1->mFeatVec;
const vector<MapPoint*> vpMapPoints1 = pKF1->GetMapPointMatches();
const cv::Mat &Descriptors1 = pKF1->mDescriptors;
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
const vector<cv::KeyPoint> &vKeysUn2 = pKF2->mvKeysUn;
const DBoW2::FeatureVector &vFeatVec2 = pKF2->mFeatVec;
const vector<MapPoint*> vpMapPoints2 = pKF2->GetMapPointMatches();
const cv::Mat &Descriptors2 = pKF2->mDescriptors;
2021-08-09 19:34:51 +08:00
2022-04-07 23:28:17 +08:00
// 保存匹配结果
2022-03-28 21:20:28 +08:00
vpMatches12 = vector<MapPoint*>(vpMapPoints1.size(),static_cast<MapPoint*>(NULL));
vector<bool> vbMatched2(vpMapPoints2.size(),false);
2021-08-09 19:34:51 +08:00
2022-04-07 23:28:17 +08:00
// Step 2 构建旋转直方图HISTO_LENGTH = 30
2021-08-09 19:34:51 +08:00
vector<int> rotHist[HISTO_LENGTH];
for(int i=0;i<HISTO_LENGTH;i++)
rotHist[i].reserve(500);
2022-04-07 23:28:17 +08:00
//! 原作者代码是 const float factor = 1.0f/HISTO_LENGTH; 是错误的,更改为下面代码
// const float factor = HISTO_LENGTH/360.0f;
2021-08-09 19:34:51 +08:00
const float factor = 1.0f/HISTO_LENGTH;
2022-03-28 21:20:28 +08:00
int nmatches = 0;
2021-08-09 19:34:51 +08:00
DBoW2::FeatureVector::const_iterator f1it = vFeatVec1.begin();
DBoW2::FeatureVector::const_iterator f2it = vFeatVec2.begin();
DBoW2::FeatureVector::const_iterator f1end = vFeatVec1.end();
DBoW2::FeatureVector::const_iterator f2end = vFeatVec2.end();
2022-03-28 21:20:28 +08:00
while(f1it != f1end && f2it != f2end)
2021-08-09 19:34:51 +08:00
{
2022-04-07 23:28:17 +08:00
// Step 3 开始遍历分别取出属于同一node的特征点(只有属于同一node才有可能是匹配点)
2021-08-09 19:34:51 +08:00
if(f1it->first == f2it->first)
{
2022-04-07 23:28:17 +08:00
// 遍历KF中属于该node的特征点
2021-08-09 19:34:51 +08:00
for(size_t i1=0, iend1=f1it->second.size(); i1<iend1; i1++)
{
const size_t idx1 = f1it->second[i1];
2022-03-28 21:20:28 +08:00
if(pKF1 -> NLeft != -1 && idx1 >= pKF1 -> mvKeysUn.size()){
2021-08-09 19:34:51 +08:00
continue;
}
2022-03-28 21:20:28 +08:00
MapPoint* pMP1 = vpMapPoints1[idx1];
if(!pMP1)
continue;
if(pMP1->isBad())
continue;
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &d1 = Descriptors1.row(idx1);
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
int bestDist1=256;
int bestIdx2 =-1 ;
int bestDist2=256;
2021-08-09 19:34:51 +08:00
2022-04-07 23:28:17 +08:00
// Step 4 遍历KF2中属于该node的特征点找到了最优及次优匹配点
2021-08-09 19:34:51 +08:00
for(size_t i2=0, iend2=f2it->second.size(); i2<iend2; i2++)
{
2022-03-28 21:20:28 +08:00
const size_t idx2 = f2it->second[i2];
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
if(pKF2 -> NLeft != -1 && idx2 >= pKF2 -> mvKeysUn.size()){
2021-08-09 19:34:51 +08:00
continue;
}
2022-03-28 21:20:28 +08:00
MapPoint* pMP2 = vpMapPoints2[idx2];
2021-08-09 19:34:51 +08:00
2022-04-07 23:28:17 +08:00
// 如果已经有匹配的点,或者遍历到的特征点对应的地图点无效
2022-03-28 21:20:28 +08:00
if(vbMatched2[idx2] || !pMP2)
continue;
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
if(pMP2->isBad())
continue;
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &d2 = Descriptors2.row(idx2);
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
int dist = DescriptorDistance(d1,d2);
2021-08-09 19:34:51 +08:00
2022-03-28 21:20:28 +08:00
if(dist<bestDist1)
{
bestDist2=bestDist1;
bestDist1=dist;
bestIdx2=idx2;
2021-08-09 19:34:51 +08:00
}
2022-03-28 21:20:28 +08:00
else if(dist<bestDist2)
2021-08-09 19:34:51 +08:00
{
2022-03-28 21:20:28 +08:00
bestDist2=dist;
2021-08-09 19:34:51 +08:00
}
}
2022-04-07 23:28:17 +08:00
// Step 5 对匹配结果进行检查,满足阈值、最优/次优比例,记录旋转直方图信息
2022-03-28 21:20:28 +08:00
if(bestDist1<TH_LOW)
2021-08-09 19:34:51 +08:00
{
2022-03-28 21:20:28 +08:00
if(static_cast<float>(bestDist1)<mfNNratio*static_cast<float>(bestDist2))
2021-08-09 19:34:51 +08:00
{
2022-03-28 21:20:28 +08:00
vpMatches12[idx1]=vpMapPoints2[bestIdx2];
vbMatched2[bestIdx2]=true;
if(mbCheckOrientation)
{
float rot = vKeysUn1[idx1].angle-vKeysUn2[bestIdx2].angle;
if(rot<0.0)
rot+=360.0f;
int bin = round(rot*factor);
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
rotHist[bin].push_back(idx1);
}
nmatches++;
2021-08-09 19:34:51 +08:00
}
}
}
f1it++;
f2it++;
}
else if(f1it->first < f2it->first)
{
f1it = vFeatVec1.lower_bound(f2it->first);
}
else
{
f2it = vFeatVec2.lower_bound(f1it->first);
}
}
2022-04-07 23:28:17 +08:00
// Step 6 检查旋转直方图分布,剔除差异较大的匹配
2021-08-09 19:34:51 +08:00
if(mbCheckOrientation)
{
int ind1=-1;
int ind2=-1;
int ind3=-1;
ComputeThreeMaxima(rotHist,HISTO_LENGTH,ind1,ind2,ind3);
for(int i=0; i<HISTO_LENGTH; i++)
{
if(i==ind1 || i==ind2 || i==ind3)
continue;
for(size_t j=0, jend=rotHist[i].size(); j<jend; j++)
{
2022-03-28 21:20:28 +08:00
vpMatches12[rotHist[i][j]]=static_cast<MapPoint*>(NULL);
2021-08-09 19:34:51 +08:00
nmatches--;
}
}
}
return nmatches;
}
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchForTriangulation(KeyFrame *pKF1, KeyFrame *pKF2,
vector<pair<size_t, size_t> > &vMatchedPairs, const bool bOnlyStereo, const bool bCoarse)
2020-12-01 11:58:17 +08:00
{
const DBoW2::FeatureVector &vFeatVec1 = pKF1->mFeatVec;
const DBoW2::FeatureVector &vFeatVec2 = pKF2->mFeatVec;
//Compute epipole in second image
2022-04-07 23:28:17 +08:00
// Step 1 计算KF1的相机中心在KF2图像平面的二维像素坐标
2022-03-28 21:20:28 +08:00
Sophus::SE3f T1w = pKF1->GetPose();
Sophus::SE3f T2w = pKF2->GetPose();
Sophus::SE3f Tw2 = pKF2->GetPoseInverse(); // for convenience
Eigen::Vector3f Cw = pKF1->GetCameraCenter();
Eigen::Vector3f C2 = T2w * Cw;
Eigen::Vector2f ep = pKF2->mpCamera->project(C2);
Sophus::SE3f T12;
Sophus::SE3f Tll, Tlr, Trl, Trr;
Eigen::Matrix3f R12; // for fastest computation
Eigen::Vector3f t12; // for fastest computation
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
GeometricCamera* pCamera1 = pKF1->mpCamera, *pCamera2 = pKF2->mpCamera;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(!pKF1->mpCamera2 && !pKF2->mpCamera2){
T12 = T1w * Tw2;
R12 = T12.rotationMatrix();
t12 = T12.translation();
}
else{
Sophus::SE3f Tr1w = pKF1->GetRightPose();
Sophus::SE3f Twr2 = pKF2->GetRightPoseInverse();
Tll = T1w * Tw2;
Tlr = T1w * Twr2;
Trl = Tr1w * Tw2;
Trr = Tr1w * Twr2;
}
Eigen::Matrix3f Rll = Tll.rotationMatrix(), Rlr = Tlr.rotationMatrix(), Rrl = Trl.rotationMatrix(), Rrr = Trr.rotationMatrix();
Eigen::Vector3f tll = Tll.translation(), tlr = Tlr.translation(), trl = Trl.translation(), trr = Trr.translation();
2020-12-01 11:58:17 +08:00
// Find matches between not tracked keypoints
// Matching speed-up by ORB Vocabulary
// Compare only ORB that share the same node
2022-04-07 23:28:17 +08:00
2020-12-01 11:58:17 +08:00
int nmatches=0;
2022-04-07 23:28:17 +08:00
// 记录匹配是否成功,避免重复匹配
vector<bool> vbMatched2(pKF2->N,false);
2020-12-01 11:58:17 +08:00
vector<int> vMatches12(pKF1->N,-1);
2022-04-07 23:28:17 +08:00
// 用于统计匹配点对旋转差的直方图
2020-12-01 11:58:17 +08:00
vector<int> rotHist[HISTO_LENGTH];
for(int i=0;i<HISTO_LENGTH;i++)
rotHist[i].reserve(500);
2022-04-07 23:28:17 +08:00
//! 原作者代码是 const float factor = 1.0f/HISTO_LENGTH; 是错误的,更改为下面代码
// const float factor = HISTO_LENGTH/360.0f;
2020-12-01 11:58:17 +08:00
const float factor = 1.0f/HISTO_LENGTH;
2022-04-07 23:28:17 +08:00
// We perform the matching over ORB that belong to the same vocabulary node (at a certain level)
// Step 2 利用BoW加速匹配只对属于同一节点(特定层)的ORB特征进行匹配
// FeatureVector其实就是一个map类那就可以直接获取它的迭代器进行遍历
// FeatureVector的数据结构类似于{(node1,feature_vector1) (node2,feature_vector2)...}
// f1it->first对应node编号f1it->second对应属于该node的所有特特征点编号
2020-12-01 11:58:17 +08:00
DBoW2::FeatureVector::const_iterator f1it = vFeatVec1.begin();
DBoW2::FeatureVector::const_iterator f2it = vFeatVec2.begin();
DBoW2::FeatureVector::const_iterator f1end = vFeatVec1.end();
DBoW2::FeatureVector::const_iterator f2end = vFeatVec2.end();
2022-03-28 21:20:28 +08:00
2022-04-07 23:28:17 +08:00
// Step 2.1遍历pKF1和pKF2中的node节点
2020-12-01 11:58:17 +08:00
while(f1it!=f1end && f2it!=f2end)
{
2022-04-07 23:28:17 +08:00
// 如果f1it和f2it属于同一个node节点才会进行匹配这就是BoW加速匹配原理
2020-12-01 11:58:17 +08:00
if(f1it->first == f2it->first)
{
2022-04-07 23:28:17 +08:00
// Step 2.2遍历属于同一node节点(idf1it->first)下的所有特征点
2020-12-01 11:58:17 +08:00
for(size_t i1=0, iend1=f1it->second.size(); i1<iend1; i1++)
{
2022-04-07 23:28:17 +08:00
// 获取pKF1中属于该node节点的所有特征点索引
2020-12-01 11:58:17 +08:00
const size_t idx1 = f1it->second[i1];
2022-04-07 23:28:17 +08:00
// Step 2.3通过特征点索引idx1在pKF1中取出对应的MapPoint
2020-12-01 11:58:17 +08:00
MapPoint* pMP1 = pKF1->GetMapPoint(idx1);
2022-04-07 23:28:17 +08:00
2020-12-01 11:58:17 +08:00
// If there is already a MapPoint skip
2022-04-07 23:28:17 +08:00
// 由于寻找的是未匹配的特征点所以pMP1应该为NULL
2020-12-01 11:58:17 +08:00
if(pMP1)
2022-03-28 21:20:28 +08:00
{
2020-12-01 11:58:17 +08:00
continue;
2022-03-28 21:20:28 +08:00
}
2022-04-07 23:28:17 +08:00
// 如果mvuRight中的值大于0表示是双目且该特征点有深度值
2022-03-28 21:20:28 +08:00
const bool bStereo1 = (!pKF1->mpCamera2 && pKF1->mvuRight[idx1]>=0);
if(bOnlyStereo)
if(!bStereo1)
continue;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 2.4通过特征点索引idx1在pKF1中取出对应的特征点
2020-12-01 11:58:17 +08:00
const cv::KeyPoint &kp1 = (pKF1 -> NLeft == -1) ? pKF1->mvKeysUn[idx1]
: (idx1 < pKF1 -> NLeft) ? pKF1 -> mvKeys[idx1]
2022-04-07 23:28:17 +08:00
: pKF1 -> mvKeysRight[idx1 - pKF1 -> NLeft];
2020-12-01 11:58:17 +08:00
const bool bRight1 = (pKF1 -> NLeft == -1 || idx1 < pKF1 -> NLeft) ? false
2022-04-07 23:28:17 +08:00
: true;
//if(bRight1) continue;
// 通过特征点索引idx1在pKF1中取出对应的特征点的描述子
2020-12-01 11:58:17 +08:00
const cv::Mat &d1 = pKF1->mDescriptors.row(idx1);
2022-04-07 23:28:17 +08:00
2020-12-01 11:58:17 +08:00
int bestDist = TH_LOW;
int bestIdx2 = -1;
2022-04-07 23:28:17 +08:00
// Step 2.5遍历该node节点下(f2it->first)对应KF2中的所有特征点
2020-12-01 11:58:17 +08:00
for(size_t i2=0, iend2=f2it->second.size(); i2<iend2; i2++)
{
2022-04-07 23:28:17 +08:00
// 获取pKF2中属于该node节点的所有特征点索引
2020-12-01 11:58:17 +08:00
size_t idx2 = f2it->second[i2];
2022-04-07 23:28:17 +08:00
// 通过特征点索引idx2在pKF2中取出对应的MapPoint
2020-12-01 11:58:17 +08:00
MapPoint* pMP2 = pKF2->GetMapPoint(idx2);
2022-04-07 23:28:17 +08:00
2020-12-01 11:58:17 +08:00
// If we have already matched or there is a MapPoint skip
2022-04-07 23:28:17 +08:00
// 如果pKF2当前特征点索引idx2已经被匹配过或者对应的3d点非空那么跳过这个索引idx2
2020-12-01 11:58:17 +08:00
if(vbMatched2[idx2] || pMP2)
continue;
2022-03-28 21:20:28 +08:00
const bool bStereo2 = (!pKF2->mpCamera2 && pKF2->mvuRight[idx2]>=0);
if(bOnlyStereo)
if(!bStereo2)
continue;
2022-04-07 23:28:17 +08:00
// 通过特征点索引idx2在pKF2中取出对应的特征点的描述子
2020-12-01 11:58:17 +08:00
const cv::Mat &d2 = pKF2->mDescriptors.row(idx2);
2022-04-07 23:28:17 +08:00
// Step 2.6 计算idx1与idx2在两个关键帧中对应特征点的描述子距离
2020-12-01 11:58:17 +08:00
const int dist = DescriptorDistance(d1,d2);
2022-04-07 23:28:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist>TH_LOW || dist>bestDist)
2020-12-01 11:58:17 +08:00
continue;
2022-04-07 23:28:17 +08:00
// 通过特征点索引idx2在pKF2中取出对应的特征点
2020-12-01 11:58:17 +08:00
const cv::KeyPoint &kp2 = (pKF2 -> NLeft == -1) ? pKF2->mvKeysUn[idx2]
: (idx2 < pKF2 -> NLeft) ? pKF2 -> mvKeys[idx2]
2022-04-07 23:28:17 +08:00
: pKF2 -> mvKeysRight[idx2 - pKF2 -> NLeft];
2020-12-01 11:58:17 +08:00
const bool bRight2 = (pKF2 -> NLeft == -1 || idx2 < pKF2 -> NLeft) ? false
2022-04-07 23:28:17 +08:00
: true;
//? 为什么双目就不需要判断像素点到极点的距离的判断?
// 因为双目模式下可以左右互匹配恢复三维点
2022-03-28 21:20:28 +08:00
if(!bStereo1 && !bStereo2 && !pKF1->mpCamera2)
{
const float distex = ep(0)-kp2.pt.x;
const float distey = ep(1)-kp2.pt.y;
2022-04-07 23:28:17 +08:00
// Step 2.7 极点e2到kp2的像素距离如果小于阈值th,认为kp2对应的MapPoint距离pKF1相机太近跳过该匹配点对
// 作者根据kp2金字塔尺度因子(scale^nscale=1.2n为层数)定义阈值th
// 金字塔层数从0到7对应距离 sqrt(100*pKF2->mvScaleFactors[kp2.octave]) 是10-20个像素
//? 对这个阈值的有效性持怀疑态度
2022-03-28 21:20:28 +08:00
if(distex*distex+distey*distey<100*pKF2->mvScaleFactors[kp2.octave])
{
continue;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
if(pKF1->mpCamera2 && pKF2->mpCamera2){
if(bRight1 && bRight2){
R12 = Rrr;
t12 = trr;
T12 = Trr;
pCamera1 = pKF1->mpCamera2;
pCamera2 = pKF2->mpCamera2;
}
else if(bRight1 && !bRight2){
R12 = Rrl;
t12 = trl;
T12 = Trl;
pCamera1 = pKF1->mpCamera2;
pCamera2 = pKF2->mpCamera;
}
else if(!bRight1 && bRight2){
R12 = Rlr;
t12 = tlr;
T12 = Tlr;
pCamera1 = pKF1->mpCamera;
pCamera2 = pKF2->mpCamera2;
}
else{
R12 = Rll;
t12 = tll;
T12 = Tll;
pCamera1 = pKF1->mpCamera;
pCamera2 = pKF2->mpCamera;
}
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
// Step 2.8 计算特征点kp2到kp1对应极线的距离是否小于阈值
2022-03-28 21:20:28 +08:00
if(bCoarse || pCamera1->epipolarConstrain(pCamera2,kp1,kp2,R12,t12,pKF1->mvLevelSigma2[kp1.octave],pKF2->mvLevelSigma2[kp2.octave])) // MODIFICATION_2
{
2022-04-07 23:28:17 +08:00
// bestIdx2bestDist 是 kp1 对应 KF2中的最佳匹配点 index及匹配距离
2020-12-01 11:58:17 +08:00
bestIdx2 = idx2;
bestDist = dist;
}
}
2022-04-07 23:28:17 +08:00
2020-12-01 11:58:17 +08:00
if(bestIdx2>=0)
{
const cv::KeyPoint &kp2 = (pKF2 -> NLeft == -1) ? pKF2->mvKeysUn[bestIdx2]
: (bestIdx2 < pKF2 -> NLeft) ? pKF2 -> mvKeys[bestIdx2]
2022-04-07 23:28:17 +08:00
: pKF2 -> mvKeysRight[bestIdx2 - pKF2 -> NLeft];
// 记录匹配结果
2020-12-01 11:58:17 +08:00
vMatches12[idx1]=bestIdx2;
2022-04-07 23:28:17 +08:00
// !记录已经匹配,避免重复匹配。原作者漏掉!可以添加下面代码
// vbMatched2[bestIdx2]=true;
2020-12-01 11:58:17 +08:00
nmatches++;
2022-04-07 23:28:17 +08:00
// 记录旋转差直方图信息
2020-12-01 11:58:17 +08:00
if(mbCheckOrientation)
{
2022-04-07 23:28:17 +08:00
// angle角度表示匹配点对的方向差。
2020-12-01 11:58:17 +08:00
float rot = kp1.angle-kp2.angle;
if(rot<0.0)
rot+=360.0f;
int bin = round(rot*factor);
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
rotHist[bin].push_back(idx1);
}
}
}
f1it++;
f2it++;
}
else if(f1it->first < f2it->first)
{
f1it = vFeatVec1.lower_bound(f2it->first);
}
else
{
f2it = vFeatVec2.lower_bound(f1it->first);
}
}
2022-04-07 23:28:17 +08:00
// Step 3 用旋转差直方图来筛掉错误匹配对
2020-12-01 11:58:17 +08:00
if(mbCheckOrientation)
{
int ind1=-1;
int ind2=-1;
int ind3=-1;
ComputeThreeMaxima(rotHist,HISTO_LENGTH,ind1,ind2,ind3);
for(int i=0; i<HISTO_LENGTH; i++)
{
if(i==ind1 || i==ind2 || i==ind3)
continue;
for(size_t j=0, jend=rotHist[i].size(); j<jend; j++)
2022-04-07 23:28:17 +08:00
{
vbMatched2[vMatches12[rotHist[i][j]]] = false; // !清除匹配关系。原作者漏掉!
2020-12-01 11:58:17 +08:00
vMatches12[rotHist[i][j]]=-1;
nmatches--;
}
}
}
2022-04-07 23:28:17 +08:00
// Step 4 存储匹配关系下标是关键帧1的特征点id存储的是关键帧2的特征点id
2020-12-01 11:58:17 +08:00
vMatchedPairs.clear();
vMatchedPairs.reserve(nmatches);
for(size_t i=0, iend=vMatches12.size(); i<iend; i++)
{
if(vMatches12[i]<0)
continue;
vMatchedPairs.push_back(make_pair(i,vMatches12[i]));
}
2022-03-28 21:20:28 +08:00
return nmatches;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
int ORBmatcher::Fuse(KeyFrame *pKF, const vector<MapPoint *> &vpMapPoints, const float th, const bool bRight)
{
GeometricCamera* pCamera;
Sophus::SE3f Tcw;
Eigen::Vector3f Ow;
if(bRight){
Tcw = pKF->GetRightPose();
Ow = pKF->GetRightCameraCenter();
pCamera = pKF->mpCamera2;
}
else{
Tcw = pKF->GetPose();
Ow = pKF->GetCameraCenter();
pCamera = pKF->mpCamera;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float &fx = pKF->fx;
const float &fy = pKF->fy;
const float &cx = pKF->cx;
const float &cy = pKF->cy;
const float &bf = pKF->mbf;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nFused=0;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int nMPs = vpMapPoints.size();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// For debbuging
int count_notMP = 0, count_bad=0, count_isinKF = 0, count_negdepth = 0, count_notinim = 0, count_dist = 0, count_normal=0, count_notidx = 0, count_thcheck = 0;
2022-04-07 23:28:17 +08:00
// 遍历所有的待投影地图点
2022-03-28 21:20:28 +08:00
for(int i=0; i<nMPs; i++)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// Step 1 判断地图点的有效性
2022-03-28 21:20:28 +08:00
MapPoint* pMP = vpMapPoints[i];
if(!pMP)
{
count_notMP++;
continue;
}
2022-04-07 23:28:17 +08:00
// 地图点无效 或 已经是该帧的地图点(无需融合),跳过
2022-03-28 21:20:28 +08:00
if(pMP->isBad())
{
count_bad++;
continue;
}
else if(pMP->IsInKeyFrame(pKF))
{
count_isinKF++;
continue;
}
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 将地图点变换到关键帧的相机坐标系下
2022-03-28 21:20:28 +08:00
Eigen::Vector3f p3Dw = pMP->GetWorldPos();
Eigen::Vector3f p3Dc = Tcw * p3Dw;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be positive
2022-04-07 23:28:17 +08:00
// 深度值为负,跳过
2022-03-28 21:20:28 +08:00
if(p3Dc(2)<0.0f)
{
count_negdepth++;
continue;
}
2022-04-07 23:28:17 +08:00
// Step 2 得到地图点投影到关键帧的图像坐标
2022-03-28 21:20:28 +08:00
const float invz = 1/p3Dc(2);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const Eigen::Vector2f uv = pCamera->project(p3Dc);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Point must be inside the image
2022-04-07 23:28:17 +08:00
// 投影点需要在有效范围内
2022-03-28 21:20:28 +08:00
if(!pKF->IsInImage(uv(0),uv(1)))
{
count_notinim++;
continue;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float ur = uv(0)-bf*invz;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float maxDistance = pMP->GetMaxDistanceInvariance();
const float minDistance = pMP->GetMinDistanceInvariance();
Eigen::Vector3f PO = p3Dw-Ow;
const float dist3D = PO.norm();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be inside the scale pyramid of the image
2022-04-07 23:28:17 +08:00
// Step 3 地图点到关键帧相机光心距离需满足在有效范围内
2022-03-28 21:20:28 +08:00
if(dist3D<minDistance || dist3D>maxDistance) {
count_dist++;
continue;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Viewing angle must be less than 60 deg
2022-04-07 23:28:17 +08:00
// Step 4 地图点的平均观测方向正视程度要小于60°
2022-03-28 21:20:28 +08:00
Eigen::Vector3f Pn = pMP->GetNormal();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(PO.dot(Pn)<0.5*dist3D)
{
count_normal++;
continue;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nPredictedLevel = pMP->PredictScale(dist3D,pKF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a radius
2022-04-07 23:28:17 +08:00
// 根据MapPoint的深度确定尺度从而确定搜索范围
2022-03-28 21:20:28 +08:00
const float radius = th*pKF->mvScaleFactors[nPredictedLevel];
2022-04-07 23:28:17 +08:00
// Step 5 在投影点附近搜索窗口内找到候选匹配点的索引
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices = pKF->GetFeaturesInArea(uv(0),uv(1),radius,bRight);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices.empty())
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
count_notidx++;
continue;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
// Match to the most similar keypoint in the radius
2022-04-07 23:28:17 +08:00
// Step 6 遍历寻找最佳匹配点(最小描述子距离)
2022-03-28 21:20:28 +08:00
const cv::Mat dMP = pMP->GetDescriptor();
int bestDist = 256;
int bestIdx = -1;
for(vector<size_t>::const_iterator vit=vIndices.begin(), vend=vIndices.end(); vit!=vend; vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
size_t idx = *vit;
const cv::KeyPoint &kp = (pKF -> NLeft == -1) ? pKF->mvKeysUn[idx]
2022-04-07 23:28:17 +08:00
: (!bRight) ? pKF -> mvKeys[idx]
: pKF -> mvKeysRight[idx];
2022-03-28 21:20:28 +08:00
const int &kpLevel= kp.octave;
2022-04-07 23:28:17 +08:00
// 金字塔层级要接近(同一层或小一层),否则跳过
2022-03-28 21:20:28 +08:00
if(kpLevel<nPredictedLevel-1 || kpLevel>nPredictedLevel)
2020-12-01 11:58:17 +08:00
continue;
2022-04-07 23:28:17 +08:00
// 计算投影点与候选匹配特征点的距离,如果偏差很大,直接跳过
2022-03-28 21:20:28 +08:00
if(pKF->mvuRight[idx]>=0)
{
// Check reprojection error in stereo
2022-04-07 23:28:17 +08:00
// 双目情况
2022-03-28 21:20:28 +08:00
const float &kpx = kp.pt.x;
const float &kpy = kp.pt.y;
const float &kpr = pKF->mvuRight[idx];
const float ex = uv(0)-kpx;
const float ey = uv(1)-kpy;
2022-04-07 23:28:17 +08:00
// 右目数据的偏差也要考虑进去
2022-03-28 21:20:28 +08:00
const float er = ur-kpr;
const float e2 = ex*ex+ey*ey+er*er;
2022-04-07 23:28:17 +08:00
//自由度为3, 误差小于1个像素,这种事情95%发生的概率对应卡方检验阈值为7.82
2022-03-28 21:20:28 +08:00
if(e2*pKF->mvInvLevelSigma2[kpLevel]>7.8)
continue;
}
else
{
2022-04-07 23:28:17 +08:00
// 单目情况
2022-03-28 21:20:28 +08:00
const float &kpx = kp.pt.x;
const float &kpy = kp.pt.y;
const float ex = uv(0)-kpx;
const float ey = uv(1)-kpy;
const float e2 = ex*ex+ey*ey;
2022-04-07 23:28:17 +08:00
// 自由度为2的卡方检验阈值5.99(假设测量有一个像素的偏差)
2022-03-28 21:20:28 +08:00
if(e2*pKF->mvInvLevelSigma2[kpLevel]>5.99)
continue;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(bRight) idx += pKF->NLeft;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &dKF = pKF->mDescriptors.row(idx);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(dMP,dKF);
2022-04-07 23:28:17 +08:00
if(dist<bestDist)// 找MapPoint在该区域最佳匹配的特征点
2022-03-28 21:20:28 +08:00
{
bestDist = dist;
bestIdx = idx;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
// If there is already a MapPoint replace otherwise add new measurement
2022-04-07 23:28:17 +08:00
// Step 7 找到投影点对应的最佳匹配特征点,根据是否存在地图点来融合
// 最佳匹配距离要小于阈值
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_LOW)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
MapPoint* pMPinKF = pKF->GetMapPoint(bestIdx);
if(pMPinKF)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// 如果最佳匹配点有对应有效地图点,选择被观测次数最多的那个替换
2022-03-28 21:20:28 +08:00
if(!pMPinKF->isBad())
{
if(pMPinKF->Observations()>pMP->Observations())
pMP->Replace(pMPinKF);
else
pMPinKF->Replace(pMP);
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
else
{
2022-04-07 23:28:17 +08:00
// 如果最佳匹配点没有对应地图点,添加观测信息
2022-03-28 21:20:28 +08:00
pMP->AddObservation(pKF,bestIdx);
pKF->AddMapPoint(pMP,bestIdx);
}
nFused++;
2020-12-01 11:58:17 +08:00
}
else
2022-03-28 21:20:28 +08:00
count_thcheck++;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
return nFused;
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
/**
* @brief 使
*
* @param[in] pKF
* @param[in] Scw Sim3 Sim
* @param[in] vpPoints
* @param[in] th
* @param[out] vpReplacePoint
* @return int
*/
2022-03-28 21:20:28 +08:00
int ORBmatcher::Fuse(KeyFrame *pKF, Sophus::Sim3f &Scw, const vector<MapPoint *> &vpPoints, float th, vector<MapPoint *> &vpReplacePoint)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
// Get Calibration Parameters for later projection
const float &fx = pKF->fx;
const float &fy = pKF->fy;
const float &cx = pKF->cx;
const float &cy = pKF->cy;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Decompose Scw
2022-04-07 23:28:17 +08:00
// Step 1 将Sim3转化为SE3并分解
2022-03-28 21:20:28 +08:00
Sophus::SE3f Tcw = Sophus::SE3f(Scw.rotationMatrix(),Scw.translation()/Scw.scale());
Eigen::Vector3f Ow = Tcw.inverse().translation();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Set of MapPoints already found in the KeyFrame
2022-04-07 23:28:17 +08:00
// 当前帧已有的匹配地图点
2022-03-28 21:20:28 +08:00
const set<MapPoint*> spAlreadyFound = pKF->GetMapPoints();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int nFused=0;
2022-04-07 23:28:17 +08:00
// 与当前帧闭环匹配上的关键帧及其共视关键帧组成的地图点
2022-03-28 21:20:28 +08:00
const int nPoints = vpPoints.size();
// For each candidate MapPoint project and match
2022-04-07 23:28:17 +08:00
// 遍历所有的地图点
2022-03-28 21:20:28 +08:00
for(int iMP=0; iMP<nPoints; iMP++)
{
MapPoint* pMP = vpPoints[iMP];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Discard Bad MapPoints and already found
2022-04-07 23:28:17 +08:00
// 地图点无效 或 已经是该帧的地图点(无需融合),跳过
2022-03-28 21:20:28 +08:00
if(pMP->isBad() || spAlreadyFound.count(pMP))
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Get 3D Coords.
2022-04-07 23:28:17 +08:00
// Step 2 地图点变换到当前相机坐标系下
2022-03-28 21:20:28 +08:00
Eigen::Vector3f p3Dw = pMP->GetWorldPos();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Transform into Camera Coords.
Eigen::Vector3f p3Dc = Tcw * p3Dw;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be positive
if(p3Dc(2)<0.0f)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Project into Image
2022-04-07 23:28:17 +08:00
// Step 3 得到地图点投影到当前帧的图像坐标
2022-03-28 21:20:28 +08:00
const Eigen::Vector2f uv = pKF->mpCamera->project(p3Dc);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Point must be inside the image
2022-04-07 23:28:17 +08:00
// 投影点必须在图像范围内
2022-03-28 21:20:28 +08:00
if(!pKF->IsInImage(uv(0),uv(1)))
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be inside the scale pyramid of the image
2022-04-07 23:28:17 +08:00
// Step 4 根据距离是否在图像合理金字塔尺度范围内和观测角度是否小于60度判断该地图点是否有效
2022-03-28 21:20:28 +08:00
const float maxDistance = pMP->GetMaxDistanceInvariance();
const float minDistance = pMP->GetMinDistanceInvariance();
Eigen::Vector3f PO = p3Dw-Ow;
const float dist3D = PO.norm();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist3D<minDistance || dist3D>maxDistance)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Viewing angle must be less than 60 deg
Eigen::Vector3f Pn = pMP->GetNormal();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(PO.dot(Pn)<0.5*dist3D)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Compute predicted scale level
const int nPredictedLevel = pMP->PredictScale(dist3D,pKF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a radius
2022-04-07 23:28:17 +08:00
// 计算搜索范围
2022-03-28 21:20:28 +08:00
const float radius = th*pKF->mvScaleFactors[nPredictedLevel];
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 5 在当前帧内搜索匹配候选点
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices = pKF->GetFeaturesInArea(uv(0),uv(1),radius);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices.empty())
2020-12-01 11:58:17 +08:00
continue;
2022-03-28 21:20:28 +08:00
// Match to the most similar keypoint in the radius
2022-04-07 23:28:17 +08:00
// Step 6 寻找最佳匹配点(没有用到次佳匹配的比例)
2022-03-28 21:20:28 +08:00
const cv::Mat dMP = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist = INT_MAX;
int bestIdx = -1;
for(vector<size_t>::const_iterator vit=vIndices.begin(); vit!=vIndices.end(); vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
const size_t idx = *vit;
const int &kpLevel = pKF->mvKeysUn[idx].octave;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(kpLevel<nPredictedLevel-1 || kpLevel>nPredictedLevel)
continue;
const cv::Mat &dKF = pKF->mDescriptors.row(idx);
int dist = DescriptorDistance(dMP,dKF);
if(dist<bestDist)
{
bestDist = dist;
bestIdx = idx;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
// If there is already a MapPoint replace otherwise add new measurement
2022-04-07 23:28:17 +08:00
// Step 7 替换或新增地图点
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_LOW)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
MapPoint* pMPinKF = pKF->GetMapPoint(bestIdx);
if(pMPinKF)
{
2022-04-07 23:28:17 +08:00
// 如果这个地图点已经存在,则记录要替换信息
// 这里不能直接替换原因是需要对地图点加锁后才能替换否则可能会crash。所以先记录在加锁后替换
2022-03-28 21:20:28 +08:00
if(!pMPinKF->isBad())
vpReplacePoint[iMP] = pMPinKF;
}
else
{
2022-04-07 23:28:17 +08:00
// 如果这个地图点不存在,直接添加
2022-03-28 21:20:28 +08:00
pMP->AddObservation(pKF,bestIdx);
pKF->AddMapPoint(pMP,bestIdx);
}
nFused++;
2020-12-01 11:58:17 +08:00
}
}
2022-04-07 23:28:17 +08:00
// 融合(替换和新增)的地图点数目
2022-03-28 21:20:28 +08:00
return nFused;
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
/**
* @brief Sim3
* 使SearchByBoW
* @param[in] pKF1
* @param[in] pKF2
* @param[in] vpMatches12 ipKF1 vpMatches12[i]null
* @param[in] s12 pKF2 pKF1 Sim
* @param[in] R12 pKF2 pKF1 Sim
* @param[in] t12 pKF2 pKF1 Sim
* @param[in] th
* @return int
*/
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchBySim3(KeyFrame* pKF1, KeyFrame* pKF2, std::vector<MapPoint *> &vpMatches12, const Sophus::Sim3f &S12, const float th)
2020-12-01 11:58:17 +08:00
{
2022-04-07 23:28:17 +08:00
// Step 1 准备工作
2022-03-28 21:20:28 +08:00
const float &fx = pKF1->fx;
const float &fy = pKF1->fy;
const float &cx = pKF1->cx;
const float &cy = pKF1->cy;
2022-04-07 23:28:17 +08:00
// 从world到camera1的变换
// 从world到camera2的变换
2022-03-28 21:20:28 +08:00
// Camera 1 & 2 from world
Sophus::SE3f T1w = pKF1->GetPose();
Sophus::SE3f T2w = pKF2->GetPose();
//Transformation between cameras
2022-04-07 23:28:17 +08:00
// Sim3 的逆
2022-03-28 21:20:28 +08:00
Sophus::Sim3f S21 = S12.inverse();
2022-04-07 23:28:17 +08:00
// 取出关键帧中的地图点
2022-03-28 21:20:28 +08:00
const vector<MapPoint*> vpMapPoints1 = pKF1->GetMapPointMatches();
const int N1 = vpMapPoints1.size();
const vector<MapPoint*> vpMapPoints2 = pKF2->GetMapPointMatches();
const int N2 = vpMapPoints2.size();
2022-04-07 23:28:17 +08:00
// pKF1中特征点的匹配情况有匹配为true否则false
2022-03-28 21:20:28 +08:00
vector<bool> vbAlreadyMatched1(N1,false);
2022-04-07 23:28:17 +08:00
// pKF2中特征点的匹配情况有匹配为true否则false
2022-03-28 21:20:28 +08:00
vector<bool> vbAlreadyMatched2(N2,false);
2022-04-07 23:28:17 +08:00
// Step 2记录已经匹配的特征点
2022-03-28 21:20:28 +08:00
for(int i=0; i<N1; i++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
MapPoint* pMP = vpMatches12[i];
if(pMP)
{
2022-04-07 23:28:17 +08:00
// pKF1中第i个特征点已经匹配成功
2022-03-28 21:20:28 +08:00
vbAlreadyMatched1[i]=true;
2022-04-07 23:28:17 +08:00
// 得到该地图点在关键帧pkF2 中的id
2022-03-28 21:20:28 +08:00
int idx2 = get<0>(pMP->GetIndexInKeyFrame(pKF2));
if(idx2>=0 && idx2<N2)
2022-04-07 23:28:17 +08:00
// pKF2中第idx2个特征点在pKF1中有匹配
2022-03-28 21:20:28 +08:00
vbAlreadyMatched2[idx2]=true;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
vector<int> vnMatch1(N1,-1);
vector<int> vnMatch2(N2,-1);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Transform from KF1 to KF2 and search
2022-04-07 23:28:17 +08:00
// Step 3通过Sim变换寻找 pKF1 中特征点和 pKF2 中的新的匹配
// 之前使用SearchByBoW进行特征点匹配时会有漏匹配
2022-03-28 21:20:28 +08:00
for(int i1=0; i1<N1; i1++)
{
MapPoint* pMP = vpMapPoints1[i1];
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 该特征点已经有匹配点了,直接跳过
2022-03-28 21:20:28 +08:00
if(!pMP || vbAlreadyMatched1[i1])
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(pMP->isBad())
continue;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 3.1通过Sim变换将pKF1的地图点投影到pKF2中的图像坐标
2022-03-28 21:20:28 +08:00
Eigen::Vector3f p3Dw = pMP->GetWorldPos();
2022-04-07 23:28:17 +08:00
// 把pKF1的地图点从world坐标系变换到camera1坐标系
2022-03-28 21:20:28 +08:00
Eigen::Vector3f p3Dc1 = T1w * p3Dw;
2022-04-07 23:28:17 +08:00
// 再通过Sim3将该地图点从camera1变换到camera2坐标系
2022-03-28 21:20:28 +08:00
Eigen::Vector3f p3Dc2 = S21 * p3Dc1;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be positive
if(p3Dc2(2)<0.0)
continue;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 投影到camera2图像坐标 (u,v)
2022-03-28 21:20:28 +08:00
const float invz = 1.0/p3Dc2(2);
const float x = p3Dc2(0)*invz;
const float y = p3Dc2(1)*invz;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float u = fx*x+cx;
const float v = fy*y+cy;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Point must be inside the image
if(!pKF2->IsInImage(u,v))
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float maxDistance = pMP->GetMaxDistanceInvariance();
const float minDistance = pMP->GetMinDistanceInvariance();
const float dist3D = p3Dc2.norm();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be inside the scale invariance region
if(dist3D<minDistance || dist3D>maxDistance )
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Compute predicted octave
2022-04-07 23:28:17 +08:00
// Step 3.2:预测投影的点在图像金字塔哪一层
2022-03-28 21:20:28 +08:00
const int nPredictedLevel = pMP->PredictScale(dist3D,pKF2);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a radius
2022-04-07 23:28:17 +08:00
// 计算特征点搜索半径
2022-03-28 21:20:28 +08:00
const float radius = th*pKF2->mvScaleFactors[nPredictedLevel];
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 3.3:搜索该区域内的所有候选匹配特征点
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices = pKF2->GetFeaturesInArea(u,v,radius);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices.empty())
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Match to the most similar keypoint in the radius
const cv::Mat dMP = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist = INT_MAX;
int bestIdx = -1;
2022-04-07 23:28:17 +08:00
// Step 3.4:遍历所有候选特征点,寻找最佳匹配点(并未使用次佳最佳比例约束)
2022-03-28 21:20:28 +08:00
for(vector<size_t>::const_iterator vit=vIndices.begin(), vend=vIndices.end(); vit!=vend; vit++)
{
const size_t idx = *vit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::KeyPoint &kp = pKF2->mvKeysUn[idx];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(kp.octave<nPredictedLevel-1 || kp.octave>nPredictedLevel)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &dKF = pKF2->mDescriptors.row(idx);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(dMP,dKF);
if(dist<bestDist)
{
bestDist = dist;
bestIdx = idx;
}
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_HIGH)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
vnMatch1[i1]=bestIdx;
2020-12-01 11:58:17 +08:00
}
}
2022-04-07 23:28:17 +08:00
// Transform from KF2 to KF1 and search
// Step 4通过Sim变换寻找 pKF2 中特征点和 pKF1 中的新的匹配
// 具体步骤同上
2022-03-28 21:20:28 +08:00
for(int i2=0; i2<N2; i2++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
MapPoint* pMP = vpMapPoints2[i2];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(!pMP || vbAlreadyMatched2[i2])
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(pMP->isBad())
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
Eigen::Vector3f p3Dw = pMP->GetWorldPos();
Eigen::Vector3f p3Dc2 = T2w * p3Dw;
Eigen::Vector3f p3Dc1 = S12 * p3Dc2;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be positive
if(p3Dc1(2)<0.0)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float invz = 1.0/p3Dc1(2);
const float x = p3Dc1(0)*invz;
const float y = p3Dc1(1)*invz;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float u = fx*x+cx;
const float v = fy*y+cy;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Point must be inside the image
if(!pKF1->IsInImage(u,v))
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float maxDistance = pMP->GetMaxDistanceInvariance();
const float minDistance = pMP->GetMinDistanceInvariance();
const float dist3D = p3Dc1.norm();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be inside the scale pyramid of the image
if(dist3D<minDistance || dist3D>maxDistance)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Compute predicted octave
const int nPredictedLevel = pMP->PredictScale(dist3D,pKF1);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a radius of 2.5*sigma(ScaleLevel)
const float radius = th*pKF1->mvScaleFactors[nPredictedLevel];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices = pKF1->GetFeaturesInArea(u,v,radius);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices.empty())
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Match to the most similar keypoint in the radius
const cv::Mat dMP = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist = INT_MAX;
int bestIdx = -1;
for(vector<size_t>::const_iterator vit=vIndices.begin(), vend=vIndices.end(); vit!=vend; vit++)
{
const size_t idx = *vit;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::KeyPoint &kp = pKF1->mvKeysUn[idx];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(kp.octave<nPredictedLevel-1 || kp.octave>nPredictedLevel)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat &dKF = pKF1->mDescriptors.row(idx);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const int dist = DescriptorDistance(dMP,dKF);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(dist<bestDist)
{
bestDist = dist;
bestIdx = idx;
}
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(bestDist<=TH_HIGH)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
vnMatch2[i2]=bestIdx;
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
// Check agreement
2022-04-07 23:28:17 +08:00
// Step 5 一致性检查,只有在两次互相匹配中都出现才能够认为是可靠的匹配
2022-03-28 21:20:28 +08:00
int nFound = 0;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for(int i1=0; i1<N1; i1++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
int idx2 = vnMatch1[i1];
if(idx2>=0)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
int idx1 = vnMatch2[idx2];
if(idx1==i1)
{
2022-04-07 23:28:17 +08:00
// 更新匹配的地图点
2022-03-28 21:20:28 +08:00
vpMatches12[i1] = vpMapPoints2[idx2];
nFound++;
}
2020-12-01 11:58:17 +08:00
}
}
2022-03-28 21:20:28 +08:00
return nFound;
2020-12-01 11:58:17 +08:00
}
2022-04-07 23:28:17 +08:00
/**
* @brief
*
* Step 1
* Step 2
* Step 3
* Step 4
* Step 5
* Step 6
* Step 7
* @param[in] CurrentFrame
* @param[in] LastFrame
* @param[in] th 715
* @param[in] bMono
* @return int
*/
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchByProjection(Frame &CurrentFrame, const Frame &LastFrame, const float th, const bool bMono)
{
int nmatches = 0;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Rotation Histogram (to check rotation consistency)
2022-04-07 23:28:17 +08:00
// Step 1 建立旋转直方图,用于检测旋转一致性
2022-03-28 21:20:28 +08:00
vector<int> rotHist[HISTO_LENGTH];
for(int i=0;i<HISTO_LENGTH;i++)
rotHist[i].reserve(500);
2022-04-07 23:28:17 +08:00
//! 原作者代码是 const float factor = 1.0f/HISTO_LENGTH; 是错误的,更改为下面代码
// const float factor = HISTO_LENGTH/360.0f;
2022-03-28 21:20:28 +08:00
const float factor = 1.0f/HISTO_LENGTH;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 2 计算当前帧和前一帧的平移向量
//当前帧的相机位姿
2022-03-28 21:20:28 +08:00
const Sophus::SE3f Tcw = CurrentFrame.GetPose();
const Eigen::Vector3f twc = Tcw.inverse().translation();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const Sophus::SE3f Tlw = LastFrame.GetPose();
2022-04-07 23:28:17 +08:00
const Eigen::Vector3f tlc = Tlw * twc;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 判断前进还是后退
const bool bForward = tlc(2)>CurrentFrame.mb && !bMono; // 非单目情况如果Z大于基线则表示相机明显前进
const bool bBackward = -tlc(2)>CurrentFrame.mb && !bMono; // 非单目情况,如果-Z小于基线则表示相机明显后退
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 3 对于前一帧的每一个地图点,通过相机投影模型,得到投影到当前帧的像素坐标
2022-03-28 21:20:28 +08:00
for(int i=0; i<LastFrame.N; i++)
{
MapPoint* pMP = LastFrame.mvpMapPoints[i];
2022-04-07 23:28:17 +08:00
2022-03-28 21:20:28 +08:00
if(pMP)
{
if(!LastFrame.mvbOutlier[i])
{
2022-04-07 23:28:17 +08:00
// 对上一帧有效的MapPoints投影到当前帧坐标系
2022-03-28 21:20:28 +08:00
Eigen::Vector3f x3Dw = pMP->GetWorldPos();
Eigen::Vector3f x3Dc = Tcw * x3Dw;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float xc = x3Dc(0);
const float yc = x3Dc(1);
const float invzc = 1.0/x3Dc(2);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(invzc<0)
continue;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 投影到当前帧中
2022-03-28 21:20:28 +08:00
Eigen::Vector2f uv = CurrentFrame.mpCamera->project(x3Dc);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(uv(0)<CurrentFrame.mnMinX || uv(0)>CurrentFrame.mnMaxX)
continue;
if(uv(1)<CurrentFrame.mnMinY || uv(1)>CurrentFrame.mnMaxY)
continue;
2022-04-07 23:28:17 +08:00
// 认为投影前后地图点的尺度信息不变
2022-03-28 21:20:28 +08:00
int nLastOctave = (LastFrame.Nleft == -1 || i < LastFrame.Nleft) ? LastFrame.mvKeys[i].octave
2022-04-07 23:28:17 +08:00
: LastFrame.mvKeysRight[i - LastFrame.Nleft].octave;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a window. Size depends on scale
2022-04-07 23:28:17 +08:00
// 单目th = 7双目th = 15
float radius = th*CurrentFrame.mvScaleFactors[nLastOctave]; // 尺度越大,搜索范围越大
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 记录候选匹配点的id
2022-03-28 21:20:28 +08:00
vector<size_t> vIndices2;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 4 根据相机的前后前进方向来判断搜索尺度范围。
// 以下可以这么理解例如一个有一定面积的圆点在某个尺度n下它是一个特征点
// 当相机前进时圆点的面积增大在某个尺度m下它是一个特征点由于面积增大则需要在更高的尺度下才能检测出来
// 当相机后退时圆点的面积减小在某个尺度m下它是一个特征点由于面积减小则需要在更低的尺度下才能检测出来
if(bForward) // 前进,则上一帧兴趣点在所在的尺度nLastOctave<=nCurOctave
2022-03-28 21:20:28 +08:00
vIndices2 = CurrentFrame.GetFeaturesInArea(uv(0),uv(1), radius, nLastOctave);
2022-04-07 23:28:17 +08:00
else if(bBackward) // 后退,则上一帧兴趣点在所在的尺度0<=nCurOctave<=nLastOctave
2022-03-28 21:20:28 +08:00
vIndices2 = CurrentFrame.GetFeaturesInArea(uv(0),uv(1), radius, 0, nLastOctave);
2022-04-07 23:28:17 +08:00
else // 在[nLastOctave-1, nLastOctave+1]中搜索
2022-03-28 21:20:28 +08:00
vIndices2 = CurrentFrame.GetFeaturesInArea(uv(0),uv(1), radius, nLastOctave-1, nLastOctave+1);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices2.empty())
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const cv::Mat dMP = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist = 256;
int bestIdx2 = -1;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 5 遍历候选匹配点,寻找距离最小的最佳匹配点
2022-03-28 21:20:28 +08:00
for(vector<size_t>::const_iterator vit=vIndices2.begin(), vend=vIndices2.end(); vit!=vend; vit++)
{
const size_t i2 = *vit;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 如果该特征点已经有对应的MapPoint了,则退出该次循环
2022-03-28 21:20:28 +08:00
if(CurrentFrame.mvpMapPoints[i2])
if(CurrentFrame.mvpMapPoints[i2]->Observations()>0)
continue;
2020-12-01 11:58:17 +08:00
if(CurrentFrame.Nleft == -1 && CurrentFrame.mvuRight[i2]>0)
{
2022-04-07 23:28:17 +08:00
// 双目和rgbd的情况需要保证右图的点也在搜索半径以内
2022-03-28 21:20:28 +08:00
const float ur = uv(0) - CurrentFrame.mbf*invzc;
2020-12-01 11:58:17 +08:00
const float er = fabs(ur - CurrentFrame.mvuRight[i2]);
if(er>radius)
continue;
}
const cv::Mat &d = CurrentFrame.mDescriptors.row(i2);
const int dist = DescriptorDistance(dMP,d);
if(dist<bestDist)
{
bestDist=dist;
bestIdx2=i2;
}
}
2022-04-07 23:28:17 +08:00
// 最佳匹配距离要小于设定阈值
2020-12-01 11:58:17 +08:00
if(bestDist<=TH_HIGH)
{
CurrentFrame.mvpMapPoints[bestIdx2]=pMP;
nmatches++;
2022-04-07 23:28:17 +08:00
// Step 6 计算匹配点旋转角度差所在的直方图
2020-12-01 11:58:17 +08:00
if(mbCheckOrientation)
{
cv::KeyPoint kpLF = (LastFrame.Nleft == -1) ? LastFrame.mvKeysUn[i]
: (i < LastFrame.Nleft) ? LastFrame.mvKeys[i]
: LastFrame.mvKeysRight[i - LastFrame.Nleft];
cv::KeyPoint kpCF = (CurrentFrame.Nleft == -1) ? CurrentFrame.mvKeysUn[bestIdx2]
2022-04-07 23:28:17 +08:00
: (bestIdx2 < CurrentFrame.Nleft) ? CurrentFrame.mvKeys[bestIdx2]
: CurrentFrame.mvKeysRight[bestIdx2 - CurrentFrame.Nleft];
2020-12-01 11:58:17 +08:00
float rot = kpLF.angle-kpCF.angle;
if(rot<0.0)
rot+=360.0f;
int bin = round(rot*factor);
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
rotHist[bin].push_back(bestIdx2);
}
}
if(CurrentFrame.Nleft != -1){
2022-03-28 21:20:28 +08:00
Eigen::Vector3f x3Dr = CurrentFrame.GetRelativePoseTrl() * x3Dc;
Eigen::Vector2f uv = CurrentFrame.mpCamera->project(x3Dr);
2020-12-01 11:58:17 +08:00
int nLastOctave = (LastFrame.Nleft == -1 || i < LastFrame.Nleft) ? LastFrame.mvKeys[i].octave
2022-04-07 23:28:17 +08:00
: LastFrame.mvKeysRight[i - LastFrame.Nleft].octave;
2020-12-01 11:58:17 +08:00
// Search in a window. Size depends on scale
float radius = th*CurrentFrame.mvScaleFactors[nLastOctave];
vector<size_t> vIndices2;
if(bForward)
2022-03-28 21:20:28 +08:00
vIndices2 = CurrentFrame.GetFeaturesInArea(uv(0),uv(1), radius, nLastOctave, -1,true);
2020-12-01 11:58:17 +08:00
else if(bBackward)
2022-03-28 21:20:28 +08:00
vIndices2 = CurrentFrame.GetFeaturesInArea(uv(0),uv(1), radius, 0, nLastOctave, true);
2020-12-01 11:58:17 +08:00
else
2022-03-28 21:20:28 +08:00
vIndices2 = CurrentFrame.GetFeaturesInArea(uv(0),uv(1), radius, nLastOctave-1, nLastOctave+1, true);
2020-12-01 11:58:17 +08:00
const cv::Mat dMP = pMP->GetDescriptor();
int bestDist = 256;
int bestIdx2 = -1;
for(vector<size_t>::const_iterator vit=vIndices2.begin(), vend=vIndices2.end(); vit!=vend; vit++)
{
const size_t i2 = *vit;
if(CurrentFrame.mvpMapPoints[i2 + CurrentFrame.Nleft])
if(CurrentFrame.mvpMapPoints[i2 + CurrentFrame.Nleft]->Observations()>0)
continue;
const cv::Mat &d = CurrentFrame.mDescriptors.row(i2 + CurrentFrame.Nleft);
const int dist = DescriptorDistance(dMP,d);
if(dist<bestDist)
{
bestDist=dist;
bestIdx2=i2;
}
}
if(bestDist<=TH_HIGH)
{
CurrentFrame.mvpMapPoints[bestIdx2 + CurrentFrame.Nleft]=pMP;
nmatches++;
if(mbCheckOrientation)
{
cv::KeyPoint kpLF = (LastFrame.Nleft == -1) ? LastFrame.mvKeysUn[i]
: (i < LastFrame.Nleft) ? LastFrame.mvKeys[i]
: LastFrame.mvKeysRight[i - LastFrame.Nleft];
cv::KeyPoint kpCF = CurrentFrame.mvKeysRight[bestIdx2];
float rot = kpLF.angle-kpCF.angle;
if(rot<0.0)
rot+=360.0f;
int bin = round(rot*factor);
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
rotHist[bin].push_back(bestIdx2 + CurrentFrame.Nleft);
}
}
}
}
}
}
//Apply rotation consistency
2022-04-07 23:28:17 +08:00
// Step 7 进行旋转一致检测,剔除不一致的匹配
2020-12-01 11:58:17 +08:00
if(mbCheckOrientation)
{
int ind1=-1;
int ind2=-1;
int ind3=-1;
ComputeThreeMaxima(rotHist,HISTO_LENGTH,ind1,ind2,ind3);
for(int i=0; i<HISTO_LENGTH; i++)
{
2022-04-07 23:28:17 +08:00
// 对于数量不是前3个的点对剔除
2020-12-01 11:58:17 +08:00
if(i!=ind1 && i!=ind2 && i!=ind3)
{
for(size_t j=0, jend=rotHist[i].size(); j<jend; j++)
{
CurrentFrame.mvpMapPoints[rotHist[i][j]]=static_cast<MapPoint*>(NULL);
nmatches--;
}
}
}
}
return nmatches;
}
2022-04-07 23:28:17 +08:00
/**
* @brief ,
*
* @param[in] CurrentFrame
* @param[in] pKF
* @param[in] sAlreadyFound PNP
* @param[in] th
* @param[in] ORBdist ORB
* @return int
*/
2022-03-28 21:20:28 +08:00
int ORBmatcher::SearchByProjection(Frame &CurrentFrame, KeyFrame *pKF, const set<MapPoint*> &sAlreadyFound, const float th , const int ORBdist)
{
int nmatches = 0;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const Sophus::SE3f Tcw = CurrentFrame.GetPose();
Eigen::Vector3f Ow = Tcw.inverse().translation();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Rotation Histogram (to check rotation consistency)
2022-04-07 23:28:17 +08:00
// Step 1 建立旋转直方图,用于检测旋转一致性
2022-03-28 21:20:28 +08:00
vector<int> rotHist[HISTO_LENGTH];
for(int i=0;i<HISTO_LENGTH;i++)
rotHist[i].reserve(500);
const float factor = 1.0f/HISTO_LENGTH;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const vector<MapPoint*> vpMPs = pKF->GetMapPointMatches();
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 2 遍历关键帧中的每个地图点,通过相机投影模型,得到投影到当前帧的像素坐标
2022-03-28 21:20:28 +08:00
for(size_t i=0, iend=vpMPs.size(); i<iend; i++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
MapPoint* pMP = vpMPs[i];
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(pMP)
{
2022-04-07 23:28:17 +08:00
// 地图点存在 并且 不在已有地图点集合里
2022-03-28 21:20:28 +08:00
if(!pMP->isBad() && !sAlreadyFound.count(pMP))
{
//Project
Eigen::Vector3f x3Dw = pMP->GetWorldPos();
Eigen::Vector3f x3Dc = Tcw * x3Dw;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const Eigen::Vector2f uv = CurrentFrame.mpCamera->project(x3Dc);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(uv(0)<CurrentFrame.mnMinX || uv(0)>CurrentFrame.mnMaxX)
continue;
if(uv(1)<CurrentFrame.mnMinY || uv(1)>CurrentFrame.mnMaxY)
continue;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Compute predicted scale level
Eigen::Vector3f PO = x3Dw-Ow;
float dist3D = PO.norm();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
const float maxDistance = pMP->GetMaxDistanceInvariance();
const float minDistance = pMP->GetMinDistanceInvariance();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Depth must be inside the scale pyramid of the image
if(dist3D<minDistance || dist3D>maxDistance)
continue;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
//预测尺度
2022-03-28 21:20:28 +08:00
int nPredictedLevel = pMP->PredictScale(dist3D,&CurrentFrame);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
// Search in a window
2022-04-07 23:28:17 +08:00
// 搜索半径和尺度相关
2022-03-28 21:20:28 +08:00
const float radius = th*CurrentFrame.mvScaleFactors[nPredictedLevel];
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// Step 3 搜索候选匹配点
2022-03-28 21:20:28 +08:00
const vector<size_t> vIndices2 = CurrentFrame.GetFeaturesInArea(uv(0), uv(1), radius, nPredictedLevel-1, nPredictedLevel+1);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
if(vIndices2.empty())
2020-12-01 11:58:17 +08:00
continue;
2022-03-28 21:20:28 +08:00
const cv::Mat dMP = pMP->GetDescriptor();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int bestDist = 256;
int bestIdx2 = -1;
2022-04-07 23:28:17 +08:00
// Step 4 遍历候选匹配点,寻找距离最小的最佳匹配点
2022-03-28 21:20:28 +08:00
for(vector<size_t>::const_iterator vit=vIndices2.begin(); vit!=vIndices2.end(); vit++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
const size_t i2 = *vit;
if(CurrentFrame.mvpMapPoints[i2])
continue;
const cv::Mat &d = CurrentFrame.mDescriptors.row(i2);
const int dist = DescriptorDistance(dMP,d);
if(dist<bestDist)
{
bestDist=dist;
bestIdx2=i2;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
if(bestDist<=ORBdist)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
CurrentFrame.mvpMapPoints[bestIdx2]=pMP;
nmatches++;
2022-04-07 23:28:17 +08:00
// Step 5 计算匹配点旋转角度差所在的直方图
2022-03-28 21:20:28 +08:00
if(mbCheckOrientation)
{
float rot = pKF->mvKeysUn[i].angle-CurrentFrame.mvKeysUn[bestIdx2].angle;
if(rot<0.0)
rot+=360.0f;
int bin = round(rot*factor);
if(bin==HISTO_LENGTH)
bin=0;
assert(bin>=0 && bin<HISTO_LENGTH);
rotHist[bin].push_back(bestIdx2);
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
}
2020-12-01 11:58:17 +08:00
}
}
2022-04-07 23:28:17 +08:00
// Step 6 进行旋转一致检测,剔除不一致的匹配
2022-03-28 21:20:28 +08:00
if(mbCheckOrientation)
{
int ind1=-1;
int ind2=-1;
int ind3=-1;
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
ComputeThreeMaxima(rotHist,HISTO_LENGTH,ind1,ind2,ind3);
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
for(int i=0; i<HISTO_LENGTH; i++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
if(i!=ind1 && i!=ind2 && i!=ind3)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
for(size_t j=0, jend=rotHist[i].size(); j<jend; j++)
{
CurrentFrame.mvpMapPoints[rotHist[i][j]]=NULL;
nmatches--;
}
2020-12-01 11:58:17 +08:00
}
}
}
2022-03-28 21:20:28 +08:00
return nmatches;
}
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
/**
* @brief bin
*
* @param[in] histo
* @param[in] L
* @param[in & out] ind1 bin
* @param[in & out] ind2 bin
* @param[in & out] ind3 bin
*/
2022-03-28 21:20:28 +08:00
void ORBmatcher::ComputeThreeMaxima(vector<int>* histo, const int L, int &ind1, int &ind2, int &ind3)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
int max1=0;
int max2=0;
int max3=0;
for(int i=0; i<L; i++)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
const int s = histo[i].size();
if(s>max1)
{
max3=max2;
max2=max1;
max1=s;
ind3=ind2;
ind2=ind1;
ind1=i;
}
else if(s>max2)
{
max3=max2;
max2=s;
ind3=ind2;
ind2=i;
}
else if(s>max3)
{
max3=s;
ind3=i;
}
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
2022-04-07 23:28:17 +08:00
// 如果差距太大了,说明次优的非常不好,这里就索性放弃了,都置为-1
2022-03-28 21:20:28 +08:00
if(max2<0.1f*(float)max1)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
ind2=-1;
ind3=-1;
2020-12-01 11:58:17 +08:00
}
2022-03-28 21:20:28 +08:00
else if(max3<0.1f*(float)max1)
2020-12-01 11:58:17 +08:00
{
2022-03-28 21:20:28 +08:00
ind3=-1;
2020-12-01 11:58:17 +08:00
}
}
2022-04-07 23:28:17 +08:00
// Bit set count operation from
// Hamming distance两个二进制串之间的汉明距离指的是其不同位数的个数
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2022-03-28 21:20:28 +08:00
int ORBmatcher::DescriptorDistance(const cv::Mat &a, const cv::Mat &b)
{
const int *pa = a.ptr<int32_t>();
const int *pb = b.ptr<int32_t>();
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
int dist=0;
2020-12-01 11:58:17 +08:00
2022-04-07 23:28:17 +08:00
// 8*32=256bit
2022-03-28 21:20:28 +08:00
for(int i=0; i<8; i++, pa++, pb++)
{
2022-04-07 23:28:17 +08:00
unsigned int v = *pa ^ *pb; // 相等为0,不等为1
// 下面的操作就是计算其中bit为1的个数了,这个操作看上面的链接就好
// 其实我觉得也还阔以直接使用8bit的查找表,然后做32次寻址操作就完成了;不过缺点是没有利用好CPU的字长
2022-03-28 21:20:28 +08:00
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
dist += (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
}
2020-12-01 11:58:17 +08:00
2022-03-28 21:20:28 +08:00
return dist;
2020-12-01 11:58:17 +08:00
}
} //namespace ORB_SLAM