#include"DBoW3/DBoW3.h"//词袋支持头文件#include<opencv2/core/core.hpp>//opencv核心模块#include<opencv2/highgui/highgui.hpp>//gui模块#include<opencv2/features2d/features2d.hpp>//特征点头文件#include<iostream>#include<vector>#include<string>usingnamespace cv;usingnamespace std;/***************************************************
* 本节演示了如何根据前面训练的字典计算相似性评分
* ************************************************/intmain(int argc,char**argv){
if(argc !=2){
cout <<"Usage: 需要字典"<< endl;return1;}
string zidian_file = argv[1];
DBoW3::Vocabulary vocab(zidian_file);// read the images and database(读取图像和数据库)
cout <<"reading database"<< endl;//输出reading database(读取数据)//DBoW3::Vocabulary vocab("../src/vocabulary.yml.gz");//vocabulary.yml.gz路径//DBoW3::Vocabulary vocab("../src/vocab_larger.yml.gz"); // use large vocab if you want:if(vocab.empty()){
cerr <<"Vocabulary does not exist."<< endl;//输出Vocabulary does not existreturn1;}
cout <<"reading images... "<< endl;//输出reading images...
vector<Mat> images;for(int i =0; i <10; i++){
string path ="../data/"+to_string(i +1)+".png";//图像读取路径
images.push_back(imread(path));}// NOTE: in this case we are comparing images with a vocabulary generated by themselves, this may lead to overfit. 这里我们用它们生成的字典比较它们本身的相似性,这可能会产生过拟合// detect ORB features
cout <<"detecting ORB features ... "<< endl;//输出detecting ORB features ...(正在检测ORB特征)
Ptr<Feature2D> detector =ORB::create();//默认图像500个特征点
vector<Mat> descriptors;//描述子 将10张图像提取ORB特征并存放在vector容器里for(Mat &image:images){
vector<KeyPoint> keypoints;//关键点
Mat descriptor;//描述子
detector->detectAndCompute(image,Mat(), keypoints, descriptor);//检测和计算
descriptors.push_back(descriptor);}// we can compare the images directly or we can compare one image to a database// images :
cout <<"comparing images with images "<< endl;//输出comparing images with imagesfor(int i =0; i < images.size(); i++){
DBoW3::BowVector v1;//descriptors[i]表示图像i中所有的ORB描述子集合,函数transform()计算出用先前字典来描述的单词向量,每个向量中元素的值要么是0,表示图像i中没有这个单词;要么是该单词的权重//BoW描述向量中含有每个单词的ID和权重,两者构成了整个稀疏的向量//当比较两个向量时,DBoW3会为我们计算一个分数
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);//p296式(11.9)
cout <<"image "<< i <<" vs image "<< j <<" : "<< score << endl;//输出一幅图像与另外一幅图像之间的相似度评分}
cout << endl;}// or with database//在进行数据库查询时,DBoW对上面的分数进行排序,给出最相似的结果
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;//输出database info(数据库信息)为for(int i =0; i < descriptors.size(); i++){
DBoW3::QueryResults ret;
db.query(descriptors[i], ret,4);// max result=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")#opencvfind_package( OpenCV 3.1 REQUIRED )include_directories( ${
OpenCV_INCLUDE_DIRS})#dbow3#dbow3 is a simple lib so I assume you installed it in default directoryset( 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
#include"DBoW3/DBoW3.h"//词袋支持头文件#include<opencv2/core/core.hpp>//opencv核心模块#include<opencv2/highgui/highgui.hpp>//gui模块#include<opencv2/features2d/features2d.hpp>//特征点头文件#include<iostream>#include<vector>#include<string>usingnamespace cv;usingnamespace std;intmain(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);// string dataset_dir = argv[1];// ifstream fin ( dataset_dir+"/home/liqiang/slambook2/ch11/rgbd_dataset_freiburg1_desk2/rgb" );// if ( !fin )// {
// cout<<"please generate the associate file called associate.txt!"<<endl;// return 1;// }// vector<string> rgb_files, depth_files;// vector<double> rgb_times, depth_times;// while ( !fin.eof() )// {
// string rgb_time, rgb_file, depth_time, depth_file;// fin>>rgb_time>>rgb_file>>depth_time>>depth_file;// rgb_times.push_back ( atof ( rgb_time.c_str() ) );// depth_times.push_back ( atof ( depth_time.c_str() ) );// rgb_files.push_back ( dataset_dir+"/"+rgb_file );// depth_files.push_back ( dataset_dir+"/"+depth_file );// if ( fin.good() == false )// break;// }// fin.close();
cout<<"generating features ... "<<endl;//输出generating features (正在检测ORB特征)...
vector<Mat> descriptors;//描述子
Ptr< Feature2D > detector =ORB::create();int index =1;for( String path : imagesPath ){
Mat image =imread(path);
vector<KeyPoint> keypoints;//关键点
Mat descriptor;//描述子
detector->detectAndCompute( image,Mat(), keypoints, descriptor );
descriptors.push_back( descriptor );
cout<<"extracting features from image "<< index++<<endl;//输出extracting features from image(从图像中提取特征)}
cout<<"extract total "<<descriptors.size()*500<<" features."<<endl;// create vocabulary
cout<<"creating vocabulary, please wait ... "<<endl;//输出creating vocabulary, please wait (创建词典,请稍等)...
DBoW3::Vocabulary vocab;
vocab.create( descriptors );
cout<<"vocabulary info: "<<vocab<<endl;
vocab.save("vocab_larger.yml.gz");//保存词典
cout<<"done"<<endl;return0;}
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")#opencvfind_package( OpenCV 3.1 REQUIRED )include_directories( ${
OpenCV_INCLUDE_DIRS})#dbow3#dbow3 is a simple lib so I assume you installed it in default directoryset( 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