Study Emgu VoteForUniqueness

Recently i was studying Emgu, but find there is a bug in the VoteForUniqueness function in class Features2DToolbox.

The idea of "VoteForUniqueness" is to filter ambiguous matchings. Lets say you want to match the points of image A with the points of image B.

Here is how it works :

  • "KnnMatch" is looking for 2-Nearest Neighbor for each point of image A in image B.
  • In image B, if the 1st and 2nd neighbors are too similar (the distance ratio is less than 0.8), we consider that we don't know which one is the best matching, so the matching is filtered (deleted).

Be awarded that here, when we speak about nearest neighbor and distance, we talk about the distance between the features of points (not the position).

According to the functionality of VoteForUniqueness, we need to fix a bug inside the function. 

When the n ratio of earest and the 2nd nearest is larger than the threshold, which means they are two similar, we need to remove that match by mask them out.

/// <summary>
/// Filter the matched Features, such that if a match is not unique, it is rejected.
/// </summary>
/// <param name="uniquenessThreshold">The distance different ratio which a match is consider unique, a good number will be 0.8</param>
/// <param name="mask">This is both input and output. This matrix indicates which row is valid for the matches.</param>
/// <param name="matches">Matches. Each matches[i] is k or less matches for the same query descriptor.</param> 
public static void VoteForUniqueness(VectorOfVectorOfDMatch matches, double uniquenessThreshold, Mat mask)
{
    MDMatch[][] mArr = matches.ToArrayOfArray();
    byte[] maskData = new byte[mArr.Length];
    GCHandle maskHandle = GCHandle.Alloc(maskData, GCHandleType.Pinned);
    using (Mat m = new Mat(mArr.Length, 1, DepthType.Cv8U, 1, maskHandle.AddrOfPinnedObject(), 1))
    {
        mask.CopyTo(m);
        for (int i = 0; i < mArr.Length; i++)
        {
            //Origianlly is mArr[i][0].Distance / mArr[i][1].Distance) < uniquenessThreshold
            if (maskData[i] != 0 && (mArr[i][0].Distance / mArr[i][1].Distance) >= uniquenessThreshold)
            {
                maskData[i] = (byte)0;  //if the distance is too similiar, then elimilate the feature by set mask to 0
            }
        }

        m.CopyTo(mask);
    }
    maskHandle.Free();
}
原文地址:https://www.cnblogs.com/shengguang/p/5176501.html