我写了一个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 …Run Code Online (Sandbox Code Playgroud)