C# OpenCvSharp Tracker 目标追踪

news2024/10/1 23:29:31

目录

效果

项目

代码

下载


C# OpenCvSharp Tracker 目标追踪

效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using OpenCvSharp.Tracking;
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace C__OpenCvSharp_Tracker_目标追踪
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string filename = "";
        bool play = false;
        Tracker tracker;

        VideoCapture capture;
        bool m_mouseDown = false;
        bool m_mouseMove = false;

        System.Drawing.Point startPoint = new System.Drawing.Point();
        System.Drawing.Point endPoint = new System.Drawing.Point();

        Mat currentFrame = new Mat();
        Rect roi = new Rect();

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            toolStripStatusLabel1.Text = "请打开视频文件";
            label2.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Video files (*.avi)|*.avi|MP4 files (*.mp4)|*.mp4";
            ofd.RestoreDirectory = true;
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filename = ofd.FileName;
                toolStripStatusLabel1.Text = filename;
                capture = new VideoCapture(filename);
                if (!capture.IsOpened())
                {
                    toolStripStatusLabel1.Text = " 打开视频文件失败";
                    return;
                }
                capture.Read(currentFrame);
                if (!currentFrame.Empty())
                {
                    pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
                    timer1.Interval = (int)(1000.0 / capture.Fps);
                    timer1.Enabled = true;

                    m_mouseMove = false;
                    m_mouseDown = false;
                    pictureBox2.Image = null;
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            play = true;
            if (pictureBox2.Image != null)
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                    default:
                        tracker = TrackerCSRT.Create();
                        break;
                    case 1:
                        tracker = TrackerGOTURN.Create();
                        break;
                    case 2:
                        tracker = TrackerKCF.Create();
                        break;
                    case 3:
                        tracker = TrackerMIL.Create();
                        break;
                }
                tracker.Init(currentFrame, roi);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            play = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (play)
            {
                capture.Read(currentFrame);
                if (currentFrame.Empty())
                {
                    play = false;
                    pictureBox1.Image = null;
                    pictureBox2.Image = null;

                    timer1.Enabled = false;
                    return;
                }
                if (pictureBox2.Image != null && tracker != null)
                {
                    tracker.Update(currentFrame, ref roi);
                    Cv2.Rectangle(currentFrame, roi, Scalar.Red);
                    label2.Text = String.Format("ROI:({0},{1})-({2},{3})", roi.X, roi.Y, roi.X + roi.Width, roi.Y + roi.Height);
                }
                pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue, 2);
            Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));
            g.DrawRectangle(p, rect);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            if (!m_mouseDown) return;

            m_mouseMove = true;
            endPoint = e.Location;

            pictureBox1.Invalidate();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            m_mouseDown = false;
            m_mouseMove = false;

            System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);
            System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);
            if (image_startPoint.X < 0)
                image_startPoint.X = 0;
            if (image_startPoint.Y < 0)
                image_startPoint.Y = 0;
            if (image_endPoint.X < 0)
                image_endPoint.X = 0;
            if (image_endPoint.Y < 0)
                image_endPoint.Y = 0;
            if (image_startPoint.X > currentFrame.Cols)
                image_startPoint.X = currentFrame.Cols;
            if (image_startPoint.Y > currentFrame.Rows)
                image_startPoint.Y = currentFrame.Rows;
            if (image_endPoint.X > currentFrame.Cols)
                image_endPoint.X = currentFrame.Cols;
            if (image_endPoint.Y > currentFrame.Rows)
                image_endPoint.Y = currentFrame.Rows;

            label2.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);
            int w = (image_endPoint.X - image_startPoint.X);
            int h = (image_endPoint.Y - image_startPoint.Y);
            if (w > 10 && h > 10)
            {
                roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);

                Mat roi_mat = currentFrame[roi];
                pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            play = false;
            m_mouseDown = true;

            startPoint = e.Location;
        }

        private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)
        {
            System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
            Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);

            int zoomedWidth = pictureBox.Width;
            int zoomedHeight = pictureBox.Height;

            int imageWidth = pictureBox1.Image.Width;
            int imageHeight = pictureBox1.Image.Height;

            double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);
            double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);
            int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;
            int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;

            int zoomedX = point.X - black_left_width;
            int zoomedY = point.Y - black_top_height;

            System.Drawing.Point outPoint = new System.Drawing.Point();
            outPoint.X = (int)((double)zoomedX / zoomRatex);
            outPoint.Y = (int)((double)zoomedY / zoomRatey);

            return outPoint;
        }

    }
}

