OpenCV 2.4.13中把Bow保存为词典

环境依赖

本示例依赖于OpenCV 2.4.13

项目应用

应用于SLAM中回环检测

具体代码

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

#include 
#include 

#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 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 detector =
            new DynamicAdaptedFeatureDetector(
                    AdjusterAdapter::create("STAR"), 500, 1500, 5);
    Ptr extractor =
            new SurfDescriptorExtractor(1000, 4, 2, false, true);

    //begin the bow training
    BOWKMeansTrainer bowTraining(1000);
    vector kps;
    Mat descriptors;
    for (int i = 0; idetect(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" <

你可能感兴趣的:(OpenCV 2.4.13中把Bow保存为词典)