【slam十四讲第二版】【课本例题代码向】【第十一讲~回环检测】【DBoW3的安装】【创建字典】【相似度检测】【增加字典规模】
- 0 前言
- 1 DBoW3的安装
- 2 创建字典
-
- 2.1 feature_training.cpp
- 2.2 CMakeLists.txt
- 2.3 输出
- 3 相似度的计算
-
- 3.1 loop_closure.cpp
- 3.2 CMakeLists.txt
- 3.3 输出
- 4 增加字典规模
-
- 4.1 gen_vocab_large.cpp
- 4.2 CMakeLists.txt
- 4.3 输出
- 4.4 使用该字典进行回环
0 前言
- 《视觉SLAM十四讲 第二版》笔记及课后习题(第十一讲)
- 视觉SLAM十四讲CH11代码解析及课后习题详解
1 DBoW3的安装
- 首先获取安装包,事实证明,使用高博的gaoxiang12/slambook第一版里面的安装包才可以,安装包自取:链接: https://pan.baidu.com/s/1CfUpOdoVtaQoaMhcoNOt1w 提取码: 7gvi
- 安装过程
cd DBow3/
mkdir build
cd build/
cmake ..
make -j4
sudo make install
2 创建字典
- 该工程实现需要一个包含十张图片的数据集,自取:链接: https://pan.baidu.com/s/19I1holzTCyWhzdI1RpOBeg 提取码: khln
- 整个工程代码为:链接: https://pan.baidu.com/s/1NV4LZwt7FV-25ef0wNOHIg 提取码: oh59
2.1 feature_training.cpp
#include "DBoW3/DBoW3.h"
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
cout<<"reading images... "<<endl;
vector<Mat> images;
for ( int i=0; i<10; i++ )
{
string path = "./data/"+to_string(i+1)+".png";
images.push_back( imread(path) );
}
cout<<"detecting ORB features ... "<<endl;
Ptr< Feature2D > detector = ORB::create();
vector<Mat> descriptors;
for ( Mat& image:images )
{
vector<KeyPoint> keypoints;
Mat descriptor;
detector->detectAndCompute( image, Mat(), keypoints, descriptor );
descriptors.push_back( descriptor );
}
cout<<"creating vocabulary ... "<<endl;
DBoW3::Vocabulary vocab;
vocab.create( descriptors );
cout<<"vocabulary info: "<<vocab<<endl;
vocab.save( "vocabulary.yml.gz" );
cout<<"done"<<endl;
return 0;
}
2.2 CMakeLists.txt
cmake_minimum_required( VERSION 2.8 )
project( loop_closure )
set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )
# opencv
find_package( OpenCV 3.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
# dbow3
# dbow3 is a simple lib so I assume you installed it in default directory
set( DBoW3_INCLUDE_DIRS "/usr/local/include" )
set( DBoW3_LIBS "/usr/local/lib/libDBoW3.a" )
add_executable( feature_training src/feature_training.cpp )
target_link_libraries( feature_training ${OpenCV_LIBS} ${DBoW3_LIBS} )
2.3 输出
- 会生成一个
vocabulary.yml.gz
文件,后面会有用,自取:链接: https://pan.baidu.com/s/1i4zAnHM2BeycBi_GwF2fnA 提取码: sbsf
2.运行指令
./feature_training ../data/
/home/bupo/my_study/slam14/slam14_my/cap11/feature_training/cmake-build-debug/feature_training ./data
reading images...
detecting ORB features ...
[ INFO:0] Initialize OpenCL runtime...
creating vocabulary ...
vocabulary info: Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 4970
done
进程已结束,退出代码0
- 可以看到:分支数量k为10,深度L为5,单词数量为4983,没有达到最大容量。Weighting是权重,Scoring是评分
3 相似度的计算
- 该工程实现需要一个包含十张图片的数据集,自取:链接: https://pan.baidu.com/s/19I1holzTCyWhzdI1RpOBeg 提取码: khln
- 前面这个工程会生成一个
vocabulary.yml.gz
文件,该工程会用到,自取:链接: https://pan.baidu.com/s/1i4zAnHM2BeycBi_GwF2fnA 提取码: sbsf
- 该工程自取:链接: 链接: https://pan.baidu.com/s/12E6fvy2DV9-BqOuXtFigYw 提取码: hmp2
3.1 loop_closure.cpp
#include "DBoW3/DBoW3.h"
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char **argv) {
if (argc != 2) {
cout << "Usage: 需要字典" << endl;
return 1;
}
string zidian_file = argv[1];
DBoW3::Vocabulary vocab(zidian_file);
cout << "reading database" << endl;
if (vocab.empty()) {
cerr << "Vocabulary does not exist." << endl;
return 1;
}
cout << "reading images... " << endl;
vector<Mat> images;
for (int i = 0; i < 10; i++) {
string path = "../data/" + to_string(i + 1) + ".png";
images.push_back(imread(path));
}
cout << "detecting ORB features ... " << endl;
Ptr<Feature2D> detector = ORB::create();
vector<Mat> descriptors;
for (Mat &image:images) {
vector<KeyPoint> keypoints;
Mat descriptor;
detector->detectAndCompute(image, Mat(), keypoints, descriptor);
descriptors.push_back(descriptor);
}
cout << "comparing images with images " << endl;
for (int i = 0; i < images.size(); i++)
{
DBoW3::BowVector v1;
vocab.transform(descriptors[i], v1);
for (int j = i; j < images.size(); j++)
{
DBoW3::BowVector v2;
vocab.transform(descriptors[j], v2);
double score = vocab.score(v1, v2);
cout << "image " << i << " vs image " << j << " : " << score << endl;
}
cout << endl;
}
cout << "comparing images with database " << endl;
DBoW3::Database db(vocab, false, 0);
for (int i = 0; i < descriptors.size(); i++)
db.add(descriptors[i]);
cout << "database info: " << db << endl;
for (int i = 0; i < descriptors.size(); i++)
{
DBoW3::QueryResults ret;
db.query(descriptors[i], ret, 4);
cout << "searching for image " << i << " returns " << ret << endl << endl;
}
cout << "done." << endl;
}
3.2 CMakeLists.txt
cmake_minimum_required( VERSION 2.8 )
project( loop_closure )
set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )
# opencv
find_package( OpenCV 3.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
# dbow3
# dbow3 is a simple lib so I assume you installed it in default directory
set( DBoW3_INCLUDE_DIRS "/usr/local/include" )
set( DBoW3_LIBS "/usr/local/lib/libDBoW3.a" )
add_executable( loop_closure src/loop_closure.cpp )
target_link_libraries( loop_closure ${OpenCV_LIBS} ${DBoW3_LIBS} )
3.3 输出
- 运行指令
./loop_closure
/home/bupo/my_study/slam14/slam14_my/cap11/loop_closure/cmake-build-debug/loop_closure
reading database
reading images...
detecting ORB features ...
[ INFO:0] Initialize OpenCL runtime...
comparing images with images
image 0 vs image 0 : 1
image 0 vs image 1 : 0.0367529
image 0 vs image 2 : 0.0277822
image 0 vs image 3 : 0.0281337
image 0 vs image 4 : 0.0335461
image 0 vs image 5 : 0.0427682
image 0 vs image 6 : 0.038458
image 0 vs image 7 : 0.0305787
image 0 vs image 8 : 0.0295247
image 0 vs image 9 : 0.0621202
image 1 vs image 1 : 1
image 1 vs image 2 : 0.0371357
image 1 vs image 3 : 0.0300564
image 1 vs image 4 : 0.0339359
image 1 vs image 5 : 0.0412664
image 1 vs image 6 : 0.0226204
image 1 vs image 7 : 0.0304568
image 1 vs image 8 : 0.0426928
image 1 vs image 9 : 0.033033
image 2 vs image 2 : 1
image 2 vs image 3 : 0.0318231
image 2 vs image 4 : 0.0271078
image 2 vs image 5 : 0.0269704
image 2 vs image 6 : 0.0233219
image 2 vs image 7 : 0.0497462
image 2 vs image 8 : 0.0374033
image 2 vs image 9 : 0.0292551
image 3 vs image 3 : 1
image 3 vs image 4 : 0.0348777
image 3 vs image 5 : 0.0366132
image 3 vs image 6 : 0.0424612
image 3 vs image 7 : 0.0172669
image 3 vs image 8 : 0.032024
image 3 vs image 9 : 0.042369
image 4 vs image 4 : 1
image 4 vs image 5 : 0.0627885
image 4 vs image 6 : 0.041152
image 4 vs image 7 : 0.0233412
image 4 vs image 8 : 0.0198614
image 4 vs image 9 : 0.0288873
image 5 vs image 5 : 1
image 5 vs image 6 : 0.0319993
image 5 vs image 7 : 0.0236407
image 5 vs image 8 : 0.0263738
image 5 vs image 9 : 0.0306995
image 6 vs image 6 : 1
image 6 vs image 7 : 0.0345444
image 6 vs image 8 : 0.0376772
image 6 vs image 9 : 0.0297798
image 7 vs image 7 : 1
image 7 vs image 8 : 0.0315193
image 7 vs image 9 : 0.0284877
image 8 vs image 8 : 1
image 8 vs image 9 : 0.040886
image 9 vs image 9 : 1
comparing images with database
database info: Database: Entries = 10, Using direct index = no. Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 4983
searching for image 0 returns 4 results:
<EntryId: 0, Score: 1>
<EntryId: 9, Score: 0.0621202>
<EntryId: 5, Score: 0.0427682>
<EntryId: 6, Score: 0.038458>
searching for image 1 returns 4 results:
<EntryId: 1, Score: 1>
<EntryId: 8, Score: 0.0426928>
<EntryId: 5, Score: 0.0412664>
<EntryId: 2, Score: 0.0371357>
searching for image 2 returns 4 results:
<EntryId: 2, Score: 1>
<EntryId: 7, Score: 0.0497462>
<EntryId: 8, Score: 0.0374033>
<EntryId: 1, Score: 0.0371357>
searching for image 3 returns 4 results:
<EntryId: 3, Score: 1>
<EntryId: 6, Score: 0.0424612>
<EntryId: 9, Score: 0.042369>
<EntryId: 5, Score: 0.0366132>
searching for image 4 returns 4 results:
<EntryId: 4, Score: 1>
<EntryId: 5, Score: 0.0627885>
<EntryId: 6, Score: 0.041152>
<EntryId: 3, Score: 0.0348777>
searching for image 5 returns 4 results:
<EntryId: 5, Score: 1>
<EntryId: 4, Score: 0.0627885>
<EntryId: 0, Score: 0.0427682>
<EntryId: 1, Score: 0.0412664>
searching for image 6 returns 4 results:
<EntryId: 6, Score: 1>
<EntryId: 3, Score: 0.0424612>
<EntryId: 4, Score: 0.041152>
<EntryId: 0, Score: 0.038458>
searching for image 7 returns 4 results:
<EntryId: 7, Score: 1>
<EntryId: 2, Score: 0.0497462>
<EntryId: 6, Score: 0.0345444>
<EntryId: 8, Score: 0.0315193>
searching for image 8 returns 4 results:
<EntryId: 8, Score: 1>
<EntryId: 1, Score: 0.0426928>
<EntryId: 9, Score: 0.040886>
<EntryId: 6, Score: 0.0376772>
searching for image 9 returns 4 results:
<EntryId: 9, Score: 1>
<EntryId: 0, Score: 0.0621202>
<EntryId: 3, Score: 0.042369>
<EntryId: 8, Score: 0.040886>
done.
进程已结束,退出代码0
- 可以看到不同的图像与相似图像的评分有多大差异。我们看到,明显相似的图1和图10(在C++中下标为0和9)其相似度评分约为0.0525,而其他图像约为0.02。
4 增加字典规模
- 这里需要下载对应的数据集,可以去官网下载:https://vision.in.tum.de/data/datasets/rgbd-dataset/download#,也可以在网盘自取链接: https://pan.baidu.com/s/1bg6L8u9RF0YtpkzYX38MAw 提取码: 6dj4
- 整个工程除去数据集自取:链接: https://pan.baidu.com/s/1LIhMVq8FlUsOA0Sk1IlD5g 提取码: kv9b
4.1 gen_vocab_large.cpp
#include "DBoW3/DBoW3.h"
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
String directoryPath = "/home/bupo/my_study/slam14/slam14_my/cap11/gen_vocab_large/rgbd_dataset_freiburg1_desk2/rgb";
vector<String> imagesPath;
glob(directoryPath, imagesPath);
4.2 CMakeLists.txt
cmake_minimum_required( VERSION 2.8 )
project( loop_closure )
set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )
# opencv
find_package( OpenCV 3.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
# dbow3
# dbow3 is a simple lib so I assume you installed it in default directory
set( DBoW3_INCLUDE_DIRS "/usr/local/include" )
set( DBoW3_LIBS "/usr/local/lib/libDBoW3.a" )
add_executable( gen_vocab src/gen_vocab_large.cpp )
target_link_libraries( gen_vocab ${OpenCV_LIBS} ${DBoW3_LIBS} )
4.3 输出
/home/bupo/my_study/slam14/slam14_my/cap11/gen_vocab_large/cmake-build-debug/gen_vocab
generating features ...
[ INFO:0] Initialize OpenCL runtime...
extracting features from image 1
extracting features from image 2
extracting features from image 639
extracting features from image 640
extract total 320000 features.
creating vocabulary, please wait ...
vocabulary info: Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 89849
done
进程已结束,退出代码0
- 同时会生成文件
vocab_larger.yml.gz
,这个文件,是通过640个图片训练得到的字典,可以在loop_closure中,当然,如果你不想训练,可以直接自取我训练得到的结果:链接: https://pan.baidu.com/s/1-L7uYoCKmzrLRqmTxwxxMg 提取码: 66kc
4.4 使用该字典进行回环
/home/bupo/my_study/slam14/slam14_my/cap11/loop_closure/cmake-build-debug/loop_closure
reading database
reading images...
detecting ORB features ...
[ INFO:0] Initialize OpenCL runtime...
comparing images with images
image 0 vs image 0 : 1
image 0 vs image 1 : 0.0264319
image 0 vs image 2 : 0.0192686
image 0 vs image 3 : 0.0174829
image 0 vs image 4 : 0.00919418
image 0 vs image 5 : 0.0196796
image 0 vs image 6 : 0.0195631
image 0 vs image 7 : 0.0156169
image 0 vs image 8 : 0.0225417
image 0 vs image 9 : 0.0543705
image 1 vs image 1 : 1
image 1 vs image 2 : 0.0305082
image 1 vs image 3 : 0.0226657
image 1 vs image 4 : 0.0116976
image 1 vs image 5 : 0.0178306
image 1 vs image 6 : 0.0277621
image 1 vs image 7 : 0.0198653
image 1 vs image 8 : 0.0267385
image 1 vs image 9 : 0.0262276
image 2 vs image 2 : 1
image 2 vs image 3 : 0.0210716
image 2 vs image 4 : 0.0280933
image 2 vs image 5 : 0.0342187
image 2 vs image 6 : 0.0247903
image 2 vs image 7 : 0.0233946
image 2 vs image 8 : 0.0221119
image 2 vs image 9 : 0.0238046
image 3 vs image 3 : 1
image 3 vs image 4 : 0.0189549
image 3 vs image 5 : 0.0257821
image 3 vs image 6 : 0.0244571
image 3 vs image 7 : 0.0196989
image 3 vs image 8 : 0.0260922
image 3 vs image 9 : 0.0240474
image 4 vs image 4 : 1
image 4 vs image 5 : 0.0398126
image 4 vs image 6 : 0.0276248
image 4 vs image 7 : 0.014187
image 4 vs image 8 : 0.014109
image 4 vs image 9 : 0.0167629
image 5 vs image 5 : 1
image 5 vs image 6 : 0.0176884
image 5 vs image 7 : 0.0227182
image 5 vs image 8 : 0.0249955
image 5 vs image 9 : 0.0248106
image 6 vs image 6 : 1
image 6 vs image 7 : 0.0155511
image 6 vs image 8 : 0.0323909
image 6 vs image 9 : 0.0206828
image 7 vs image 7 : 1
image 7 vs image 8 : 0.0309826
image 7 vs image 9 : 0.0322799
image 8 vs image 8 : 1
image 8 vs image 9 : 0.0163077
image 9 vs image 9 : 1
comparing images with database
database info: Database: Entries = 10, Using direct index = no. Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 89849
searching for image 0 returns 4 results:
<EntryId: 0, Score: 1>
<EntryId: 9, Score: 0.0543705>
<EntryId: 1, Score: 0.0264319>
<EntryId: 8, Score: 0.0225417>
searching for image 1 returns 4 results:
<EntryId: 1, Score: 1>
<EntryId: 2, Score: 0.0305082>
<EntryId: 6, Score: 0.0277621>
<EntryId: 8, Score: 0.0267385>
searching for image 2 returns 4 results:
<EntryId: 2, Score: 1>
<EntryId: 5, Score: 0.0342187>
<EntryId: 1, Score: 0.0305082>
<EntryId: 4, Score: 0.0280933>
searching for image 3 returns 4 results:
<EntryId: 3, Score: 1>
<EntryId: 8, Score: 0.0260922>
<EntryId: 5, Score: 0.0257821>
<EntryId: 6, Score: 0.0244571>
searching for image 4 returns 4 results:
<EntryId: 4, Score: 1>
<EntryId: 5, Score: 0.0398126>
<EntryId: 2, Score: 0.0280933>
<EntryId: 6, Score: 0.0276248>
searching for image 5 returns 4 results:
<EntryId: 5, Score: 1>
<EntryId: 4, Score: 0.0398126>
<EntryId: 2, Score: 0.0342187>
<EntryId: 3, Score: 0.0257821>
searching for image 6 returns 4 results:
<EntryId: 6, Score: 1>
<EntryId: 8, Score: 0.0323909>
<EntryId: 1, Score: 0.0277621>
<EntryId: 4, Score: 0.0276248>
searching for image 7 returns 4 results:
<EntryId: 7, Score: 1>
<EntryId: 9, Score: 0.0322799>
<EntryId: 8, Score: 0.0309826>
<EntryId: 2, Score: 0.0233946>
searching for image 8 returns 4 results:
<EntryId: 8, Score: 1>
<EntryId: 6, Score: 0.0323909>
<EntryId: 7, Score: 0.0309826>
<EntryId: 1, Score: 0.0267385>
searching for image 9 returns 4 results:
<EntryId: 9, Score: 1>
<EntryId: 0, Score: 0.0543705>
<EntryId: 7, Score: 0.0322799>
<EntryId: 1, Score: 0.0262276>
done.
进程已结束,退出代码0