.net 中使用OpenCvSharp 判断一张图片中是否包含指定图标

1. 添加包引用

<ItemGroup>
    <PackageReference Include="OpenCvSharp4" Version="4.7.0.20230115" />
    <PackageReference Include="OpenCvSharp4.Extensions" Version="4.7.0.20230115" />
    <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.7.0.20230115" />
  </ItemGroup>

2.具体代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using OpenCvSharp.XFeatures2D;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using Point = OpenCvSharp.Point;

namespace ConsoleApp9
{
    internal class Program
    {
        static void Main(string[] args)
        {


            var isFind = FindPicFromImage(@"C:\Users\63537\Desktop\新建文件夹 (2)\douyinda.jpg", @"C:\Users\63537\Desktop\新建文件夹 (2)\新建文件夹\douyin2.png");
            if (isFind)
            {
                Console.WriteLine("找到");
            }
            else
            {
                Console.WriteLine("未找到");
            }

            Console.ReadKey();

        }
        /// <summary>
        /// 查找一张图片中是否包含指定图标
        /// </summary>
        /// <param name="sourceImgPath">源图</param>
        /// <param name="picImgPath">要查找的图标</param>
        /// <param name="threshold">准确度(越接近1准确度越高)</param>
        /// <param name="imgWidth">源图宽度(在源图上截取指定图标时源图的宽度,后期可能要对比的源图大小会不一样,例如同一个画面在不同手机下的截图大小应该不一样,这时要指定一个尺寸作为对比)</param>
        /// <returns></returns>
        public static bool FindPicFromImage(string sourceImgPath, string picImgPath, double threshold = 0.9, double imgWidth = 0)
        {
            try
            {
                
                using Mat source = new Mat(sourceImgPath);
                if (imgWidth != 0)
                {
                    //如果源图宽度不等于对比图的宽度则按照指定宽度等比例缩放(提高识别率)
                    if (source.Width != imgWidth)
                    {
                        Cv2.Resize(source, source, new OpenCvSharp.Size(imgWidth, imgWidth * source.Height / (double)source.Width));
                    }
                }

                using Mat pic = new Mat(picImgPath, ImreadModes.AnyColor);
                using Mat result = new Mat();
                //模板匹配
                Cv2.MatchTemplate(source, pic, result, TemplateMatchModes.CCoeffNormed);
                Point minLoc, maxLoc;
                double minValue, maxValue;
                Cv2.MinMaxLoc(result, out minValue, out maxValue, out minLoc, out maxLoc);
                Console.WriteLine(maxValue);
                if (maxValue >= threshold)
                {
                    using Mat mask = source.Clone();
                    //画框显示
                    Cv2.Rectangle(mask, maxLoc, new Point(maxLoc.X + pic.Cols, maxLoc.Y + pic.Rows), Scalar.Green, 2);
                    Cv2.ImShow("mask", mask);
                    Cv2.WaitKey(0);
                    Cv2.DestroyAllWindows();
                    return true;

                }
                else
                {

                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }

        }


    }
}

 

posted on 2023-05-04 19:24  落叶子  阅读(254)  评论(0编辑  收藏  举报

导航