找到最近的邻居 - OpenCV

Uda*_*aya 8 c++ opencv

我有一组openCV Point2f类型的图像点(坐标).我想找到该组中每个点的4个最近邻居.openCV中是否有任何特定的内置函数来执行此操作,还是应该测量每个点之间的距离并确定最接近的四个?

Uda*_*aya 9

以下代码将有助于从一组点中找到所选点的最近邻居.

vector<Point2f> pointsForSearch; //Insert all 2D points to this vector
flann::KDTreeIndexParams indexParams;
flann::Index kdtree(Mat(pointsForSearch).reshape(1), indexParams);
vector<float> query;
query.push_back(pnt.x); //Insert the 2D point we need to find neighbours to the query
query.push_back(pnt.y); //Insert the 2D point we need to find neighbours to the query
vector<int> indices;
vector<float> dists;
kdtree.radiusSearch(query, indices, dists, range, numOfPoints);
Run Code Online (Sandbox Code Playgroud)

indices给出了所选邻居的索引,dists给出了所选邻居的距离.


sie*_*hie 1

您可以使用 k 最近邻分类器CvKNearest。使用所有点训练分类器后,您可以k通过调用函数获得最近的邻居 CvKNearest::find_nearest