Ask Your Question
0

Get nearest neighbors

asked 2019-01-31 07:41:45 -0600

xavier12358 gravatar image

Hello,

The problem we will discuss is pretty common, I want to search the nearest neighbors with Opencv. I found dozens of example but the program always sends me wrong informations.

here is the example I get:

std::vector<cv::Point2f> pointsForSearch;

for (int c = 100; c < 640; c += 100) {
    for (int l = 100; l < 640; l += 100) {
        pointsForSearch.emplace_back(cv::Point2f(float(c), float(l)));
    }
}

cv::Mat_<float> features(0,2);

 for(auto && point : pointsForSearch) {

   //Fill matrix
   cv::Mat row = (cv::Mat_<float>(1, 2) << point.x, point.y);
   features.push_back(row);
 }
 std::cout << features << std::endl;

 cv::flann::Index flann_index(features, cv::flann::KDTreeIndexParams(1));


 unsigned int max_neighbours = 10;
 cv::Mat query = (cv::Mat_<float>(1, 2) << 313.0, 245.6);
 cv::Mat indices, dists; //neither assume type nor size here !
 double radius= 2000.0;

 flann_index.radiusSearch(query, indices, dists, radius, max_neighbours,
     cv::flann::SearchParams(32));

 std::cerr << indices.type() << std::endl << indices << std::endl;
 std::cerr << dists.type() << std::endl << dists << std::endl;

The result is :

4
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
5
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

The result is alway zero. What is wrong with the previous example?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-01-31 13:38:23 -0600

berak gravatar image

updated 2019-02-01 02:09:50 -0600

try a larger radius (e.g. the distance between your query and pointsForSearch[13] is already > 2300)

as it seems, your Index is using the squared L2 distance (large values !)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-01-31 07:41:45 -0600

Seen: 678 times

Last updated: Jan 31 '19