1

以下代码使用 C++ 编写,我正在使用 OpenCV 进行实验。假设我以下列方式使用 kd-tree (FlannBasedMatcher):

//these are inputs to the code snippet below. 
//They are filled with suitable values
Mat& queryDescriptors;
vector<Training> &trainCollection;
vector< vector<DMatch> >& matches;
int knn;

//setting flann parameters
const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(4);
const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams(64);
FlannBasedMatcher matcher(indexParams, searchParams);

for (int i = 0; i < trainCollection.size();i++){
    Training train = trainCollection.at(i);  
    Mat trainDescriptors(train.trainDescriptors);
    trainDescriptorCollection.push_back(trainDescriptors);
}
matcher.add(trainDescriptorCollection);
matcher.train();

//Now, we may do knnMatch (or anyother matching) 
matcher.knnMatch(queryDescriptors,matches,knn);

在上面的代码中,训练似乎是在调用 train() 函数时进行的(即构建了 kd-tree)。但是,如果我们查看 train() 函数的内部,这就是问题所在:

void FlannBasedMatcher::train()
{
    if( flannIndex.empty() || mergedDescriptors.size() < addedDescCount )
    {
        mergedDescriptors.set( trainDescCollection );
        flannIndex = new flann::Index( mergedDescriptors.getDescriptors(), *indexParams );
    }
}

这两个操作(设置训练描述符和 flann 索引,我在调用 train() 之前已经完成)。那么kd-tree究竟是什么时候建立的呢?

4

2 回答 2

3

When the code calls FlannBasedMatcher::train(),the index of FlannBasedMatcher will be built by

flannIndex = new flann::Index( mergedDescriptors.getDescriptors(), *indexParams );

The code

if( flannIndex.empty() || mergedDescriptors.size() < addedDescCount )

is to check whether the index of FlannBasedMatcher has already been built before.If index has been built before,the train() function will skip the process of building index to save time.

于 2013-03-12T13:41:26.450 回答
2

根据文档,每次匹配前都会进行培训(即在您的 cae 中构建 kd-tree)。该类cv::DescriptorMatcher在需要时自动调用训练方法。

于 2013-03-04T12:42:13.937 回答