using OpenCvSharp;
using OpenCvSharp.Extensions;
using OpenCvSharp.Tracking;
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace C__OpenCvSharp_Tracker_目标追踪
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string filename = "";
        bool play = false;
        Tracker tracker;

        VideoCapture capture;
        bool m_mouseDown = false;
        bool m_mouseMove = false;

        System.Drawing.Point startPoint = new System.Drawing.Point();
        System.Drawing.Point endPoint = new System.Drawing.Point();

        Mat currentFrame = new Mat();
        Rect roi = new Rect();

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            toolStripStatusLabel1.Text = "请打开视频文件";
            label2.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Video files (*.avi)|*.avi|MP4 files (*.mp4)|*.mp4";
            ofd.RestoreDirectory = true;
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filename = ofd.FileName;
                toolStripStatusLabel1.Text = filename;
                capture = new VideoCapture(filename);
                if (!capture.IsOpened())
                {
                    toolStripStatusLabel1.Text = " 打开视频文件失败";
                    return;
                }
                capture.Read(currentFrame);
                if (!currentFrame.Empty())
                {
                    pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
                    timer1.Interval = (int)(1000.0 / capture.Fps);
                    timer1.Enabled = true;

                    m_mouseMove = false;
                    m_mouseDown = false;
                    pictureBox2.Image = null;
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            play = true;
            if (pictureBox2.Image != null)
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                    default:
                        tracker = TrackerCSRT.Create();
                        break;
                    case 1:
                        tracker = TrackerGOTURN.Create();
                        break;
                    case 2:
                        tracker = TrackerKCF.Create();
                        break;
                    case 3:
                        tracker = TrackerMIL.Create();
                        break;
                }
                tracker.Init(currentFrame, roi);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            play = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (play)
            {
                capture.Read(currentFrame);
                if (currentFrame.Empty())
                {
                    play = false;
                    pictureBox1.Image = null;
                    pictureBox2.Image = null;

                    timer1.Enabled = false;
                    return;
                }
                if (pictureBox2.Image != null && tracker != null)
                {
                    tracker.Update(currentFrame, ref roi);
                    Cv2.Rectangle(currentFrame, roi, Scalar.Red);
                    label2.Text = String.Format("ROI:({0},{1})-({2},{3})", roi.X, roi.Y, roi.X + roi.Width, roi.Y + roi.Height);
                }
                pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue, 2);
            Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));
            g.DrawRectangle(p, rect);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            if (!m_mouseDown) return;

            m_mouseMove = true;
            endPoint = e.Location;

            pictureBox1.Invalidate();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            m_mouseDown = false;
            m_mouseMove = false;

            System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);
            System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);
            if (image_startPoint.X < 0)
                image_startPoint.X = 0;
            if (image_startPoint.Y < 0)
                image_startPoint.Y = 0;
            if (image_endPoint.X < 0)
                image_endPoint.X = 0;
            if (image_endPoint.Y < 0)
                image_endPoint.Y = 0;
            if (image_startPoint.X > currentFrame.Cols)
                image_startPoint.X = currentFrame.Cols;
            if (image_startPoint.Y > currentFrame.Rows)
                image_startPoint.Y = currentFrame.Rows;
            if (image_endPoint.X > currentFrame.Cols)
                image_endPoint.X = currentFrame.Cols;
            if (image_endPoint.Y > currentFrame.Rows)
                image_endPoint.Y = currentFrame.Rows;

            label2.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);
            int w = (image_endPoint.X - image_startPoint.X);
            int h = (image_endPoint.Y - image_startPoint.Y);
            if (w > 10 && h > 10)
            {
                roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);

                Mat roi_mat = currentFrame[roi];
                pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            play = false;
            m_mouseDown = true;

            startPoint = e.Location;
        }

        private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)
        {
            System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
            Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);

            int zoomedWidth = pictureBox.Width;
            int zoomedHeight = pictureBox.Height;

            int imageWidth = pictureBox1.Image.Width;
            int imageHeight = pictureBox1.Image.Height;

            double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);
            double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);
            int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;
            int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;

            int zoomedX = point.X - black_left_width;
            int zoomedY = point.Y - black_top_height;

            System.Drawing.Point outPoint = new System.Drawing.Point();
            outPoint.X = (int)((double)zoomedX / zoomRatex);
            outPoint.Y = (int)((double)zoomedY / zoomRatey);

            return outPoint;
        }

    }
}

