SLAM、OpenCV、Linux、ROS等

OpenCV 2.4.13中把Bow保存为词典

2018-12-12  本文已影响0人  一恪slam

环境依赖

本示例依赖于OpenCV 2.4.13

项目应用

应用于SLAM中回环检测

具体代码

// Created by yike on 18-12-11.

#include <iostream>
#include <fstream>

#include "opencv2/opencv_modules.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/nonfree/nonfree.hpp"

using namespace cv;
using namespace std;

const int N=2;

int main(){

    const string imgName1 = "/home/gzz/CLionProjects/fabmap/fabmap/5000.jpg";
    const string imgName2 = "/home/gzz/CLionProjects/fabmap/fabmap/5005.jpg";

    Vector<Mat> imgs;
    imgs.push_back(imread(imgName1));
    imgs.push_back(imread(imgName2));

    //Creating detector, descriptor extractor and descriptor matcher
    cout << "Creating detector, descriptor extractor\n";
    Ptr<FeatureDetector> detector =
            new DynamicAdaptedFeatureDetector(
                    AdjusterAdapter::create("STAR"), 500, 1500, 5);
    Ptr<DescriptorExtractor> extractor =
            new SurfDescriptorExtractor(1000, 4, 2, false, true);

    //begin the bow training
    BOWKMeansTrainer bowTraining(1000);
    vector<KeyPoint> kps;
    Mat descriptors;
    for (int i = 0; i<N;i++){
        detector->detect(imgs[i],kps);
        extractor->compute(imgs[i],kps,descriptors);
        bowTraining.add(descriptors);
    }
    //generate the dictionary
    Mat dictionary = bowTraining.cluster();

    //save the dictionary to file
    FileStorage fs;
    fs.open("./fabmap/voc.yml",FileStorage::WRITE);
    fs <<"Vocabulary" <<dictionary;
    fs.release();

    cout<<"done!"<<endl;

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读