C# emgu.cv图像识别 从大图中寻找小图,判断存在图的相似度

13 篇文章 0 订阅

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

也就是说:

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

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

在这里插入图片描述
这是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;
          }
      }

第二,查找图片位置

/// <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);
        }
    }
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值