Emgucv SURF特征点的寻找和匹配

Emgucv SURF特征点的寻找和匹配使用 SURF 类和其相关的函数需要添加 Emgu CV Features2D 和 Emgu CV XFeatures2D 这两个命名空间 同时也可以使用 cuda 模块来进行加速 由于使用 Cuda 需要安装 但 Cuda 目前支持的显卡驱动并不兼容 win10 所以这里我就简单的说一下通用的方法吧 这个代码里使用了 UMat UMat 应该算是 3 0 版本中的新特性 根据官方的说法 UMat 等价于 Opencv 中的 Mat 具

public partial class Form1 : Form { public Form1() { InitializeComponent(); Image 
  
    byte> a = 
   new Image 
   
     byte>( 
    "IMG__.JPG").Resize( 
    0.4,Inter.Area); 
    //模板 Image 
    
      byte> b = 
     new Image 
     
       byte>( 
      "DSC_0437.JPG").Resize( 
      0.4, Inter.Area); 
      //待匹配的图像 Mat homography = 
      null; Mat mask = 
      null; VectorOfKeyPoint modelKeyPoints = 
      new VectorOfKeyPoint(); VectorOfKeyPoint observedKeyPoints = 
      new VectorOfKeyPoint(); VectorOfVectorOfDMatch matches = 
      new VectorOfVectorOfDMatch(); UMat a1 = a.Mat.ToUMat(AccessType.Read); UMat b1 = b.Mat.ToUMat(AccessType.Read); SURF surf = 
      new SURF( 
      300); UMat modelDescriptors = 
      new UMat(); UMat observedDescriptors = 
      new UMat(); surf.DetectAndCompute(a1, 
      null, modelKeyPoints, modelDescriptors, 
      false); 
      //进行检测和计算,把opencv中的两部分和到一起了,分开用也可以 surf.DetectAndCompute(b1, 
      null, observedKeyPoints, observedDescriptors, 
      false); BFMatcher matcher = 
      new BFMatcher(DistanceType.L2); 
      //开始进行匹配 matcher.Add(modelDescriptors); matcher.KnnMatch(observedDescriptors, matches, 
      2, 
      null); mask = 
      new Mat(matches.Size, 
      1, DepthType.Cv8U, 
      1); mask.SetTo( 
      new MCvScalar( 
      255)); Features2DToolbox.VoteForUniqueness(matches, 
      0.8, mask); 
      //去除重复的匹配 
      int Count = CvInvoke.CountNonZero(mask); 
      //用于寻找模板在图中的位置 
      if (Count >= 
      4) { Count = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints, matches, mask, 
      1.5, 
      20); 
      if (Count >= 
      4) homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints, observedKeyPoints, matches, mask, 
      2); } Mat result = 
      new Mat(); Features2DToolbox.DrawMatches(a.Convert 
      
        byte>().Mat, modelKeyPoints, b.Convert 
       
         byte>().Mat, observedKeyPoints,matches, result, 
        new MCvScalar( 
        255, 
        0, 
        255), 
        new MCvScalar( 
        0, 
        255, 
        255), mask); 
        //绘制匹配的关系图 
        if (homography != 
        null) 
        //如果在图中找到了模板,就把它画出来 { Rectangle rect = 
        new Rectangle(Point.Empty, a.Size); PointF[] points = 
        new PointF[] { 
        new PointF(rect.Left, rect.Bottom), 
        new PointF(rect.Right, rect.Bottom), 
        new PointF(rect.Right, rect.Top), 
        new PointF(rect.Left, rect.Top) }; points = CvInvoke.PerspectiveTransform(points, homography); Point[] points2 = Array.ConvertAll 
        
          (points, Point.Round); VectorOfPoint vp = 
         new VectorOfPoint(points2); CvInvoke.Polylines(result, vp, 
         true, 
         new MCvScalar( 
         255, 
         0, 
         0, 
         255), 
         15); } imageBox1.Image = result; } } 
         
        
       
      
     
    
  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217891.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午8:32
下一篇 2026年3月18日 上午8:32


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号