在 KD 树中查找最近的邻居
Finding nearest neighbor(s) in a KD Tree
警告:问题相当长,也许太长了。如果是这样,我深表歉意。
我正在开发一个程序,涉及 kd 树的最近邻搜索(在本例中,它是一个具有 3961 个单独点的 11 维树(。我们才刚刚了解它们,虽然我对树的作用有很好的了解,但在最近的邻居搜索方面,我感到非常困惑。
我设置了一个 2D 点数组,每个点都包含一个质量和一个位置,如下所示。
struct point{
double quality;
double location;
}
// in main
point **parray;
// later points to an array of [3961][11] points
然后,我翻译了数据,使其均值为零,并根据单位方差重新调整了数据。我不会发布代码,因为它对我的问题并不重要。之后,我以随机顺序将点构建到树中,如下所示:
struct Node {
point *key;
Node *left;
Node *right;
Node (point *k) { key = k; left = right = NULL; }
};
Node *kd = NULL;
// Build the data into a kd-tree
random_shuffle(parray, &parray[n]);
for(int val=0; val<n; val++) {
for(int dim=1; dim<D+1; dim++) {
kd = insert(kd, &parray[val][dim], dim);
}
}
非常标准的东西。如果我错误地使用了random_shuffle((,或者我的树的结构本身有什么错误,请告诉我。它应该洗牌阵列的第一个维度,同时保持每个维度的 11 个维度按顺序排列且保持不变。
现在我进入了 neighbor(( 函数,这就是我感到困惑的地方。
neighbor(( 函数(后半部分是伪代码,坦率地说,我不知道从哪里开始(:
Node *neighbor (Node *root, point *pn, int d,
Node *best, double bestdist) {
double dist = 0;
// Recursively move down tree, ignore the node we are comparing to
if(!root || root->key == pn) return NULL;
// Dist = SQRT of the SUMS of SQUARED DIFFERENCES of qualities
for(int dim=1; dim<D+1; dim++)
dist += pow(pn[d].quality - root->key->quality, 2);
dist = sqrt(dist);
// If T is better than current best, current best = T
if(!best || dist<bestdist) {
bestdist = dist;
best = root;
}
// If the dist doesn't reach a plane, prune search, walk back up tree
// Else traverse down that tree
// Process root node, return
}
这是main((中对邻居的调用,大部分未完成。我不确定main((中应该有什么,neighbor((函数中应该有什么:
// Nearest neighbor(s) search
double avgdist = 0.0;
// For each neighbor
for(int i=0; i<n; i++) {
// Should this be an array/tree of x best neighbors to keep track of them?
Node *best;
double bestdist = 1000000000;
// Find nearest neighbor(s)?
for(int i=0; i<nbrs; i++) {
neighbor(kd, parray[n], 1, best, &bestdist);
}
// Determine "distance" between the two?
// Add to total dist?
avgdist += bestdist;
}
// Average the total dist
// avgdist /= n;
如您所见,我被困在最后两段代码上。几天来我一直在为此绞尽脑汁,但我仍然被困住了。它很快就会到期,所以当然感谢任何和所有的帮助。提前谢谢。
kd树不涉及洗牌。
事实上,您将需要使用排序(或更好的快速选择(来构建树。
首先为最近的邻居 (1NN( 求解它。一旦你有了这部分工作,通过保留一堆顶级候选者,并使用第 k 个点进行修剪,应该相当清楚如何找到 kNN。
相关文章:
- 查找最近配对时的OpenMP竞赛条件
- 如何获取pcl迭代最近点(ICP)的迭代点数?
- 优化在 Z^3 中查找邻居
- 如何找到给定点的最近平方
- 将消息从节点发送到其邻居
- 将前向声明的结构替换为最近定义的结构
- Nanoflann发现邻居提出段错误
- 应该如何编写用于计算最近点距离的C++函数?
- 最近的邻居用nanoflann搜索
- GCC:--静态链接到pthread的整个存档配方在最近的GCC版本中停止工作
- 向下四舍五入到五个c++的最近倍数
- 在 3D 点云、GPU 中查找最近的邻居
- 多线程程序中的K最近的邻居
- C++-使用opencv-frann查找最近的邻居
- 优化最近邻居大小调整算法以提高速度
- 在 KD 树中查找最近的邻居
- 查找最近的邻居-OpenCV
- 是否存在使用用户提供的提示的最近邻居数据结构
- c++一个固定大小的优先级队列,用于存储k个最近邻居
- 在direct3D9中有没有一种方法可以通过最近的邻居来缩放精灵