/// <summary>
/// Matches two sets of points using RANSAC.
/// </summary>
///
/// <returns>The homography matrix matching x1 and x2.</returns>
///
public MatrixH Estimate(PointF[] points1, PointF[] points2)
{
// Initial argument checks
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");
}
// Normalize each set of points so that the origin is
// at centroid and mean distance from origin is sqrt(2).
MatrixH T1, T2;
this.pointSet1 = Tools.Normalize(points1, out T1);
this.pointSet2 = Tools.Normalize(points2, out T2);
d2 = new double[points1.Length];
// Compute RANSAC and find the inlier points
MatrixH H = ransac.Compute(points1.Length, out inliers);
if (inliers == null || inliers.Length < 4)
{
//throw new Exception("RANSAC could not find enough points to fit an homography.");
return(null);
}
// Compute the final homography considering all inliers
H = homography(inliers);
// Denormalize
H = T2.Inverse() * (H * T1);
return(H);
}