private void Transform(double[] srcPoints) { const int POINT_COUNT = 8; System.Diagnostics.Debug.Assert(srcPoints.Length == POINT_COUNT); double leftOffset = (srcGrid.Width - imgRaw.Source.Width) / 2; double topOffset = (srcGrid.Height - imgRaw.Source.Height) / 2; CvMat srcPointsMat = new CvMat(4, 2, MatrixType.F64C1, srcPoints); CvMat dstPointsMat = new CvMat(4, 2, MatrixType.F64C1, new double[POINT_COUNT] { dstGrid.Width * 1 / 4, dstGrid.Height * 1 / 4, dstGrid.Width * 3 / 4, dstGrid.Height * 1 / 4, dstGrid.Width * 3 / 4, dstGrid.Height * 3 / 4, dstGrid.Width * 1 / 4, dstGrid.Height * 3 / 4 }); CvMat viewerHomographyMatrix = new CvMat(3, 3, MatrixType.F64C1, new double[9]); Cv.FindHomography(srcPointsMat, dstPointsMat, viewerHomographyMatrix); CV.Mat src = WriteableBitmapConverter.ToMat((WriteableBitmap)imgRaw.Source); CV.Mat dst = new CV.Mat((int)srcGrid.Height, (int)srcGrid.Width, src.Type()); Cv.WarpPerspective(src.ToCvMat(), dst.ToCvMat(), viewerHomographyMatrix); imgTransformed.Source = WriteableBitmapConverter.ToWriteableBitmap(dst); srcPointsMat.Dispose(); dstPointsMat.Dispose(); src.Dispose(); dst.Dispose(); }