在什么阶段的训练恰恰发生在OpenCV中的FlannBasedMatcher? [英] At what stage the training exactly takes place in FlannBasedMatcher in OpenCV?

查看:1650
本文介绍了在什么阶段的训练恰恰发生在OpenCV中的FlannBasedMatcher?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code is in C++ and I am using OpenCV for my experiment. Suppose I am using kd-tree (FlannBasedMatcher) in the following way:

//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);

在上面的代码中,似乎训练发生(即kd- train()函数。但是这里是catch,如果我们看看train()函数:

In the above code it seems that training takes place (i.e. kd-tree is built) on calling the train() function. But here is the catch, if we look inside the train() function:

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

这两个操作(设置训练描述符和flann索引,我已经做了之前调用train())。

Both of these operations (setting training descriptors and flann index, I have already done before calling train()). So when exactly is the kd-tree built?

推荐答案

当代码调用FlannBasedMatcher :: train()时,FlannBasedMatcher的索引将由

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

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

代码

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

是检查FlannBasedMatcher的索引是否已经被建立。如果索引已经建立,train()函数将跳过建立索引的过程以节省时间。

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.

这篇关于在什么阶段的训练恰恰发生在OpenCV中的FlannBasedMatcher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