第11讲 回环检测

1 创建字典

  有10张图,从1.png10.png,利用这10幅图的ORB描述子创建一个字典。
  cpp文件内容为,

#include "DBoW3/DBoW3.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <vector>
#include <string>

using namespace std;
using namespace cv;

/*
 * 本节演示了如何根据data/目录下的十张图训练字典
 */

int main()
{
    cout << "正在读取图片!" << 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 << "正在检测ORB特征!" << endl;
    Ptr<Feature2D> detector = ORB::create();  //默认500个特征点
    vector<Mat> descriptors;
    //只保存特征的描述子,不保存其关键点
    for(Mat& image : images)
    {
        vector<KeyPoint> keypoints;
        Mat descriptor;
        detector->detectAndCompute(image, Mat(), keypoints, descriptor);
        descriptors.push_back(descriptor);
    }

    cout << "正在创建字典!" << endl;
    DBoW3::Vocabulary vocab;
    vocab.create(descriptors);
    cout << "字典信息:\n" << vocab << endl;
    vocab.save("vocabulary.yml.gz");  //保存字典
    cout << "已完成字典创建!" << endl;

    return 0;
}

  CMakeLists.txt文件内容为,

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11 -O3")

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIRECTORIES})

find_package(DBoW3 REQUIRED)
include_directories(${DBoW3_DIRECTORIES})

add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBRARIES} ${DBoW3_LIBRARIES})

  程序运行结果为,

正在读取图片!
正在检测ORB特征!
正在创建字典!
字典信息:
Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 4970
/*
 * 此处省略4970个单词
*/
已完成字典创建!

程序运行之后,你将会得到字典文件vocabulary.yml.gz。输出的信息表明该字典每层有10个分叉,共有5层。
  对于这个字典中的单词 i i i,它的权重 w i w_i wi的计算如下,
w i = T F i × I D F i w_i=TF_i \times IDF_i wi=TFi×IDFi
 其中 T F i TF_i TFi表示第 i i i个单词的Term Frequency,中文是译频率。它是指单词 i i i单幅图像中出现的频率。计算如下,
T F i = m i m TF_i=\frac{m_i}{m} TFi=mmi
m i m_i mi表示该图像中单词 i i i出现的次数, m m m表示该图像中所有的单词数目。那么,在计算字典中单词 i i i的权重 w i w_i wi时,应该选取哪幅图像呢?计算哪幅图像就选取哪幅图像!
 其中 I D F i IDF_i IDFi表示第 i i i个单词的Inverse Document Frequency,中文是逆文档频率。它是指单词 i i i的特征数量占字典中所有特征数量的 l o g log log值。
I D F i = l o g n n i IDF_i=log\frac{n}{n_i} IDFi=lognin
n n n表示该字典中所有特征的数量, n i n_i ni表示单词 i i i的特征数量。
  对于图像A和图像B的相似度评分利用 L 1 L_1 L1范数形式。根据词典,我们可以生成图像A的词袋向量 v A v_A vA和图像B的词袋向量 v B v_B vB。该向量的维数等于词典中的单词维数。第 i i i位上的值要么为0,表示该图像上没有单词 i i i;要么就等于单词 i i i的权重 w i w_i wi,表示该图像上有单词 i i i。词袋向量 v A v_A vA和词袋向量 v B v_B vB的相似度评分 s c o r e score score计算如下,
s c o r e = 2 ∑ i = 1 N ( ∣ v A i ∣ + ∣ v B i ∣ − ∣ v A i − v B i ∣ ) score=2\sum_{i=1}^N(|v_{A_i}|+|v_{B_i}|-|v_{A_i}-v_{B_i}|) score=2i=1N(vAi+vBivAivBi)

2 相似度的计算

  已有词典vocabulary.yml.gz,利用它来计算图片1.png10.png之间的相似度。
  cpp文件内容为,

#include "DBoW3/DBoW3.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <vector>
#include <string>

