PixelTrackingDialog.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using OpenCvSharp;
  2. using PaintDotNet.Adjust.BaseImage;
  3. using PaintDotNet.ImageCollect;
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. namespace PaintDotNet.Instrument
  8. {
  9. /// <summary>
  10. /// 像素跟踪
  11. /// </summary>
  12. internal class PixelTrackingDialog : FloatingToolForm
  13. {
  14. #region 控件
  15. private Label label6;
  16. private Label label5;
  17. private Label label4;
  18. private Label label3;
  19. private Label label2;
  20. private Label label1;
  21. private CustomControl.LinearColorPickerControl panel1;
  22. private PictureBox pictureBox1;
  23. private Label label7;
  24. private Label label8;
  25. private NumericUpDown numericUpDown1;
  26. private TrackBar trackBar1;
  27. private Label label9;
  28. #endregion
  29. private AppWorkspace appWorkspace;
  30. /// <summary>
  31. /// 原图、hls(可优化)、鹰眼图(从经过缩放的图片获取)、经过缩放的图片
  32. /// </summary>
  33. private Mat mat, ImageROI, bmat = new Mat();
  34. /// <summary>
  35. /// rgb像素点
  36. /// </summary>
  37. private Vec3b bgr;
  38. /// <summary>
  39. /// hls像素点
  40. /// </summary>
  41. private double h, s, v;
  42. /// <summary>
  43. /// 缩放倍数
  44. /// </summary>
  45. private int scale;
  46. /// <summary>
  47. /// 中间十字和圆的颜色
  48. /// 根据左侧的panel的被选中的背景色变化
  49. /// </summary>
  50. private Color color = Color.Red;
  51. public Mat Mat
  52. {
  53. set
  54. {
  55. this.mat = value;
  56. }
  57. }
  58. private void trackBar1_ValueChanged(object sender, EventArgs e)
  59. {
  60. if (mat != null)
  61. {
  62. scale = this.trackBar1.Value;
  63. this.numericUpDown1.Value = this.trackBar1.Value;
  64. //Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width * scale, mat.Height * scale));
  65. //Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());
  66. //Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));
  67. //bmat = temp;
  68. }
  69. }
  70. private void numericUpDown1_ValueChanged(object sender, EventArgs e)
  71. {
  72. if (mat != null)
  73. {
  74. scale = (int)this.numericUpDown1.Value;
  75. this.trackBar1.Value = scale;
  76. //Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width * scale, mat.Height * scale));
  77. //Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());
  78. //Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));
  79. //bmat = temp;
  80. }
  81. }
  82. /// <summary>
  83. /// 构造函数
  84. /// </summary>
  85. /// <param name="appWorkspace"></param>
  86. public PixelTrackingDialog(AppWorkspace appWorkspace)
  87. {
  88. this.appWorkspace = appWorkspace;
  89. InitializeComponent();
  90. this.label9.Text = PdnResources.GetString("Menu.Times.text");
  91. this.Text = PdnResources.GetString("Menu.Tools.PixelTracking.Text");
  92. this.label7.Parent = this.pictureBox1;
  93. this.label8.Parent = this.pictureBox1;
  94. this.label7.Location = new System.Drawing.Point(0, 0);
  95. this.label8.Location = new System.Drawing.Point(0, 17);
  96. this.pictureBox1.Paint += new PaintEventHandler(PictureBox1_Paint);
  97. }
  98. private void PixelTrackingDialog_FormClosing(object sender, FormClosingEventArgs e)
  99. {
  100. this.appWorkspace.toolBar.RefreshBtnSelect(false, "PixelTracking");
  101. this.appWorkspace.toolsPanel.RefreshBtnSelect(false, "PixelTracking");
  102. }
  103. /// <summary>
  104. /// PictureBox的绘制事件
  105. /// </summary>
  106. /// <param name="sender"></param>
  107. /// <param name="e"></param>
  108. private void PictureBox1_Paint(object sender, PaintEventArgs e)
  109. {
  110. e.Graphics.DrawLine(new Pen(color), 0, 50, 100, 50);
  111. e.Graphics.DrawLine(new Pen(color), 50, 0, 50, 100);
  112. e.Graphics.DrawEllipse(new Pen(color), new Rectangle(25, 25, 50, 50));
  113. }
  114. ///// <summary>
  115. ///// 左侧panel,渐变背景色
  116. ///// </summary>
  117. ///// <param name="sender"></param>
  118. ///// <param name="e"></param>
  119. //private void Panel1_Paint(object sender, PaintEventArgs e)
  120. //{
  121. // LinearGradientBrush brush = new LinearGradientBrush(e.ClipRectangle, Color.Yellow, Color.Blue, LinearGradientMode.Vertical);
  122. // ColorBlend colorBlend = new ColorBlend();
  123. // colorBlend.Colors = new Color[]{ Color.Yellow, Color.Red, Color.Green, Color.Blue };
  124. // colorBlend.Positions = new float[] { 0 / 3f, 1 / 3f, 2 / 3f, 3 / 3f };
  125. // brush.InterpolationColors = colorBlend;
  126. // e.Graphics.FillRectangle(brush, e.ClipRectangle);
  127. //}
  128. private void Panel1_ValueChanged(object sender, IndexEventArgs ce)
  129. {
  130. color = panel1.ValueToColor(panel1.Value);
  131. pictureBox1.Refresh();
  132. }
  133. private void InitializeComponent()
  134. {
  135. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  136. this.label6 = new System.Windows.Forms.Label();
  137. this.label5 = new System.Windows.Forms.Label();
  138. this.label4 = new System.Windows.Forms.Label();
  139. this.label3 = new System.Windows.Forms.Label();
  140. this.label2 = new System.Windows.Forms.Label();
  141. this.label1 = new System.Windows.Forms.Label();
  142. this.panel1 = new PaintDotNet.CustomControl.LinearColorPickerControl();
  143. this.label7 = new System.Windows.Forms.Label();
  144. this.label8 = new System.Windows.Forms.Label();
  145. this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
  146. this.trackBar1 = new System.Windows.Forms.TrackBar();
  147. this.label9 = new System.Windows.Forms.Label();
  148. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  149. ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
  150. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
  151. this.SuspendLayout();
  152. //
  153. // pictureBox1
  154. //
  155. this.pictureBox1.Location = new System.Drawing.Point(21, 8);
  156. this.pictureBox1.Name = "pictureBox1";
  157. this.pictureBox1.Size = new System.Drawing.Size(100, 100);
  158. this.pictureBox1.TabIndex = 1;
  159. this.pictureBox1.TabStop = false;
  160. //
  161. // label6
  162. //
  163. this.label6.AutoSize = true;
  164. this.label6.Location = new System.Drawing.Point(124, 95);
  165. this.label6.Name = "label6";
  166. this.label6.Size = new System.Drawing.Size(11, 12);
  167. this.label6.TabIndex = 14;
  168. this.label6.Text = "V";
  169. //
  170. // label5
  171. //
  172. this.label5.AutoSize = true;
  173. this.label5.Location = new System.Drawing.Point(124, 79);
  174. this.label5.Name = "label5";
  175. this.label5.Size = new System.Drawing.Size(11, 12);
  176. this.label5.TabIndex = 13;
  177. this.label5.Text = "S";
  178. //
  179. // label4
  180. //
  181. this.label4.AutoSize = true;
  182. this.label4.Location = new System.Drawing.Point(124, 63);
  183. this.label4.Name = "label4";
  184. this.label4.Size = new System.Drawing.Size(11, 12);
  185. this.label4.TabIndex = 12;
  186. this.label4.Text = "H";
  187. //
  188. // label3
  189. //
  190. this.label3.AutoSize = true;
  191. this.label3.Location = new System.Drawing.Point(124, 41);
  192. this.label3.Name = "label3";
  193. this.label3.Size = new System.Drawing.Size(11, 12);
  194. this.label3.TabIndex = 11;
  195. this.label3.Text = "B";
  196. //
  197. // label2
  198. //
  199. this.label2.AutoSize = true;
  200. this.label2.Location = new System.Drawing.Point(124, 25);
  201. this.label2.Name = "label2";
  202. this.label2.Size = new System.Drawing.Size(11, 12);
  203. this.label2.TabIndex = 10;
  204. this.label2.Text = "G";
  205. //
  206. // label1
  207. //
  208. this.label1.AutoSize = true;
  209. this.label1.Location = new System.Drawing.Point(124, 9);
  210. this.label1.Name = "label1";
  211. this.label1.Size = new System.Drawing.Size(35, 12);
  212. this.label1.TabIndex = 9;
  213. this.label1.Text = "R=255";
  214. //
  215. // panel1
  216. //
  217. this.panel1.Count = 1;
  218. this.panel1.CustomGradient = new System.Drawing.Color[] {
  219. System.Drawing.Color.White,
  220. System.Drawing.Color.Yellow,
  221. System.Drawing.Color.Red,
  222. System.Drawing.Color.Green,
  223. System.Drawing.Color.Blue,
  224. System.Drawing.Color.Black};
  225. this.panel1.DrawFarNub = true;
  226. this.panel1.DrawNearNub = true;
  227. this.panel1.Location = new System.Drawing.Point(3, 8);
  228. this.panel1.MaxColor = System.Drawing.Color.White;
  229. this.panel1.MinColor = System.Drawing.Color.Black;
  230. this.panel1.Name = "panel1";
  231. this.panel1.Orientation = System.Windows.Forms.Orientation.Vertical;
  232. this.panel1.Size = new System.Drawing.Size(14, 100);
  233. this.panel1.TabIndex = 17;
  234. this.panel1.TabStop = false;
  235. this.panel1.Value = 153;
  236. this.panel1.ValueChanged += new PaintDotNet.IndexEventHandler(this.Panel1_ValueChanged);
  237. //
  238. // label7
  239. //
  240. this.label7.AutoSize = true;
  241. this.label7.BackColor = System.Drawing.Color.Transparent;
  242. this.label7.Location = new System.Drawing.Point(24, 11);
  243. this.label7.Name = "label7";
  244. this.label7.Size = new System.Drawing.Size(17, 12);
  245. this.label7.TabIndex = 18;
  246. this.label7.Text = "X=";
  247. //
  248. // label8
  249. //
  250. this.label8.AutoSize = true;
  251. this.label8.BackColor = System.Drawing.Color.Transparent;
  252. this.label8.Location = new System.Drawing.Point(24, 27);
  253. this.label8.Name = "label8";
  254. this.label8.Size = new System.Drawing.Size(17, 12);
  255. this.label8.TabIndex = 19;
  256. this.label8.Text = "Y=";
  257. //
  258. // numericUpDown1
  259. //
  260. this.numericUpDown1.Location = new System.Drawing.Point(4, 114);
  261. this.numericUpDown1.Maximum = new decimal(new int[] {
  262. 10,
  263. 0,
  264. 0,
  265. 0});
  266. this.numericUpDown1.Minimum = new decimal(new int[] {
  267. 1,
  268. 0,
  269. 0,
  270. 0});
  271. this.numericUpDown1.Name = "numericUpDown1";
  272. this.numericUpDown1.Size = new System.Drawing.Size(37, 21);
  273. this.numericUpDown1.TabIndex = 20;
  274. this.numericUpDown1.Value = new decimal(new int[] {
  275. 1,
  276. 0,
  277. 0,
  278. 0});
  279. this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
  280. //
  281. // trackBar1
  282. //
  283. this.trackBar1.Location = new System.Drawing.Point(68, 114);
  284. this.trackBar1.Minimum = 1;
  285. this.trackBar1.Name = "trackBar1";
  286. this.trackBar1.Size = new System.Drawing.Size(91, 45);
  287. this.trackBar1.TabIndex = 21;
  288. this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None;
  289. this.trackBar1.Value = 1;
  290. this.trackBar1.ValueChanged += new System.EventHandler(this.trackBar1_ValueChanged);
  291. //
  292. // label9
  293. //
  294. this.label9.AutoSize = true;
  295. this.label9.Location = new System.Drawing.Point(45, 118);
  296. this.label9.Name = "label9";
  297. this.label9.Size = new System.Drawing.Size(0, 12);
  298. this.label9.TabIndex = 22;
  299. //
  300. // PixelTrackingDialog
  301. //
  302. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  303. this.ClientSize = new System.Drawing.Size(164, 141);
  304. this.Controls.Add(this.label9);
  305. this.Controls.Add(this.trackBar1);
  306. this.Controls.Add(this.numericUpDown1);
  307. this.Controls.Add(this.label8);
  308. this.Controls.Add(this.label7);
  309. this.Controls.Add(this.panel1);
  310. this.Controls.Add(this.label6);
  311. this.Controls.Add(this.label5);
  312. this.Controls.Add(this.label4);
  313. this.Controls.Add(this.label3);
  314. this.Controls.Add(this.label2);
  315. this.Controls.Add(this.label1);
  316. this.Controls.Add(this.pictureBox1);
  317. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  318. this.Name = "PixelTrackingDialog";
  319. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.PixelTrackingDialog_FormClosing);
  320. this.Controls.SetChildIndex(this.pictureBox1, 0);
  321. this.Controls.SetChildIndex(this.label1, 0);
  322. this.Controls.SetChildIndex(this.label2, 0);
  323. this.Controls.SetChildIndex(this.label3, 0);
  324. this.Controls.SetChildIndex(this.label4, 0);
  325. this.Controls.SetChildIndex(this.label5, 0);
  326. this.Controls.SetChildIndex(this.label6, 0);
  327. this.Controls.SetChildIndex(this.panel1, 0);
  328. this.Controls.SetChildIndex(this.label7, 0);
  329. this.Controls.SetChildIndex(this.label8, 0);
  330. this.Controls.SetChildIndex(this.numericUpDown1, 0);
  331. this.Controls.SetChildIndex(this.trackBar1, 0);
  332. this.Controls.SetChildIndex(this.label9, 0);
  333. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  334. ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
  335. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
  336. this.ResumeLayout(false);
  337. this.PerformLayout();
  338. }
  339. public bool IsPreview()
  340. {
  341. bool iss = CameraPreviewDialog.cameraPreviewDialog != null && CameraPreviewDialog.cameraPreviewDialog.IsLeave() && CameraPreviewDialog.cameraPreviewDialog.documentWorkspace.CompositionSurface != null ;
  342. return iss;
  343. }
  344. /// <summary>
  345. /// 设置像素跟踪的数据
  346. /// </summary>
  347. public void SetImageAndData(System.Drawing.Point point)
  348. {
  349. if (IsPreview() || this.appWorkspace.ActiveDocumentWorkspace != null)
  350. {
  351. scale = int.Parse(this.numericUpDown1.Value.ToString());
  352. if (IsPreview())
  353. mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(CameraPreviewDialog.cameraPreviewDialog.documentWorkspace.CompositionSurface.CreateAliasedBitmap());
  354. else
  355. mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreateAliasedBitmap());
  356. Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width, mat.Height));
  357. Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());
  358. Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));
  359. bmat = temp;
  360. if (point.Y < 0) point.Y = 0;
  361. if (point.X < 0) point.X = 0;
  362. this.label7.Text = "X=" + point.X.ToString();
  363. this.label8.Text = "Y=" + point.Y.ToString();
  364. if (point.X < mat.Width && point.Y < mat.Height)
  365. {
  366. bgr = mat.At<Vec3b>(point.Y, point.X);
  367. this.label1.Text = "R=" + bgr[2];
  368. this.label2.Text = "G=" + bgr[1];
  369. this.label3.Text = "B=" + bgr[0];
  370. BaseTools.RgbToHsv(bgr, out h, out s, out v);
  371. this.label4.Text = "H=" + (int)(h / 2);
  372. this.label5.Text = "S=" + (int)(s * 255);
  373. this.label6.Text = "V=" + (int)(v * 255);
  374. //设置源图像ROI
  375. Rect roi = new Rect(point.X, point.Y, 100, 100);//首先要用个rect确定我们的兴趣区域在哪
  376. ImageROI = new Mat(bmat, roi);
  377. //按缩放比例截取小图像
  378. Rect roi1 = new Rect((100 - 100 / scale) / 2, (100 - 100 / scale) / 2, 100 / scale, 100 / scale);
  379. ImageROI = new Mat(ImageROI, roi1);
  380. //按固定宽高放大图像
  381. Cv2.Resize(ImageROI, ImageROI, new OpenCvSharp.Size(100, 100));
  382. this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(ImageROI);
  383. }
  384. }
  385. GC.Collect();
  386. }
  387. }
  388. }