In 2D space, an affine transformation can be defined by two sets of three nonlinear two-dimensional points. In C #, you can use the following procedure to calculate the corresponding matrix:
public static Matrix fit(PointF[] src, PointF[] dst) { Matrix m1 = new Matrix(new RectangleF(0, 0, 1, 1), src); m1.Invert(); Matrix m2 = new Matrix(new RectangleF(0, 0, 1, 1), dst); m2.Multiply(m1); return m2; }
It works for both array arguments having 3 elements.
If you only need rotation and translation, you can use the following procedure:
public static Matrix fitOrt(PointF src1, PointF src2, PointF dst1, PointF dst2) { return fit(new PointF[] { src1, src2, ort(src1, src2) }, new PointF[] { dst1, dst2, ort(dst1, dst2) }); } public static PointF ort(PointF p, PointF q) { return new PointF(pX + qY - pY, pY - qX + pX); }
If you want to find the best approximation between two sets of several points, you can start with this http://elonen.iki.fi/code/misc-notes/affine-fit/
source share