C# OpenCvSharp MatchTemplate 多目标匹配

目录

效果

项目

代码

下载 


效果

项目

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace OpenCvSharp_MatchTemplate_多目标匹配
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = new Bitmap("test.png");
            String tempImg_path = ("t1.png");
            String srcImg_path = ("test.png");
            Bitmap bitmap = Recoganize(srcImg_path, tempImg_path, 0.95, 1, "target", 10);
            pictureBox2.Image = bitmap;
        }

        Bitmap Recoganize(String srcImg_path, String tempImg_path, double threshold = 0.5, double compressed = 0.5, string name = "target", int space = 10)
        {
            DateTime beginTime = DateTime.Now;            //获取开始时间  
            // 新建图变量并分配内存
            Mat srcImg = new Mat();
            // 读取要被匹配的图像
            srcImg = Cv2.ImRead(srcImg_path);
            // 更改尺寸
            Cv2.Resize(srcImg, srcImg, new OpenCvSharp.Size((int)srcImg.Cols * compressed, (int)srcImg.Rows * compressed));
            // 初始化保存保存匹配结果的横纵坐标列表
            List<int> Xlist = new List<int> { };
            List<int> Ylist = new List<int> { };

            int order = 0;
            Mat tempImg = Cv2.ImRead(tempImg_path);
            Cv2.Resize(tempImg, tempImg, new OpenCvSharp.Size((int)tempImg.Cols * compressed, (int)tempImg.Rows * compressed));
            Mat result = srcImg.Clone();

            int dstImg_rows = srcImg.Rows - tempImg.Rows + 1;
            int dstImg_cols = srcImg.Cols - tempImg.Cols + 1;
            Mat dstImg = new Mat(dstImg_rows, dstImg_cols, MatType.CV_32F, 1);
            Cv2.MatchTemplate(srcImg, tempImg, dstImg, TemplateMatchModes.CCoeffNormed);

            int count = 0;
            for (int i = 0; i < dstImg_rows; i++)
            {
                for (int j = 0; j < dstImg_cols; j++)
                {
                    float matchValue = dstImg.At<float>(i, j);
                    if (matchValue >= threshold && Xlist.Count == 0)
                    {
                        count++;
                        Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
                        Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
                        Xlist.Add(j);
                        Ylist.Add(i);
                    }

                    if (matchValue >= threshold && Xlist.Count != 0)
                    {
                        for (int q = 0; q < Xlist.Count; q++)
                        {
                            if (Math.Abs(j - Xlist[q]) + Math.Abs(i - Ylist[q]) < space)
                            {
                                order = 1;
                                break;
                            }
                        }
                        if (order != 1)
                        {
                            count++;
                            Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
                            Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
                            Xlist.Add(j);
                            Ylist.Add(i);
                        }
                        order = 0;
                    }
                }
            }
            Console.WriteLine("目标数量:{0}", count);
            DateTime endTime = DateTime.Now;              //获取结束时间  
            TimeSpan oTime = endTime.Subtract(beginTime); //求时间差的函数  
            Console.WriteLine("程序的运行时间:{0} 毫秒", oTime.TotalMilliseconds);
            return result.ToBitmap();
        }

    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;namespace OpenCvSharp_MatchTemplate_多目标匹配
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){pictureBox1.Image = new Bitmap("test.png");String tempImg_path = ("t1.png");String srcImg_path = ("test.png");Bitmap bitmap = Recoganize(srcImg_path, tempImg_path, 0.95, 1, "target", 10);pictureBox2.Image = bitmap;}Bitmap Recoganize(String srcImg_path, String tempImg_path, double threshold = 0.5, double compressed = 0.5, string name = "target", int space = 10){DateTime beginTime = DateTime.Now;            //获取开始时间  // 新建图变量并分配内存Mat srcImg = new Mat();// 读取要被匹配的图像srcImg = Cv2.ImRead(srcImg_path);// 更改尺寸Cv2.Resize(srcImg, srcImg, new OpenCvSharp.Size((int)srcImg.Cols * compressed, (int)srcImg.Rows * compressed));// 初始化保存保存匹配结果的横纵坐标列表List<int> Xlist = new List<int> { };List<int> Ylist = new List<int> { };int order = 0;Mat tempImg = Cv2.ImRead(tempImg_path);Cv2.Resize(tempImg, tempImg, new OpenCvSharp.Size((int)tempImg.Cols * compressed, (int)tempImg.Rows * compressed));Mat result = srcImg.Clone();int dstImg_rows = srcImg.Rows - tempImg.Rows + 1;int dstImg_cols = srcImg.Cols - tempImg.Cols + 1;Mat dstImg = new Mat(dstImg_rows, dstImg_cols, MatType.CV_32F, 1);Cv2.MatchTemplate(srcImg, tempImg, dstImg, TemplateMatchModes.CCoeffNormed);int count = 0;for (int i = 0; i < dstImg_rows; i++){for (int j = 0; j < dstImg_cols; j++){float matchValue = dstImg.At<float>(i, j);if (matchValue >= threshold && Xlist.Count == 0){count++;Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);Xlist.Add(j);Ylist.Add(i);}if (matchValue >= threshold && Xlist.Count != 0){for (int q = 0; q < Xlist.Count; q++){if (Math.Abs(j - Xlist[q]) + Math.Abs(i - Ylist[q]) < space){order = 1;break;}}if (order != 1){count++;Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);Xlist.Add(j);Ylist.Add(i);}order = 0;}}}Console.WriteLine("目标数量:{0}", count);DateTime endTime = DateTime.Now;              //获取结束时间  TimeSpan oTime = endTime.Subtract(beginTime); //求时间差的函数  Console.WriteLine("程序的运行时间:{0} 毫秒", oTime.TotalMilliseconds);return result.ToBitmap();}}
}

