/// <summary>
/// Recover the homography matrix using RANDSAC. If the matrix cannot be recovered, null is returned.
/// </summary>
/// <param name="matchedFeatures">The Matched Features, only the first ModelFeature will be considered</param>
/// <returns>The homography matrix, if it cannot be found, null is returned</returns>
public static HomographyMatrix GetHomographyMatrixFromMatchedFeatures(MatchedSURFFeature[] matchedFeatures)
{
if (matchedFeatures.Length < 4)
{
return(null);
}
HomographyMatrix homography;
if (matchedFeatures.Length < _randsacRequiredMatch)
{ // Too few points for randsac, use 4 points only
PointF[] pts1 = new PointF[4];
PointF[] pts2 = new PointF[4];
for (int i = 0; i < 4; i++)
{
pts1[i] = matchedFeatures[i].SimilarFeatures[0].Feature.Point.pt;
pts2[i] = matchedFeatures[i].ObservedFeature.Point.pt;
}
homography = CameraCalibration.GetPerspectiveTransform(pts1, pts2);
}
else
{
//use randsac to find the Homography Matrix
PointF[] pts1 = new PointF[matchedFeatures.Length];
PointF[] pts2 = new PointF[matchedFeatures.Length];
for (int i = 0; i < matchedFeatures.Length; i++)
{
pts1[i] = matchedFeatures[i].SimilarFeatures[0].Feature.Point.pt;
pts2[i] = matchedFeatures[i].ObservedFeature.Point.pt;
}
homography = CameraCalibration.FindHomography(
pts1, //points on the model image
pts2, //points on the observed image
CvEnum.HOMOGRAPHY_METHOD.RANSAC,
3);
if (homography == null)
{
return(null);
}
}
if (homography.IsValid(10))
{
return(homography);
}
else
{
homography.Dispose();
return(null);
}
}