using namespace std;
using namespace cv;

/*
 * 本节演示了如何根据前面训练的字典计算相似度评分
 */

int main()
{
    cout << "正在读取字典数据!" << endl;
    DBoW3::Vocabulary vocab("../vocabulary.yml.gz");
    if(vocab.empty())
    {
        cerr << "字典不存在,程序终止!" << endl;
        return 1;
    }

    cout << "正在读取图像!" << 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 << "正在检测ORB特征!" << 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;
        //descriptors[i]表示图像i中所有的ORB描述子集合,函数transform()计算出用先前字典来描述的单词向量,
        //每个向量中元素的值要么是0,表示图像i中没有这个单词;要么是该单词的权重!
        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 << "图像" << i+1 << "和图像" << j+1 << "之间的相似度评分为:" << 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 << "数据库信息为:\n" << db << endl;
    for(int i = 0; i < descriptors.size(); i++){
        DBoW3::QueryResults ret;
        db.query(descriptors[i], ret, 4);
        cout << "searching for image " << i+1 << " returns:\n" << ret << endl << endl;
    }
    cout << "done." << endl;
    return 0;
}

  CMakeLists.txt文件内容为,

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGES "-std=c++11 -O3")

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIRECTORIES})

find_package(DBoW3 REQUIRED)
include_directories(${DBoW3_DIRECTORIES})

add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBRARIES} ${DBoW3_LIBRARIES})

  程序运行结果为,

正在读取字典数据!
正在读取图像!
正在检测ORB特征!
comparing images with images!
图像1和图像1之间的相似度评分为:1
图像1和图像2之间的相似度评分为:0.0238961
图像1和图像3之间的相似度评分为:0.0198893
图像1和图像4之间的相似度评分为:0.0339413
图像1和图像5之间的相似度评分为:0.0160477
图像1和图像6之间的相似度评分为:0.0300543
图像1和图像7之间的相似度评分为:0.0222911
图像1和图像8之间的相似度评分为:0.0197256
图像1和图像9之间的相似度评分为:0.0228385
图像1和图像10之间的相似度评分为:0.0583173

图像2和图像2之间的相似度评分为:1
图像2和图像3之间的相似度评分为:0.0422068
图像2和图像4之间的相似度评分为:0.0282324
图像2和图像5之间的相似度评分为:0.0225322
图像2和图像6之间的相似度评分为:0.025146
图像2和图像7之间的相似度评分为:0.0161122
图像2和图像8之间的相似度评分为:0.0166048
图像2和图像9之间的相似度评分为:0.0350047
图像2和图像10之间的相似度评分为:0.0326933

图像3和图像3之间的相似度评分为:1
图像3和图像4之间的相似度评分为:0.0361374
图像3和图像5之间的相似度评分为:0.0330401
图像3和图像6之间的相似度评分为:0.0189786
图像3和图像7之间的相似度评分为:0.0182909
图像3和图像8之间的相似度评分为:0.0137075
图像3和图像9之间的相似度评分为:0.0270761
图像3和图像10之间的相似度评分为:0.0219518

图像4和图像4之间的相似度评分为:1
图像4和图像5之间的相似度评分为:0.0304209
图像4和图像6之间的相似度评分为:0.03611
图像4和图像7之间的相似度评分为:0.0205856
图像4和图像8之间的相似度评分为:0.0208058
图像4和图像9之间的相似度评分为:0.0312382
图像4和图像10之间的相似度评分为:0.0329747

图像5和图像5之间的相似度评分为:1
图像5和图像6之间的相似度评分为:0.0498645
图像5和图像7之间的相似度评分为:0.0345081
图像5和图像8之间的相似度评分为:0.0227451
图像5和图像9之间的相似度评分为:0.0208472
图像5和图像10之间的相似度评分为:0.0266803

