65,210
社区成员
发帖
与我相关
我的任务
分享public void FindMatch(Image<Gray, Byte> modelImage, Image<Gray,byte> observedImage, out long matchTime, out VectorOfKeyPointmodelKeyPoints, out VectorOfKeyPoint observedKeyPoints, out Matrix<int>indices, out Matrix<byte> mask, out HomographyMatrix homography)
{
int k = 2;
double uniquenessThreshold = 0.8;
SURFDetector surfCPU = newSURFDetector(500, false);
Stopwatch watch;
homography = null;
//extract features from the objectimage
modelKeyPoints = newVectorOfKeyPoint();
Matrix<float>modelDescriptors = surfCPU.DetectAndCompute(modelImage, null, modelKeyPoints);
watch = Stopwatch.StartNew();
// extract features from theobserved image
observedKeyPoints = newVectorOfKeyPoint();
Matrix<float>observedDescriptors = surfCPU.DetectAndCompute(observedImage, null,observedKeyPoints);
BruteForceMatcher<float>matcher = new BruteForceMatcher<float>(DistanceType.L2);
matcher.Add(modelDescriptors);
indices = newMatrix<int>(observedDescriptors.Rows, k);
using (Matrix<float> dist =new Matrix<float>(observedDescriptors.Rows, k))
{
matcher.KnnMatch(observedDescriptors, indices, dist, k, null);
mask = newMatrix<byte>(dist.Rows, 1);
mask.SetValue(255);
Features2DToolbox.VoteForUniqueness(dist, uniquenessThreshold, mask);
}
int nonZeroCount =CvInvoke.cvCountNonZero(mask);
if (nonZeroCount >= 4)
{
nonZeroCount =Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints,indices, mask, 1.5, 20);
if (nonZeroCount >= 4)
homography =Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,observedKeyPoints, indices, mask, 2);
}
watch.Stop();
matchTime = watch.ElapsedMilliseconds;
}