FrameProcessor.kmeans C# (CSharp) Method

kmeans() private method

private kmeans ( Point last_center, Byte>.Image img0, Rectangle face, double scale ) : Point[]
last_center Point
img0 Byte>.Image
face Rectangle
scale double
return Point[]
    private Point[] kmeans(Point[] last_center,Image<Gray, Byte> img0, Rectangle face, double scale)
    {
        Image<Gray, Byte> img = img0.Resize(1/scale, INTER.CV_INTER_LINEAR);

        Point[] center = new Point[5] {new Point(img.Width / 4, img.Height / 2), new Point(img.Width * 3 / 4, img.Height / 2),new Point(),new Point(),new Point()};
        double[] x_accu = new double[5] { 0, 0 ,0,0,0};
        double[] y_accu = new double[5] { 0, 0,0,0,0};
        double[] mass = new double[5] { 0, 0,0,0,0};
        int n = 2;
        if (last_center[0].X != 0 || last_center[0].Y != 0)
        {
            for (int i = 0; i < 2; i++)
            {
                //center[i].X = (int)((double)last_center[i].X / scale);
                //center[i].Y = (int)((double)last_center[i].Y / scale);
            }
        }

        n = dummy_center(center, img.Size);

        bool term = false;
        for (int iter = 0; iter < 10; iter++)
        {
            if (term)
            {
                break;
            }
            for (int x = 0; x < img.Width; x++)
            {
                for (int y = 0; y < img.Height; y++)
                {
                    if (img.Data[y,x,0]==0)
                    {
                        continue;
                    }
                    double min = 1E10;
                    int minj = 0;
                    for (int j = 0; j < n; j++)
                    {
                        double temp = (x - center[j].X) * (x - center[j].X) + (y - center[j].Y) * (x - center[j].Y);
                        if (min > temp)
                        {
                            min = temp;
                            minj = j;
                        }
                    }
                    mass[minj] += img.Data[y,x,0];
                    x_accu[minj] += img.Data[y, x, 0] * x;
                    y_accu[minj] += img.Data[y, x, 0] * y;
                }
            }
            for (int j = 0; j < 2; j++)
            {
                if (mass[j] != 0)
                {
                    center[j] = new Point((int)(x_accu[j] / mass[j]), (int)(y_accu[j] / mass[j]));
                }
                x_accu[j] = 0;
                y_accu[j] = 0;
                mass[j] = 0;
            }
            n = dummy_center(center, img.Size);
        }
        for (int j = 0; j < 5; j++)
        {
            center[j].X = (int)(scale * center[j].X);
            center[j].Y = (int)(scale * center[j].Y);
        }
        return center;
    }