看高博的十四讲,跑了创建字典的例子,但是显示单词数是0:Number of words=0
vocabulary info: Vocabulary: k = 10, L = 5, Weighting = tf-idf, Scoring = L1-norm, Number of words = 0
刚开始怀疑DBoW3装的不对,但是重装之后还是不行,没办法我就从头调代码,在读取图片的时候发现图片为空。
于是怀疑图片路径不对,填写了完整的路径,发现可以了。
#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 cv;
using namespace std;
/***************************************************
* 本节演示了如何根据data/目录下的十张图训练字典
* ************************************************/
int main( int argc, char** argv )
{
// read the image
cout<<"reading images... "<<endl;
vector<Mat> images;
for ( int i=0; i<10; i++ )
{
string path = "/home/user/ch12/data/"+to_string(i+1)+".png";//需要完整路径
images.push_back( imread(path) );
if(!images[i].empty())//如果读取的图片为空就输出提示,很坑啊,希望大家注意
{
imshow("读取图像", images[i]);
waitKey(30);
}
else
{
cout<<"image is empty"<<endl;
}
}
// detect ORB features
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 );
}
// create vocabulary
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;
}
耐心一些,多去找方法想问题。
每天一点进步,坚持下去。