示例#1
0
    //
    // Interface

    public static void RegressEllipsePoints(Point[] points,
                                            out double x2, out double xy, out double y2, out double x1, out double y1, out double c0)
    {
        int n = points.Length;

        // Solve using QrDecomposition (least-squares regression)
        LeastSquaresRegression.Matrix lhs = new LeastSquaresRegression.Matrix(n, 5);

        for (int i = 0; i < n; ++i)
        {
            Point p = points[i];
            lhs[i, 0] = p.X * p.X;
            lhs[i, 1] = p.X * p.Y;
            lhs[i, 2] = p.Y * p.Y;
            lhs[i, 3] = p.X;
            lhs[i, 4] = p.Y;
        }

        LeastSquaresRegression.Matrix rhs = new LeastSquaresRegression.Matrix(n, 1);
        for (int i = 0; i < n; ++i)
        {
            rhs[i, 0] = -1.0;
        }

        LeastSquaresRegression.Matrix sln = lhs.Solve(rhs);

        x2 = sln[0, 0]; xy = sln[1, 0]; y2 = sln[2, 0]; x1 = sln[3, 0]; y1 = sln[4, 0]; c0 = 1.0;

        EllipseAnalysis.NormalizeCoeffs(ref x2, ref xy, ref y2, ref x1, ref y1, ref c0);
    }
示例#2
0
    public static Ellipse FromRegression(Point[] points)
    {
        double x2, xy, y2, x1, y1, c0;

        EllipseAnalysis.RegressEllipsePoints(points, out x2, out xy, out y2, out x1, out y1, out c0);

        if (!EllipseAnalysis.IsConicAnEllipse(x2, xy, y2, x1, y1, c0))
        {
            return(new Ellipse(0, 0, 0, 0, 0f));        //throw new ApplicationException("Error regressing ellipse: parameters non-conic.");
        }
        double cx, cy, mj, mn, th;

        EllipseAnalysis.ReduceConic(x2, xy, y2, x1, y1, c0, out cx, out cy, out mj, out mn, out th);

        return(new Ellipse((int)cx, (int)cy, (int)mj, (int)mn, (float)Geometry.Rad2Deg(th)));
    }