//Compares two objects for equality. public override bool Equals(object obj) { if (obj is PointH) { PointH p = (PointH)obj; if (_px / _pw == p._px / p._pw && _py / _pw == p._py / p._pw) { return(true); } } return(false); }
//Transforms the given points using this transformation matrix. public PointH[] TransformPoints(params PointH[] points) { PointH[] r = new PointH[points.Length]; for (int j = 0; j < points.Length; j++) { r[j].X = Elements[0] * points[j].X + Elements[1] * points[j].Y + Elements[2] * points[j].W; r[j].Y = Elements[3] * points[j].X + Elements[4] * points[j].Y + Elements[5] * points[j].W; r[j].W = Elements[6] * points[j].X + Elements[7] * points[j].Y + points[j].W; } return(r); }
//Converts to a Integer point by truncating the point coordinates. public static Point Truncate(PointH point) { return(new Point( (int)System.Math.Truncate(point._px / point._pw), (int)System.Math.Truncate(point._py / point._pw))); }
//Converts to a Integer point by rounding the point coordinates. public static Point Round(PointH point) { return(new Point( (int)System.Math.Round(point._px / point._pw), (int)System.Math.Round(point._py / point._pw))); }
//Converts to a Integer point by computing the ceiling of the point coordinates. public static Point Ceiling(PointH point) { return(new Point( (int)System.Math.Ceiling(point._px / point._pw), (int)System.Math.Ceiling(point._py / point._pw))); }
/// <summary> /// Creates an homography matrix matching points /// from a set of points to another. /// </summary> public static MatrixH Homography(PointH[] points1, PointH[] points2) { // Initial argument checkings if (points1.Length != points2.Length) { throw new ArgumentException("The number of points should be equal."); } if (points1.Length < 4) { throw new ArgumentException("At least four points are required to fit an homography"); } int N = points1.Length; MatrixH T1, T2; // Normalize input points points1 = points1.Normalize(out T1); points2 = points2.Normalize(out T2); // Create the matrix A double[,] A = new double[3 * N, 9]; for (int i = 0; i < N; i++) { PointH X = points1[i]; double x = points2[i].X; double y = points2[i].Y; double w = points2[i].W; int r = 3 * i; A[r, 0] = 0; A[r, 1] = 0; A[r, 2] = 0; A[r, 3] = -w * X.X; A[r, 4] = -w * X.Y; A[r, 5] = -w * X.W; A[r, 6] = y * X.X; A[r, 7] = y * X.Y; A[r, 8] = y * X.W; r++; A[r, 0] = w * X.X; A[r, 1] = w * X.Y; A[r, 2] = w * X.W; A[r, 3] = 0; A[r, 4] = 0; A[r, 5] = 0; A[r, 6] = -x * X.X; A[r, 7] = -x * X.Y; A[r, 8] = -x * X.W; r++; A[r, 0] = -y * X.X; A[r, 1] = -y * X.Y; A[r, 2] = -y * X.W; A[r, 3] = x * X.X; A[r, 4] = x * X.Y; A[r, 5] = x * X.W; A[r, 6] = 0; A[r, 7] = 0; A[r, 8] = 0; } // Create the singular value decomposition SingularValueDecomposition svd = new SingularValueDecomposition(A, false, true); double[,] V = svd.RightSingularVectors; // Extract the homography matrix MatrixH H = new MatrixH((float)V[0, 8], (float)V[1, 8], (float)V[2, 8], (float)V[3, 8], (float)V[4, 8], (float)V[5, 8], (float)V[6, 8], (float)V[7, 8], (float)V[8, 8]); // Denormalize H = T2.Inverse().Multiply(H.Multiply(T1)); return(H); }