Beispiel #1
0
        static private void FillArea(int[,] Result, int x, int y, int ResultC, int w, int h)
        {
            Stack <MyShortPoint> Q = new Stack <MyShortPoint>();
            int startC             = Result[x, y];

            Q.Push(new MyShortPoint(x, y));
            while (Q.Count != 0)
            {
                MyShortPoint top = Q.Peek();
                Result[top.x, top.y] = ResultC;
                Q.Pop();
                for (int i = -1; i <= 1; i++)
                {
                    for (int j = -1; j <= 1; j++)
                    {
                        if (i == 0 && j == 0)
                        {
                            continue;
                        }
                        if (Math.Abs(i) + Math.Abs(j) == 2)
                        {
                            continue;
                        }
                        if (ValidCoords(top.x + i, top.y + j, w, h) && Result[top.x + i, top.y + j] == startC)
                        {
                            Q.Push(new MyShortPoint(top.x + i, top.y + j));
                        }
                    }
                }
            }
        }
Beispiel #2
0
        static private bool IsEnclosed(int[,] Result, int x, int y, bool[,] Visited, out int second, int w, int h)
        {
            second = -2;
            bool was               = false;
            int  startC            = Result[x, y];
            Stack <MyShortPoint> Q = new Stack <MyShortPoint>();

            Q.Push(new MyShortPoint(x, y));
            while (Q.Count != 0)
            {
                MyShortPoint top = Q.Peek();
                if (Result[top.x, top.y] == second)
                {
                    continue;
                }

                Q.Pop();

                if (was && Result[top.x, top.y] != startC && Result[top.x, top.y] != second)
                {
                    second = -2;
                    return(false);
                }
                if (Visited[top.x, top.y] || Result[top.x, top.y] != startC)
                {
                    continue;
                }
                Visited[top.x, top.y] = true;
                for (int i = -1; i <= 1; i++)
                {
                    for (int j = -1; j <= 1; j++)
                    {
                        if (i == 0 && j == 0)
                        {
                            continue;
                        }
                        if (Math.Abs(i) + Math.Abs(j) == 2)
                        {
                            continue;
                        }
                        if (ValidCoords(top.x + i, top.y + j, w, h))
                        {
                            if (!Visited[top.x + i, top.y + j] && Result[top.x + i, top.y + j] == startC)
                            {
                                Q.Push(new MyShortPoint(top.x + i, top.y + j));
                                continue;
                            }
                            if (!was && Result[top.x + i, top.y + j] != startC)
                            {
                                was = true;

                                second = Result[top.x + i, top.y + j];
                                continue;
                            }
                            if (was && Result[top.x + i, top.y + j] != startC && Result[top.x + i, top.y + j] != second)
                            {
                                second = -2;
                            }
                        }
                    }
                }
            }

            return(true);
        }
Beispiel #3
0
        static public AnyMatrix <Double> GetBestFitEllipse(List <MyShortPoint> Points)
        {
            int numPoints            = Points.Count;
            RectangularMatrix D1     = new RectangularMatrix(numPoints, 3);
            RectangularMatrix D2     = new RectangularMatrix(numPoints, 3);
            SquareMatrix      S1     = new SquareMatrix(3);
            SquareMatrix      S2     = new SquareMatrix(3);
            SquareMatrix      S3     = new SquareMatrix(3);
            SquareMatrix      T      = new SquareMatrix(3);
            SquareMatrix      M      = new SquareMatrix(3);
            SquareMatrix      C1     = new SquareMatrix(3);
            RectangularMatrix a1     = new RectangularMatrix(3, 1);
            RectangularMatrix a2     = new RectangularMatrix(3, 1);
            RectangularMatrix result = new RectangularMatrix(6, 1);
            RectangularMatrix temp;

            C1[0, 0] = 0;
            C1[0, 1] = 0;
            C1[0, 2] = 0.5;
            C1[1, 0] = 0;
            C1[1, 1] = -1;
            C1[1, 2] = 0;
            C1[2, 0] = 0.5;
            C1[2, 1] = 0;
            C1[2, 2] = 0;
            //2 D1 = [x .? 2, x .* y, y .? 2]; % quadratic part of the design matrix
            //3 D2 = [x, y, ones(size(x))]; % linear part of the design matrix
            for (int xx = 0; xx < Points.Count; xx++)
            {
                MyShortPoint p = Points[xx];
                D1[xx, 0] = p.x * p.x;
                D1[xx, 1] = p.x * p.y;
                D1[xx, 2] = p.y * p.y;
                D2[xx, 0] = p.x;
                D2[xx, 1] = p.y;
                D2[xx, 2] = 1;
            }
            //4 S1 = D1’ * D1; % quadratic part of the scatter matrix
            temp = D1.Transpose() * D1;
            for (int xx = 0; xx < 3; xx++)
            {
                for (int yy = 0; yy < 3; yy++)
                {
                    S1[xx, yy] = temp[xx, yy];
                }
            }
            //5 S2 = D1’ * D2; % combined part of the scatter matrix
            temp = D1.Transpose() * D2;
            for (int xx = 0; xx < 3; xx++)
            {
                for (int yy = 0; yy < 3; yy++)
                {
                    S2[xx, yy] = temp[xx, yy];
                }
            }
            //6 S3 = D2’ * D2; % linear part of the scatter matrix
            temp = D2.Transpose() * D2;
            for (int xx = 0; xx < 3; xx++)
            {
                for (int yy = 0; yy < 3; yy++)
                {
                    S3[xx, yy] = temp[xx, yy];
                }
            }
            //7 T = – inv(S3) * S2’; % for getting a2 from a1
            T = -1 * S3.Inverse() * S2.Transpose();
            //8 M = S1 + S2 * T; % reduced scatter matrix
            M = S1 + S2 * T;
            //9 M = [M(3,  ./ 2; - M(2,  ; M(1,  ./ 2]; % premultiply by inv(C1)
            M = C1 * M;
            //10 [evec, eval] = eig(M); % solve eigensystem
            ComplexEigensystem eigenSystem = M.Eigensystem();

            //11 cond = 4 * evec(1,  .* evec(3,  – evec(2,  .? 2; % evaluate a’Ca
            //12 a1 = evec(:, find(cond > 0)); % eigenvector for min. pos. eigenvalue
            for (int xx = 0; xx < eigenSystem.Dimension; xx++)
            {
                Complex[] vector = eigenSystem.Eigenvector(xx);

                Complex condition = 4 * vector[0] * vector[2] - vector[1] * vector[1];
                if (condition.Im == 0 && condition.Re > 0)
                {
                    // Solution is found
                    // Console.WriteLine(“\nSolution Found!”);

                    //System.Windows.Forms.MessageBox.Show("Ellipse gOT");

                    for (int yy = 0; yy < vector.Length; yy++)
                    {
                        a1[yy, 0] = vector[yy].Re;
                    }
                }
            }
            //13 a2 = T * a1; % ellipse coefficients
            a2 = T * a1;
            //14 a = [a1; a2]; % ellipse coefficients
            result[0, 0] = a1[0, 0];
            result[1, 0] = a1[1, 0];
            result[2, 0] = a1[2, 0];
            result[3, 0] = a2[0, 0];
            result[4, 0] = a2[1, 0];
            result[5, 0] = a2[2, 0];
            return(result);
        }