NightFox
NightFox

Reputation: 125

Why OpenCV KdTree always returns same nearest neighbour in c++?

I have two sets of 2 dimensional points, set A & B. In set A, I have 100 points and set B contains 5000 points. For each point in set A, I want to find a nearest neighbor or the point closest to it from set B. I built a OpenCV kd-Tree on set B and used set A points as Query points.

The problem is that for all points in set A, Kd-tree always returns 1st point as the closest point. By looking at points I can see that there are other points much closer than 1st point of set B.

Here is some code:

Mat matches; //This mat will contain the index of nearest neighbour as returned by Kd-tree
Mat distances; //In this mat Kd-Tree return the distances for each nearest neighbour
Mat ClusterMemebers; //This Set A
Mat ClusterCenters;  //This set B
const cvflann::SearchParams params(32); //How many leaves to search in a tree
cv::flann::GenericIndex< cvflann::L2<int> > *kdtrees; // The flann searching tree

// Create matrices
ClusterCenters.create(cvSize(2,5000), CV_32S); // The set B
matches.create(cvSize(1,100), CV_32SC1);
distances.create(cvSize(1,100), CV_32FC1);
ClusterMembers.create(cvSize(2,100), CV_32S); // The set A
// After filling points in ClusterMembers (set A) and ClusterCenters (Set B)
// I create K-D tree
kdtrees =  new flann::GenericIndex< cvflann::L2<int> >(ClusterCenters, vflann::KDTreeIndexParams(4)); // a 4 k-d tree

// Search KdTree
kdtrees->knnSearch(ClusterMembers, matches, distances, 1,  cvflann::SearchParams(8));
int NN_index;
for(int l = 0; l < 100; l++)
{
    NN_index = matches.at<float>(cvPoint(l, 0));
    dist = distances.at<float>(cvPoint(l, 0));
}

The NN_index is always 0, which means 1st point.

Upvotes: 2

Views: 5055

Answers (1)

Bob Davies
Bob Davies

Reputation: 51

You forgot to initialize the ClusterMembers and ClusterCenters. There were some other mistakes so here is a working version of your simple test:

Mat matches; //This mat will contain the index of nearest neighbour as returned by Kd-tree
Mat distances; //In this mat Kd-Tree return the distances for each nearest neighbour
Mat ClusterMembers; //This Set A
Mat ClusterCenters;  //This set B
const cvflann::SearchParams params(32); //How many leaves to search in a tree
cv::flann::GenericIndex< cvflann::L2<int> > *kdtrees; // The flann searching tree

// Create matrices
ClusterMembers.create(cvSize(2,100), CV_32S); // The set A
randu(ClusterMembers, Scalar::all(0), Scalar::all(1000));
ClusterCenters.create(cvSize(2,5000), CV_32S); // The set B
randu(ClusterCenters, Scalar::all(0), Scalar::all(1000));
matches.create(cvSize(1,100), CV_32SC1);
distances.create(cvSize(1,100), CV_32FC1);
kdtrees =  new flann::GenericIndex< cvflann::L2<int> >(ClusterCenters, cvflann::KDTreeIndexParams(4)); // a 4 k-d tree

// Search KdTree
kdtrees->knnSearch(ClusterMembers, matches, distances, 1,  cvflann::SearchParams(8));
int NN_index;
float dist;
for(int i = 0; i < 100; i++) {
    NN_index = matches.at<int>(i,0);
    dist = distances.at<float>(i, 0);
}

Bob Davies

Upvotes: 5

Related Questions