图像6和图像6之间的相似度评分为:1
图像6和图像7之间的相似度评分为:0.0198106
图像6和图像8之间的相似度评分为:0.0162269
图像6和图像9之间的相似度评分为:0.0259153
图像6和图像10之间的相似度评分为:0.0220667

图像7和图像7之间的相似度评分为:1
图像7和图像8之间的相似度评分为:0.0212959
图像7和图像9之间的相似度评分为:0.0188494
图像7和图像10之间的相似度评分为:0.0208606

图像8和图像8之间的相似度评分为:1
图像8和图像9之间的相似度评分为:0.012798
图像8和图像10之间的相似度评分为:0.0205016

图像9和图像9之间的相似度评分为:1
图像9和图像10之间的相似度评分为:0.0228186

图像10和图像10之间的相似度评分为:1

comparing images with database!
数据库信息为:
Database: Entries = 10, Using direct index = no. Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 4970
searching for image 1 returns:
4 results:
<EntryId: 0, Score: 1>
<EntryId: 9, Score: 0.0583173>
<EntryId: 3, Score: 0.0339413>
<EntryId: 5, Score: 0.0300543>

searching for image 2 returns:
4 results:
<EntryId: 1, Score: 1>
<EntryId: 2, Score: 0.0422068>
<EntryId: 8, Score: 0.0350047>
<EntryId: 9, Score: 0.0326933>

searching for image 3 returns:
4 results:
<EntryId: 2, Score: 1>
<EntryId: 1, Score: 0.0422068>
<EntryId: 3, Score: 0.0361374>
<EntryId: 4, Score: 0.0330401>

searching for image 4 returns:
4 results:
<EntryId: 3, Score: 1>
<EntryId: 2, Score: 0.0361374>
<EntryId: 5, Score: 0.03611>
<EntryId: 0, Score: 0.0339413>

searching for image 5 returns:
4 results:
<EntryId: 4, Score: 1>
<EntryId: 5, Score: 0.0498645>
<EntryId: 6, Score: 0.0345081>
<EntryId: 2, Score: 0.0330401>

searching for image 6 returns:
4 results:
<EntryId: 5, Score: 1>
<EntryId: 4, Score: 0.0498645>
<EntryId: 3, Score: 0.03611>
<EntryId: 0, Score: 0.0300543>

searching for image 7 returns:
4 results:
<EntryId: 6, Score: 1>
<EntryId: 4, Score: 0.0345081>
<EntryId: 0, Score: 0.0222911>
<EntryId: 7, Score: 0.0212959>

searching for image 8 returns:
4 results:
<EntryId: 7, Score: 1>
<EntryId: 4, Score: 0.0227451>
<EntryId: 6, Score: 0.0212959>
<EntryId: 3, Score: 0.0208058>

searching for image 9 returns:
4 results:
<EntryId: 8, Score: 1>
<EntryId: 1, Score: 0.0350047>
<EntryId: 3, Score: 0.0312382>
<EntryId: 2, Score: 0.0270761>

searching for image 10 returns:
4 results:
<EntryId: 9, Score: 1>
<EntryId: 0, Score: 0.0583173>
<EntryId: 3, Score: 0.0329747>
<EntryId: 1, Score: 0.0326933>

done.

3 增加字典规模

  这次利用TUM RGB-D数据集中的rgbd_dataset_freiburg2_desk序列来生成字典vocab_larger.yml.gz,然后利用这个字典来计算图像之间的相似度。
  cpp文件内容为,

#include "DBoW3/DBoW3.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <chrono>

using namespace cv;
using namespace std;