下载 

Demo下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2905725.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

主流公链 - Monero

Monero: 加密货币的隐私标杆 1. 简介 Monero&#xff08;XMR&#xff09;&#xff0c;世界语中货币的意思&#xff0c;是一种去中心化的加密货币&#xff0c;旨在提供隐私和匿名性。与比特币等公开区块链不同&#xff0c;Monero专注于隐私保护&#xff0c;使用户的交易记录和余…

政安晨:专栏目录【TensorFlow与Keras实战演绎机器学习】

政安晨的个人主页&#xff1a;政安晨 欢迎 &#x1f44d;点赞✍评论⭐收藏 收录专栏: TensorFlow与Keras实战演绎机器学习 希望政安晨的博客能够对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff01; 本篇是作者政安晨的专栏《TensorFlow与Keras…

抖音视频关键词无水印下载软件|手机网页视频批量提取工具

全新视频关键词无水印下载软件&#xff0c;助您快速获取所需视频&#xff01; 随着时代的发展&#xff0c;视频内容已成为人们获取信息和娱乐的重要途径。为了方便用户获取所需视频&#xff0c;推出了一款功能强大的视频关键词无水印下载软件。该软件主要功能包括关键词批量提取…

git基本操作(小白入门快速上手一)

1、前言 我们接上一篇文章来讲&#xff0c;直接开干 1.1、工作区 1. 工作区很好理解&#xff0c;就是我们能看到的工作目录&#xff0c;就是本地的文件夹。 2. 这些本地的文件夹我们要通过 git add 命令先将他们添加到暂存区中。 3. git commit 命令则可以将暂存区中的文件提交…

【01-20】操作系统基础知识(非常详细)从零基础入门到精通,看完这一篇就够了

【01-20】操作系统基础知识&#xff08;非常详细&#xff09;从零基础入门到精通&#xff0c;看完这一篇就够了 以下是本文参考的资料 欢迎大家查收原版 本版本仅作个人笔记使用1、进程、线程和协程的区别和联系2、线程与进程的比较2.1、补充另一种问法 3、一个进程可以创建多少…

由浅到深认识Java语言(42):反射

该文章Github地址&#xff1a;https://github.com/AntonyCheng/java-notes 在此介绍一下作者开源的SpringBoot项目初始化模板&#xff08;Github仓库地址&#xff1a;https://github.com/AntonyCheng/spring-boot-init-template & CSDN文章地址&#xff1a;https://blog.c…

设计模式学习笔记 - 设计模式与范式 -结构型:8.享元模式(下):享元模式在Java Integer、String中的应用

概述 上篇文章《结构型&#xff1a;7.享元模式&#xff08;上&#xff09;&#xff1a;享元模式原理和应用》&#xff0c;通过棋牌游戏和文本编辑器的例子&#xff0c;学习了享元模式的原理、实现以及应用场景。用一句话总结下&#xff0c;享元模式中的 “享元” 指被共享的单…

这次轮到小米,遥遥领先!

年轻人的第一辆保时米 3 月28日晚小米首款汽车小米汽车 SU7 正式发布并上市&#xff0c;新车定位于“C 级高性能生态科技轿车”&#xff0c;提供双电机版本和单电机版本车型选择&#xff0c;并提供容量为 73.6 千瓦时以及 101 千瓦时电池可选&#xff0c;售价 21.59 万元-29.99…

ArcObject for Java 开发环境搭建(IDEA2021)

一、系统环境 1、JDK 需要32位JDK1.8&#xff0c;安装版或离线免安装版均可。 arcgis10.2.2使用1.8.0_151版本&#xff0c;arcgis10.7使用1.8.0_181版本&#xff0c;不过应该都可以&#xff0c;小版本没有严格区分。 2、配置环境变量 配置Arcgis环境变量&#xff08;必须&…

