当前位置:   article > 正文

C# emgu.cv图像识别 从大图中寻找小图,判断存在图的相似度_emgucv4.8中orbdetector实现比较两张图相似度

emgucv4.8中orbdetector实现比较两张图相似度

最近写了一个在一张图片中,截取一部分,通过机器判断截取图片是这张图片的。

也就是说:

第一、通过小图去找判断是否存在大图中,

第二、小图存在大图的什么位置

在这里插入图片描述
这是2个问题,我采用emgu.cv来解决这个识别问题,这里需要在nuget中引用emgu

第一个问题:

判断该小图是否存在大图中,这里我们最终结果输出一个匹配度:

/// <summary>
      /// 判断是否存在图片
      /// </summary>
      /// <param name="fatherImg"></param>
      /// <param name="childImg"></param>
      /// <param name="MaxLike"></param>
      /// <returns></returns>
      private bool IsHaveImage(string fatherImg, string childImg, double? MaxLike = null)
      {
          if (!MaxLike.HasValue)
              MaxLike = 0.99;
          Image<Bgr, byte> a = new Image<Bgr, byte>(fatherImg);
          Image<Bgr, byte> b = new Image<Bgr, byte>(childImg);
          Image<Gray, float> c = new Image<Gray, float>(a.Width, a.Height);

          c = a.MatchTemplate(b, TemplateMatchingType.CcorrNormed);
          double min = 0, max = 0;
          Point maxp = new Point(0, 0);
          Point minp = new Point(0, 0);
          CvInvoke.MinMaxLoc(c, ref min, ref max, ref minp, ref maxp);
          Console.WriteLine(min + " " + max);
          CvInvoke.Rectangle(a, new Rectangle(maxp, new Size(b.Width, b.Height)), new MCvScalar(0, 0, 255), 3);
          pictureBox1.Image = a.ToBitmap();

          if (max > MaxLike)
          {
              Console.WriteLine("true");
              return true;
          }
          else
          {
              Console.WriteLine("false");
              return false;
          }
      }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

第二,查找图片位置

/// <summary>
    /// 找图
    /// </summary>
    /// <param name="img1">大图</param>
    /// <param name="img2">小图</param>
    /// <returns></returns>
    public Rectangle GetMatchPos(string img1, string img2)
    {
        try
        {
            Mat Src = CvInvoke.Imread(img1, ImreadModes.Grayscale);
            Mat Template = CvInvoke.Imread(img2, ImreadModes.Grayscale);

            Mat MatchResult = new Mat();//匹配结果

            CvInvoke.MatchTemplate(Src, Template, MatchResult, TemplateMatchingType.CcorrNormed);//使用相关系数法匹配
            Point max_loc = new Point();
            Point min_loc = new Point();
            double max = 0, min = 0;
            CvInvoke.MinMaxLoc(MatchResult, ref min, ref max, ref min_loc, ref max_loc);//获得极值信息
            Console.WriteLine(min + " " + max);

            return new Rectangle(max_loc, Template.Size);
        }
        catch (Exception)
        {
            return new Rectangle(0, 0, 0, 0);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/287717
推荐阅读
相关标签
  

闽ICP备14008679号