下载

源码下载

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

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

相关文章

iconfont的组件化使用方法(SVG)

目录 一、需求描述二、操作步骤1.在iconfont中选择项目需要使用的图标2.在项目中创建iconfont.js3.创建svgIcon组件 一、需求描述 将iconfont图标库选择的图标以SVG的形式引入项目并通过组件化的形式在项目中引用可控制图标的大小和颜色 二、操作步骤 1.在iconfont中选择项目…

【论文笔记之 YIN】YIN, a fundamental frequency estimator for speech and music

本文对 Alain de Cheveigne 等人于 2002 年在 The Journal of the Acoustical Society of America 上发表的论文进行简单地翻译。如有表述不当之处欢迎批评指正。欢迎任何形式的转载&#xff0c;但请务必注明出处。 论文链接&#xff1a;http://audition.ens.fr/adc/pdf/2002_…

单片机精进之路-5矩阵键盘扫描

如下图&#xff0c;先在p3口输出0xfe&#xff0c;再读取p3口的电平&#xff0c;如果没有按键按下&#xff0c;temp & 0xf0还是0xf0&#xff0c;如果又第一个键按下&#xff0c;temp & 0xf0还是0xee&#xff0c;其他按键由此类推可得。 //4*4键盘检测程序,按下键后相应…

【笔记】【电子科大 离散数学】 2.命题

文章目录 数理逻辑定义 命题定义不是命题的例子 原子命题和复合命题定义约定 命题联结词否定联结词定义例子真值表 合取联结词定义例子真值表 析取联结词定义例子 蕴含联结词定义例子真值表 等价联结词定义例子真值表 命题符号化及其应用速查表格优先级复合命题符号化布尔检索演…

golang学习1,dea的golang-1.22.0

参考&#xff1a;使用IDEA配置GO的开发环境备忘录-CSDN博客 1.下载All releases - The Go Programming Language (google.cn) 2.直接next 3.window环境变量配置 4.idea的go插件安装 5.新建go项目找不到jdk解决 https://blog.csdn.net/ouyang111222/article/details/1361657…

docker存储驱动

目录 一、写时复制和用时分配 二、联合文件系统 2.1、aufs ​编辑 2.2、分层的问题 2.3、overlay 2.4 文件系统区别 三、容器跑httpd案例 3.1、案例1&#xff1a;端口映射 3.2、案例2&#xff1a;制作httpd应用镜像 3.3、案例3&#xff1a;docker数据卷挂载 3.4、案…

sqllabs的order by注入

当我们在打开sqli-labs的46关发现其实是个表格&#xff0c;当测试sort等于123时&#xff0c;会根据列数的不同来进行排序 我们需要利用这个点来判断是否存在注入漏洞&#xff0c;通过加入asc 和desc判断页面有注入点 1、基于使用if语句盲注 如果我们配合if函数&#xff0c;表达…

《Docker 简易速速上手小册》第4章 Docker 容器管理(2024 最新版)

文章目录 4.1 容器生命周期管理4.1.1 重点基础知识4.1.2 重点案例&#xff1a;启动并管理 Python Flask 应用容器4.1.3 拓展案例 1&#xff1a;调试运行中的容器4.1.4 拓展案例 2&#xff1a;优雅地停止和清理容器 4.2 容器数据管理与持久化4.2.1 重点基础知识4.2.2 重点案例&a…

IT行业风向让人琢磨不透,一端狂裁,一端狂吸收……

2024年伊始&#xff0c;全球科技行业显然又掀起了新一波的裁员潮。尽管这轮裁员潮来得不如去年年初那样猛烈&#xff0c;但依然不免令不少人感到措手不及…… 甚至有人开工第一天收到的并不是开门红包&#xff0c;而是裁员说明书&#xff1a; 以前一直以为年前被裁&#xff0c;…

甲基二十四聚乙二醇叠氮,mPEG24 N3,可以和含有 Alkyne 基团的分子反应

