当前位置:   article > 正文

OpenCvSharp Tracker 目标追踪

OpenCvSharp Tracker 目标追踪

目录

效果

项目

代码

下载


C# OpenCvSharp Tracker 目标追踪

效果

项目

代码

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

下载

源码下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/148331
推荐阅读
相关标签
  

闽ICP备14008679号