int main()
{
    String directoryPath = "/media/fyw/Elements-SE/03研究生第3学期/1华硕A501L备份/0important file/TUM RGB-D Dataset/rgbd_dataset_freiburg2_desk/rgb";
    vector<String> imagesPath;
    glob(directoryPath, imagesPath);

    cout << "总共有" << imagesPath.size() << "张图!" << endl;

    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
    cout << "正在存储每张图的描述子集合Mat类型变量!" << endl;
    vector<Mat> descriptors;
    Ptr<Feature2D> detector = ORB::create();  //默认提取500个特征点
    int idx = 1;
    for(String path : imagesPath)
    {
        //cout << "正在从第" << idx++ << "张图像中提取特征!" << endl;
        Mat image = imread(path);
        vector<KeyPoint> keypoints;
        Mat descriptor;
        detector->detectAndCompute(image, Mat(), keypoints, descriptor);
        descriptors.push_back(descriptor);
    }
    cout << "提取到的特征总数目为:" << descriptors.size() * 500 << endl;
    chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
    chrono::duration<double> timeUsed = chrono::duration_cast<chrono::duration<double>> (t2 - t1);
    cout << "存储所有图片的描述子集合花费的时间为:" << timeUsed.count() << "秒!" << endl;

    t1 = chrono::steady_clock::now();
    cout << "正在创建词典!" << endl;
    DBoW3::Vocabulary vocab;
    vocab.create(descriptors);
    cout << "词典信息为:\n" << vocab << endl;
    vocab.save("vocab_larger.yml.gz");
    cout << "done." << endl;
    t2 = chrono::steady_clock::now();
    timeUsed = chrono::duration_cast<chrono::duration<double>> (t2 - t1);
    cout << "创建词典花费的时间为:" << timeUsed.count() << "秒!" << endl;

    return 0;

}

  CMakeLists.txt文件内容为,

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11 -O3")

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIRECTORIES})

find_package(DBoW3 REQUIRED)
include_directories(${DBoW3_DIRECTORIES})

add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBRARIES} ${DBoW3_LIBRARIES})

  程序执行结果为,

总共有2965张图!
正在存储每张图的描述子集合Mat类型变量!
提取到的特征总数目为:1482500
存储所有图片的描述子集合花费的时间为:56.2785秒!
正在创建词典!
词典信息为:
Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 99552
/*
 * 此处省略了99552个单词信息。
*/
done.
创建词典花费的时间为:79.7457秒!

运行程序,生成字典vocab_larger.yml.gz。利用该字典计算图像间的相似度(重复 2 相似度的计算 的工作),结果如下,

正在读取字典数据!
正在读取图像!
正在检测ORB特征!
comparing images with images!
图像1和图像1之间的相似度评分为:1
图像1和图像2之间的相似度评分为:0.0086601
图像1和图像3之间的相似度评分为:0.00267292
图像1和图像4之间的相似度评分为:0.0211117
图像1和图像5之间的相似度评分为:0.0095985
图像1和图像6之间的相似度评分为:0.00498656
图像1和图像7之间的相似度评分为:0.0078268
图像1和图像8之间的相似度评分为:0.00259474
图像1和图像9之间的相似度评分为:0.0067211
图像1和图像10之间的相似度评分为:0.0275321

图像2和图像2之间的相似度评分为:1
图像2和图像3之间的相似度评分为:0.00609129
图像2和图像4之间的相似度评分为:0.00387263
图像2和图像5之间的相似度评分为:0.00127383
图像2和图像6之间的相似度评分为:0.00467792
图像2和图像7之间的相似度评分为:0.00152321
图像2和图像8之间的相似度评分为:0.00174409
图像2和图像9之间的相似度评分为:0.00102306
图像2和图像10之间的相似度评分为:0.015175

图像3和图像3之间的相似度评分为:1
图像3和图像4之间的相似度评分为:0.00334078
图像3和图像5之间的相似度评分为:0.00772233
图像3和图像6之间的相似度评分为:0.00708895
图像3和图像7之间的相似度评分为:0.00535888
图像3和图像8之间的相似度评分为:0.00536089
图像3和图像9之间的相似度评分为:0.00133823
图像3和图像10之间的相似度评分为:0.0105568