您好&#xff0c;欢迎来到新研之家 文章关键词&#xff1a;2563873-82-3&#xff0c;m-PEG24-azide&#xff0c;mPEG24 N3&#xff0c;甲基二十四聚乙二醇叠氮&#xff0c;甲基 PEG24 叠氮 一、基本信息 【产品简介】&#xff1a;M-PEG24-azide is a click chemical reagent…

idea集成git(实用篇)

0.Git常用命令 Git常用命令-CSDN博客 1.下载git Git - Downloads 一路傻瓜式安装即可&#xff08;NEXT&#xff09; 2.软件测试 在Windows桌面空白处&#xff0c;点击鼠标右键&#xff0c;弹出右键菜单 Git软件安装后&#xff0c;会在右键菜单中增加两个菜单 Git GUI He…

2024022502-数据库绪论

数据库绪论 数据管理的三个阶段 人工管理阶段 文件系统阶段 数据库系统阶段 基本术语 数据&#xff08;Data&#xff09; 计算机用来描述事物的记录&#xff08;文字&#xff0e;图形&#xff0e;图像&#xff0e;声音&#xff09;数据的形式本身并不能完全表达其内容&am…

LeetCode第二题: 两数相加

文章目录 题目描述示例 解题思路 - 迭代法Go语言实现 - 迭代法算法分析 解题思路 - 模拟法Go语言实现 - 模拟法算法分析 解题思路 - 优化模拟法主要方法其他方法的考虑 ‍ 题目描述 给出两个非空的链表用来表示两个非负的整数。其中&#xff0c;它们各自的位数是按照逆序的方…

第三百六十八回

文章目录 1. 概念介绍2. 方法与细节2.1 获取方法2.2 使用细节 3. 示例代码4. 内容总结 我们在上一章回中介绍了"如何获取当前系统语言"相关的内容&#xff0c;本章回中将介绍如何获取时间戳.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1. 概念介绍 我们在本章…

alembic

alembic是sqlalchemy的作者开发的。 用来做OMR模型与数据库的迁移与映射。 第一个&#xff0c;alembic的所有命令都是以alembic开头 第二&#xff0c;alembic的迁移文件也是通过版本进行控制的。首先&#xff0c;通过pip install alembic进行安装。以下将解释alembic的用法 方…

Linux内核网络

文章目录 前言网络协议栈图解功能 发送Linux内核网络数据包图解流程 接收Linux内核网络数据包图解流程 最后 前言 你好&#xff0c;我是醉墨居士&#xff0c;因为Linux内核涉及的内容极多&#xff0c;我们初学者如果一上来就开始深挖细节&#xff0c;很有可能会在Linux内核代码…

如何使用Axure RP制作web页面并实现无公网ip远程访问——“cpolar内网穿透”

文章目录 前言1.在AxureRP中生成HTML文件2.配置IIS服务3.添加防火墙安全策略4.使用cpolar内网穿透实现公网访问4.1 登录cpolar web ui管理界面4.2 启动website隧道4.3 获取公网URL地址4.4. 公网远程访问内网web站点4.5 配置固定二级子域名公网访问内网web站点4.5.1创建一条固定…

NFTScan | 02.19~02.25 NFT 市场热点汇总

欢迎来到由 NFT 基础设施 NFTScan 出品的 NFT 生态热点事件每周汇总。 周期&#xff1a;2024.02.19~ 2024.02.25 NFT Hot News 01/ LINE NEXT 与 ReadON 建立战略合作伙伴关系&#xff0c;将为 DOSI 用户提供创新数字产品 2 月 19 日&#xff0c;日本社交巨头 LINE 旗下 NFT …

lv20 QT入门与基础控件 1

1 QT简介 QT是挪威Trolltech开发的多平台C图形用户界面应用程序框架 典型应用 2 工程搭建 2.1 新建ui工程 不要写中文路径 2.1 不勾选UI&#xff08;主讲&#xff09; 3 QT信号与槽机制 语法&#xff1a;Connect&#xff08;A, SIGNLA(aaa()), B, SLOT(bbb())&#xff09;…

react+canvas实现刮刮乐效果

话不多说&#xff0c;直接看代码吧 import { useEffect } from react; import styles from ./index.less;export default function Canvas() {function init() {let gj document.querySelector(.gj);let jp document.querySelector(#jp) as HTMLElement;let canvas documen…