首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OpenCvSharp Tracker 目标追踪

  • 25-02-19 03:01
  • 2204
  • 13293
blog.csdn.net

目录

效果

项目

代码

下载


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;
        }

    }
}

  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using OpenCvSharp.Tracking;
  4. using System;
  5. using System.Drawing;
  6. using System.Reflection;
  7. using System.Windows.Forms;
  8. namespace C__OpenCvSharp_Tracker_目标追踪
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16. string filename = "";
  17. bool play = false;
  18. Tracker tracker;
  19. VideoCapture capture;
  20. bool m_mouseDown = false;
  21. bool m_mouseMove = false;
  22. System.Drawing.Point startPoint = new System.Drawing.Point();
  23. System.Drawing.Point endPoint = new System.Drawing.Point();
  24. Mat currentFrame = new Mat();
  25. Rect roi = new Rect();
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. comboBox1.SelectedIndex = 0;
  29. toolStripStatusLabel1.Text = "请打开视频文件";
  30. label2.Text = "";
  31. }
  32. private void button1_Click(object sender, EventArgs e)
  33. {
  34. OpenFileDialog ofd = new OpenFileDialog();
  35. ofd.Filter = "Video files (*.avi)|*.avi|MP4 files (*.mp4)|*.mp4";
  36. ofd.RestoreDirectory = true;
  37. ofd.CheckFileExists = true;
  38. if (ofd.ShowDialog() == DialogResult.OK)
  39. {
  40. filename = ofd.FileName;
  41. toolStripStatusLabel1.Text = filename;
  42. capture = new VideoCapture(filename);
  43. if (!capture.IsOpened())
  44. {
  45. toolStripStatusLabel1.Text = " 打开视频文件失败";
  46. return;
  47. }
  48. capture.Read(currentFrame);
  49. if (!currentFrame.Empty())
  50. {
  51. pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
  52. timer1.Interval = (int)(1000.0 / capture.Fps);
  53. timer1.Enabled = true;
  54. m_mouseMove = false;
  55. m_mouseDown = false;
  56. pictureBox2.Image = null;
  57. }
  58. }
  59. }
  60. private void button2_Click(object sender, EventArgs e)
  61. {
  62. play = true;
  63. if (pictureBox2.Image != null)
  64. {
  65. switch (comboBox1.SelectedIndex)
  66. {
  67. case 0:
  68. default:
  69. tracker = TrackerCSRT.Create();
  70. break;
  71. case 1:
  72. tracker = TrackerGOTURN.Create();
  73. break;
  74. case 2:
  75. tracker = TrackerKCF.Create();
  76. break;
  77. case 3:
  78. tracker = TrackerMIL.Create();
  79. break;
  80. }
  81. tracker.Init(currentFrame, roi);
  82. }
  83. }
  84. private void button3_Click(object sender, EventArgs e)
  85. {
  86. play = false;
  87. }
  88. private void timer1_Tick(object sender, EventArgs e)
  89. {
  90. if (play)
  91. {
  92. capture.Read(currentFrame);
  93. if (currentFrame.Empty())
  94. {
  95. play = false;
  96. pictureBox1.Image = null;
  97. pictureBox2.Image = null;
  98. timer1.Enabled = false;
  99. return;
  100. }
  101. if (pictureBox2.Image != null && tracker != null)
  102. {
  103. tracker.Update(currentFrame, ref roi);
  104. Cv2.Rectangle(currentFrame, roi, Scalar.Red);
  105. label2.Text = String.Format("ROI:({0},{1})-({2},{3})", roi.X, roi.Y, roi.X + roi.Width, roi.Y + roi.Height);
  106. }
  107. pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
  108. }
  109. }
  110. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  111. {
  112. if (!m_mouseDown || !m_mouseMove)
  113. return;
  114. Graphics g = e.Graphics;
  115. Pen p = new Pen(Color.Blue, 2);
  116. Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));
  117. g.DrawRectangle(p, rect);
  118. }
  119. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  120. {
  121. if (pictureBox1.Image == null)
  122. return;
  123. if (!m_mouseDown) return;
  124. m_mouseMove = true;
  125. endPoint = e.Location;
  126. pictureBox1.Invalidate();
  127. }
  128. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  129. {
  130. if (!m_mouseDown || !m_mouseMove)
  131. return;
  132. m_mouseDown = false;
  133. m_mouseMove = false;
  134. System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);
  135. System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);
  136. if (image_startPoint.X < 0)
  137. image_startPoint.X = 0;
  138. if (image_startPoint.Y < 0)
  139. image_startPoint.Y = 0;
  140. if (image_endPoint.X < 0)
  141. image_endPoint.X = 0;
  142. if (image_endPoint.Y < 0)
  143. image_endPoint.Y = 0;
  144. if (image_startPoint.X > currentFrame.Cols)
  145. image_startPoint.X = currentFrame.Cols;
  146. if (image_startPoint.Y > currentFrame.Rows)
  147. image_startPoint.Y = currentFrame.Rows;
  148. if (image_endPoint.X > currentFrame.Cols)
  149. image_endPoint.X = currentFrame.Cols;
  150. if (image_endPoint.Y > currentFrame.Rows)
  151. image_endPoint.Y = currentFrame.Rows;
  152. label2.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);
  153. int w = (image_endPoint.X - image_startPoint.X);
  154. int h = (image_endPoint.Y - image_startPoint.Y);
  155. if (w > 10 && h > 10)
  156. {
  157. roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);
  158. Mat roi_mat = currentFrame[roi];
  159. pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);
  160. }
  161. }
  162. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  163. {
  164. if (pictureBox1.Image == null)
  165. return;
  166. play = false;
  167. m_mouseDown = true;
  168. startPoint = e.Location;
  169. }
  170. private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)
  171. {
  172. System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
  173. Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);
  174. int zoomedWidth = pictureBox.Width;
  175. int zoomedHeight = pictureBox.Height;
  176. int imageWidth = pictureBox1.Image.Width;
  177. int imageHeight = pictureBox1.Image.Height;
  178. double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);
  179. double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);
  180. int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;
  181. int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;
  182. int zoomedX = point.X - black_left_width;
  183. int zoomedY = point.Y - black_top_height;
  184. System.Drawing.Point outPoint = new System.Drawing.Point();
  185. outPoint.X = (int)((double)zoomedX / zoomRatex);
  186. outPoint.Y = (int)((double)zoomedY / zoomRatey);
  187. return outPoint;
  188. }
  189. }
  190. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/136255145"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top