图像4和图像4之间的相似度评分为:1
图像4和图像5之间的相似度评分为:0.00179705
图像4和图像6之间的相似度评分为:0.00889503
图像4和图像7之间的相似度评分为:0.00516459
图像4和图像8之间的相似度评分为:0.00187135
图像4和图像9之间的相似度评分为:0.00512514
图像4和图像10之间的相似度评分为:0.0147898

图像5和图像5之间的相似度评分为:1
图像5和图像6之间的相似度评分为:0.0130994
图像5和图像7之间的相似度评分为:0.00137928
图像5和图像8之间的相似度评分为:0.00154401
图像5和图像9之间的相似度评分为:0.00410765
图像5和图像10之间的相似度评分为:0.00780494

图像6和图像6之间的相似度评分为:1
图像6和图像7之间的相似度评分为:0.00310489
图像6和图像8之间的相似度评分为:-0
图像6和图像9之间的相似度评分为:0.0071368
图像6和图像10之间的相似度评分为:0.00546152

图像7和图像7之间的相似度评分为:1
图像7和图像8之间的相似度评分为:0.00173427
图像7和图像9之间的相似度评分为:0.00539158
图像7和图像10之间的相似度评分为:0.00448849

图像8和图像8之间的相似度评分为:1
图像8和图像9之间的相似度评分为:0.00590678
图像8和图像10之间的相似度评分为:0.00285898

图像9和图像9之间的相似度评分为:1
图像9和图像10之间的相似度评分为:0.0111212

图像10和图像10之间的相似度评分为:1

comparing images with database!
数据库信息为:
Database: Entries = 10, Using direct index = no. Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 99552
searching for image 1 returns:
4 results:
<EntryId: 0, Score: 1>
<EntryId: 9, Score: 0.0275321>
<EntryId: 3, Score: 0.0211117>
<EntryId: 4, Score: 0.0095985>

searching for image 2 returns:
4 results:
<EntryId: 1, Score: 1>
<EntryId: 9, Score: 0.015175>
<EntryId: 0, Score: 0.0086601>
<EntryId: 2, Score: 0.00609129>

searching for image 3 returns:
4 results:
<EntryId: 2, Score: 1>
<EntryId: 9, Score: 0.0105568>
<EntryId: 4, Score: 0.00772233>
<EntryId: 5, Score: 0.00708895>

searching for image 4 returns:
4 results:
<EntryId: 3, Score: 1>
<EntryId: 0, Score: 0.0211117>
<EntryId: 9, Score: 0.0147898>
<EntryId: 5, Score: 0.00889503>

searching for image 5 returns:
4 results:
<EntryId: 4, Score: 1>
<EntryId: 5, Score: 0.0130994>
<EntryId: 0, Score: 0.0095985>
<EntryId: 9, Score: 0.00780494>

searching for image 6 returns:
4 results:
<EntryId: 5, Score: 1>
<EntryId: 4, Score: 0.0130994>
<EntryId: 3, Score: 0.00889503>
<EntryId: 8, Score: 0.0071368>

searching for image 7 returns:
4 results:
<EntryId: 6, Score: 1>
<EntryId: 0, Score: 0.0078268>
<EntryId: 8, Score: 0.00539158>
<EntryId: 2, Score: 0.00535888>

searching for image 8 returns:
4 results:
<EntryId: 7, Score: 1>
<EntryId: 8, Score: 0.00590678>
<EntryId: 2, Score: 0.00536089>
<EntryId: 9, Score: 0.00285898>

searching for image 9 returns:
4 results:
<EntryId: 8, Score: 1>
<EntryId: 9, Score: 0.0111212>
<EntryId: 5, Score: 0.0071368>
<EntryId: 0, Score: 0.0067211>

searching for image 10 returns:
4 results:
<EntryId: 9, Score: 1>
<EntryId: 0, Score: 0.0275321>
<EntryId: 1, Score: 0.015175>
<EntryId: 3, Score: 0.0147898>

done.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值