Ask Your Question

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

asked 2013-02-23 22:41:11 -0600

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 =;  
    Mat trainDescriptors(train.trainDescriptors);

//Now, we may do knnMatch (or anyother matching) 

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-02-24 08:00:07 -0600

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.

edit flag offensive delete link more


True. This is the line where index is built:

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

magarwal gravatar imagemagarwal ( 2013-04-16 21:20:06 -0600 )edit

Question Tools

1 follower


Asked: 2013-02-23 22:41:11 -0600

Seen: 4,813 times

Last updated: Feb 24 '13