CXL系统架构

CXL系统架构 CXL支持三种设备类型&#xff0c;如下图。Type 1支持CXL.cache和CXL.io&#xff1b;Type 2支持CXL.cache&#xff0c;CXL.mem和CXL.io&#xff1b;Type 3支持CXL.mem和CXL.io。无论哪种类型&#xff0c;CXL.io都是不可缺少的&#xff0c;因为设备的发现&#xff0…

springcloud微服务项目,通过gateway+nacos实现灰度发布(系统不停机升级)

一、背景 灰度发布的目的是保证系统的高可用&#xff0c;不停机&#xff0c;提升用户体验。在微服务系统中&#xff0c;原有系统不下线&#xff0c;新版系统与原有系统同时在线&#xff0c;通过访问权重在线实时配置&#xff0c;可以让少量用户先应用新版本功能&#xff0c;如…

JavaSE day14笔记

第十四天课堂笔记 课上: 适当做笔记课下 : 总结 , 读代码 , 反复敲代码 , 做练习 数组★★★ 数组 : 存储多个 同一类型 的容器格式 :数组类型 : 引用数据类型, new运算符在堆中 分配一块连续的存储空间 , 系统会给数组元素默认初始化 , 将该数组的引用赋值给数组名 引用数据…

Java中读取html文件转成String,展示在浏览器

这里写目录标题 第一章1.1&#xff09;pom中引入依赖和html文件示例1.2&#xff09;使用hutool工具包读取html文件转为string1.3&#xff09;页面显示 第一章 1.1&#xff09;pom中引入依赖和html文件示例 引入hutool工具包依赖 <dependency><groupId>cn.hutool&…

mongodb sharding分片模式的集群数据库,日志治理缺失导致写入数据库报错MongoWriteConcernException的问题总结(上)

一、背景 常见的mongodb集群模式有以下三种&#xff1a; 主从复制&#xff08;Master-Slave&#xff09;模式副本集&#xff08;Replica Set&#xff09;模式分片&#xff08;Sharding&#xff09;模式 公司测试环境搭建的集群采用分片模式&#xff0c;有同事反馈说&#xf…

Qt打印系统库的日志 - QLoggingCategory

Qt的动态库通过源码可以可以看到含有大量的qCInfo 和 qCDebug 等大量的日志&#xff0c; 但是我们正常运行Qt程序&#xff0c;这些动态库或插件里面的日志是不会输出到我们的控制台里面的。 所以本章主要记录怎么输出这些日志出来。 一&#xff1a; 步骤 主要使用的是Qt的 函…

ABA分析方法 — 软硬件开发必备的一种分析问题绝佳套路

ABA分析方法 “决定我们看到什么&#xff0c;并且看到什么方式的是我们的思维方式。” — 乔治奥威尔 更多精彩内容&#xff0c;请关注公众号《机器灵魂注入师》。 什么是ABA分析方法&#xff1f; 可能你从很多地方听过这个名词&#xff0c;无论是在软件开发还是硬件领域(比较…

单片机之串口通信

目录 串口介绍 通信的基本概念 并行通信和串行通信 同步通信和异步通信 串行异步通信方式 串行同步通信方式 通信协议 单片机常见通信接口 串行通信三种模式 串口参数 传输速度 ​串口的连接 电平标准 串行口的组成 串口数据缓冲寄存器 串行口控制寄存器 串口…

HarmonyOS入门--配置环境 + IDE汉化

文章目录 下载安装DevEco Studio配置环境先认识DevEco Studio界面工程目录工程级目录模块级目录 app.json5module.json5main_pages.json通知栏预览区 运行模拟器IED汉化 下载安装DevEco Studio 去官网下载DevEco Studio完了安装 配置环境 打开已安装的DevEco Studio快捷方式…

33.HarmonyOS App(JAVA)鸿蒙系统app数据库增删改查

33.HarmonyOS App(JAVA)鸿蒙系统app数据库增删改查 关系数据库 关系对象数据库&#xff08;ORM&#xff09; 应用偏好数据库 分布式数据库 关系型数据库&#xff08;Relational Database&#xff0c;RDB&#xff09;是一种基于关系模型来管理数据的数据库。HarmonyOS关系型…

阿里云Salesforce CRM功能差异列表 - Winter‘ 24

阉割版的阿里云Salesforce由于技术和监管等因素与国际版的Salesforce差距很大&#xff01; 一、Winter‘ 24版差异概况&#xff1a; 1.1. 主要版本&#xff1a; 阿里云上的 Salesforce 提供两个版本&#xff0c;用于生产用途的 CN 版本&#xff08;CN Edition&#xff09;和用…