/// <summary>
/// Similar to cvInitUndistortRectifyMap and is opposite to it at the same time.
/// The functions are similar in that they both are used to correct lens distortion and to perform the optional perspective (rectification) transformation.
/// They are opposite because the function cvInitUndistortRectifyMap does actually perform the reverse transformation in order to initialize the maps properly, while this function does the forward transformation.
/// </summary>
/// <param name="src">The observed point coordinates</param>
/// <param name="R">Optional rectification transformation in object space (3x3 matrix). R1 or R2, computed by cvStereoRectify can be passed here. If null, the identity matrix is used.</param>
/// <param name="P">Optional new camera matrix (3x3) or the new projection matrix (3x4). P1 or P2, computed by cvStereoRectify can be passed here. If null, the identity matrix is used.</param>
public PointF[] Undistort(PointF[] src, Matrix <double> R, Matrix <double> P)
{
PointF[] dst = new PointF[src.Length];
GCHandle srcHandle = GCHandle.Alloc(src, GCHandleType.Pinned);
GCHandle dstHandle = GCHandle.Alloc(dst, GCHandleType.Pinned);
using (Matrix <float> srcPointMatrix = new Matrix <float>(src.Length, 1, 2, srcHandle.AddrOfPinnedObject(), 2 * sizeof(float)))
using (Matrix <float> dstPointMatrix = new Matrix <float>(dst.Length, 1, 2, dstHandle.AddrOfPinnedObject(), 2 * sizeof(float)))
{
CvInvoke.cvUndistortPoints(
srcPointMatrix, dstPointMatrix,
_intrinsicMatrix.Ptr,
_distortionCoeffs.Ptr,
R,
P);
}
srcHandle.Free();
dstHandle.Free();
return(dst);
}