First time here? Check out the FAQ!

Ask Your Question
0

When does the training exactly takes place in FlannBasedMatcher in OpenCV?

asked Feb 24 '13

magarwal gravatar image

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

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

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?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Feb 24 '13

Guanta gravatar image

The flann::Index()-Contructor which is called in train builds (trains) your kd-tree. Before that, you have only specified the parameters and added all training-descriptor.

Preview: (hide)

Comments

True. This is the line where index is built:

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

magarwal gravatar imagemagarwal (Apr 17 '13)edit

Question Tools

1 follower

Stats

Asked: Feb 24 '13

Seen: 4,899 times

Last updated: Feb 24 '13