K-means算法C#

时间:2018-06-03 10:06:22

标签: c# algorithm image-processing

我试图在C#中实现K-means算法,但不知何故,它的输出是黑色(小)图像。我写了以下代码:

public static Color[,] Kmeans(int number_clusters, Vector[,] input, int iterations)
        {
            int length = input.GetLength(0);
            int height = input.GetLength(1);

            Random randomizer = new Random();

            //Inits centroides
            List<Vector> centroides = new List<Vector>(number_clusters);
            List<List<Vector>> clusters = new List<List<Vector>>(number_clusters); 

            for(int i = 0; i < number_clusters; i++)
            {
                Vector centroid = input[randomizer.Next(0, length), randomizer.Next(0, height)];

                List<Vector> cluster = new List<Vector>();

                cluster.Add(centroid); 
                clusters.Add(cluster);

                centroides.Add(centroid); 
            }

            int count = 0;

            while (count < iterations) {
                for (int x = 0; x < input.GetLength(0); x++)
                {
                    for (int y = 0; y < input.GetLength(1); y++)
                    {
                        Vector currentVector = input[x, y];
                        double min_distance = Double.PositiveInfinity;
                        Vector closest_centroid = centroides[0];

                        //Finds closest centroid
                        foreach (Vector centroid in centroides)
                        {
                            double distance = currentVector.Distance(centroid);
                            if (Math.Pow(distance, 2) < min_distance)
                            {
                                min_distance = Math.Pow(distance, 2);
                                closest_centroid = centroid;
                            }
                        }

                        //Find cluster with that centroid in it
                        List<Vector> to_assign = clusters[0];
                        foreach (List<Vector> cluster_list in clusters)
                        {
                            if (cluster_list[0].r == closest_centroid.r && cluster_list[0].g == closest_centroid.g && cluster_list[0].b == closest_centroid.b)
                            {
                                to_assign = cluster_list;
                                break;
                            }
                        }

                        to_assign.Add(currentVector);
                        int current_length = to_assign.Count();
                        Vector current_centroid = to_assign[0];

                        Vector new_centroid = new Vector(0, 0, 0);

                        foreach (Vector vector in to_assign)
                        {
                            new_centroid.Sum(vector);
                        }

                        new_centroid = new Vector(new_centroid.r / current_length, new_centroid.g / current_length, new_centroid.b / current_length);
                        to_assign.RemoveAt(0);
                        to_assign.Insert(0, new_centroid);

                        for (int i = 0; i < centroides.Count(); i++)
                        {
                            if (centroides[i].r == current_centroid.r && centroides[i].g == current_centroid.g && current_centroid.b == centroides[i].b)
                            {
                                centroides[i] = new_centroid;
                                break;
                            }
                        }
                    }
                }
                count++; 
            }

            foreach(List<Vector> cluster_list in clusters)
            {
                Vector current_centroid = cluster_list[0]; 
                for(int i = 1; i < cluster_list.Count(); i++)
                {
                    cluster_list[i].r = current_centroid.r;
                    cluster_list[i].g = current_centroid.g;
                    cluster_list[i].b = current_centroid.b;
                }
            }

            Color[,] output = new Color[length, height]; 
            foreach (List<Vector> cluster_list in clusters)
            {
                for (int i = 0; i < cluster_list.Count(); i++)
                {
                    Vector current_vector = cluster_list[i];
                    output[current_vector.x, current_vector.y] = Color.FromArgb((int) current_vector.r, (int) current_vector.g, (int) current_vector.b); 
                }
            }

            return output; 
        }

Vector是以下类:

public class Vector
    {
        public double r, g, b;
        public int x, y;
        public Vector(double r, double g, double b)
        {
            this.r = r;
            this.g = g;
            this.b = b;
        }

        public void SetCoordinates(int x, int y)
        {
            this.x = x;
            this.y = y; 
        }

        public double Distance(Vector v2)
        {
            double r_power = Math.Pow((r - v2.r), 2);
            double g_power = Math.Pow((g - v2.g), 2);
            double b_power = Math.Pow((b - v2.b), 2);

            double distance = Math.Sqrt((r_power + g_power + b_power));

            return distance; 
        }

        public Vector Product(int scalar)
        {
            return new Vector(r * scalar, g * scalar, b * scalar); 
        }

        public double Length()
        {
            return Math.Sqrt((Math.Pow(r, 2) + Math.Pow(g, 2) + Math.Pow(b, 2))); 
        }

        public Vector Sum(Vector v2)
        {
            double new_r = r + v2.r; 
            double new_g = g + v2.g;
            double new_b = b + v2.b;

            return new Vector(new_r, new_g, new_b); 
        }
    }

我尝试通过以下算法描述实现代码:

将对象分配给群集: 对于每个对象 v 在测试集中做 以下步骤: 1 计算平方距离 之间 v 和每个质心 ķ 每个 集群( d 2 ( v , ķ ))。 2 分配对象 v 到最近的质心的簇。 更新质心: 对于每个群集 ķ 计算他们的平均向量。 平均向量的计算类似于数字的平均值: 我们对聚类中的所有向量求和(用向量和),然后除 (具有逆的标量积)由簇中的向量数。 重复上面的两个步骤

任何人都可以帮我修复此算法

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

    foreach (Vector vector in to_assign)
    {
        new_centroid.Sum(vector);
    }

应改为:

foreach (Vector vector in to_assign)
        {
            new_centroid = new_centroid.Sum(vector);
        }

这是因为Sum方法返回一个新的Vector,现在没有存储