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

1. 添加包引用

1
2
3
4
5
<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   落叶子  阅读(280)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2018-05-04 EF Core注意事项
2018-05-04 Entity Framework Core 选择数据表的外键

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示