[英]Closest point algorithm | How to improve it?
我写了一个k-means聚类算法和一个颜色量化算法。 它们在结果方面按预期工作,但我想让它们更快。 在两个实现中,我需要解决一个问题:3D空间中有两个点阵列,然后对于第一个阵列的每个点,您需要找到距离第二个阵列最近的点。 我是这样做的:
size_t closest_cluster_index;
double x_dif, y_dif, z_dif;
double old_distance;
double new_distance;
for (auto point = points.begin(); point != points.end(); point++)
{
//FIX
//as suggested by juvian
//K = 1
if (point != points.begin())
{
auto cluster = &(clusters[closest_cluster_index]);
r_dif = cluster->r - point->r;
g_dif = cluster->g - point->g;
b_dif = cluster->b - point->b;
new_distance = r_dif * r_dif + g_dif * g_dif + b_dif * b_dif;
if (new_distance <= std::sqrt(old_distance) - ColorU8::differenceRGB(*(point - 1), *point))
{
old_distance = new_distance;
//do sth with closest_cluster_index;
continue;
}
}
//END OF FIX
old_distance = std::numeric_limits<double>::infinity();
for (auto cluster = clusters.begin(); cluster != clusters.end(); cluster++)
{
x_dif = cluster->x - point->x;
y_dif = cluster->y - point->y;
z_dif = cluster->z - point->z;
new_distance = x_dif * x_dif + y_dif * y_dif + z_dif * z_dif;
if (new_distance < old_distance)
{
old_distance = new_distance;
closest_cluster_index = cluster - clusters.begin();
}
}
//do sth with: closest_cluster_index
}
我怎样才能改进它? (我不想让它多线程或由GPU计算)
有效的最近邻居查询有多种数据结构。 对于3d, kdtree工作得很好,并且平均每个查询的复杂度为O(log n),这将改善您当前的O(n)。
因此,使用此结构,您可以将所有点从群集添加到它,然后对于每个点的点,您可以使用该结构查询最近的点。 对于您的特定情况,静态kdtree就足够了,因为您不需要更新点。
另一种方法 :
我们可以尝试冒险在某些点上进行额外的计算,以换取其他点上的更少。 这种方法应该适用于以下假设:
我认为这些适用于您的情况,因为您的聚类很少颜色,而您的点来自真实图像,相邻像素之间的颜色往往相似。
对于每个点,创建一个堆。 而不是存储最近的集群,而是在最大堆中存储最接近的k个集群。 当您转到下一点时,我们可以使用此信息。 让我们称这个点P及其第k个最近的聚类C.
现在换一个新点P2,在与所有集群比较之前,我们将检查最接近P2的集群是否在我们的堆中。 只有当堆中的任何簇与P2之间的距离<=距离(P,C) - 距离(P,P2)时,才会出现这种情况。 当这成立时,我们只能检查堆而不是所有簇。 当它不是真的时,我们将比较所有并重建我们的堆,P将是P2。
您需要尝试不同的k值才能看出它是否有所改善。 对于K = 2的情况,可能值得避免增加堆的复杂性并且只使用变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.