PixelTrackingDialog.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 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. private Mat mat;
  52. public Mat Mat
  53. {
  54. set
  55. {
  56. this.mat = value;
  57. }
  58. }
  59. private void trackBar1_ValueChanged(object sender, EventArgs e)
  60. {
  61. if (mat != null)
  62. {
  63. scale = this.trackBar1.Value;
  64. this.numericUpDown1.Value = this.trackBar1.Value;
  65. //Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width * scale, mat.Height * scale));
  66. //Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());
  67. //Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));
  68. //bmat = temp;
  69. }
  70. }
  71. private void numericUpDown1_ValueChanged(object sender, EventArgs e)
  72. {
  73. if (mat != null)
  74. {
  75. scale = (int)this.numericUpDown1.Value;
  76. this.trackBar1.Value = scale;
  77. //Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width * scale, mat.Height * scale));
  78. //Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());
  79. //Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));
  80. //bmat = temp;
  81. }
  82. }
  83. /// <summary>
  84. /// 构造函数
  85. /// </summary>
  86. /// <param name="appWorkspace"></param>
  87. public PixelTrackingDialog(AppWorkspace appWorkspace)
  88. {
  89. this.appWorkspace = appWorkspace;
  90. InitializeComponent();
  91. this.label9.Text = PdnResources.GetString("Menu.Times.text");
  92. this.Text = PdnResources.GetString("Menu.Tools.PixelTracking.Text");
  93. this.label7.Parent = this.pictureBox1;
  94. this.label8.Parent = this.pictureBox1;
  95. this.label7.Location = new System.Drawing.Point(0, 0);
  96. this.label8.Location = new System.Drawing.Point(0, 17);
  97. this.pictureBox1.Paint += new PaintEventHandler(PictureBox1_Paint);
  98. }
  99. private void PixelTrackingDialog_FormClosing(object sender, FormClosingEventArgs e)
  100. {
  101. this.appWorkspace.toolBar.RefreshBtnSelect(false, "PixelTracking");
  102. this.appWorkspace.toolsPanel.RefreshBtnSelect(false, "PixelTracking");
  103. }
  104. /// <summary>
  105. /// PictureBox的绘制事件
  106. /// </summary>
  107. /// <param name="sender"></param>
  108. /// <param name="e"></param>
  109. private void PictureBox1_Paint(object sender, PaintEventArgs e)
  110. {
  111. e.Graphics.DrawLine(new Pen(color), 0, 50, 100, 50);
  112. e.Graphics.DrawLine(new Pen(color), 50, 0, 50, 100);
  113. e.Graphics.DrawEllipse(new Pen(color), new Rectangle(25, 25, 50, 50));
  114. }
  115. ///// <summary>
  116. ///// 左侧panel,渐变背景色
  117. ///// </summary>
  118. ///// <param name="sender"></param>
  119. ///// <param name="e"></param>
  120. //private void Panel1_Paint(object sender, PaintEventArgs e)
  121. //{
  122. // LinearGradientBrush brush = new LinearGradientBrush(e.ClipRectangle, Color.Yellow, Color.Blue, LinearGradientMode.Vertical);
  123. // ColorBlend colorBlend = new ColorBlend();
  124. // colorBlend.Colors = new Color[]{ Color.Yellow, Color.Red, Color.Green, Color.Blue };
  125. // colorBlend.Positions = new float[] { 0 / 3f, 1 / 3f, 2 / 3f, 3 / 3f };
  126. // brush.InterpolationColors = colorBlend;
  127. // e.Graphics.FillRectangle(brush, e.ClipRectangle);
  128. //}
  129. private void Panel1_ValueChanged(object sender, IndexEventArgs ce)
  130. {
  131. color = panel1.ValueToColor(panel1.Value);
  132. pictureBox1.Refresh();
  133. }
  134. private void InitializeComponent()
  135. {
  136. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  137. this.label6 = new System.Windows.Forms.Label();
  138. this.label5 = new System.Windows.Forms.Label();
  139. this.label4 = new System.Windows.Forms.Label();
  140. this.label3 = new System.Windows.Forms.Label();
  141. this.label2 = new System.Windows.Forms.Label();
  142. this.label1 = new System.Windows.Forms.Label();
  143. this.panel1 = new PaintDotNet.CustomControl.LinearColorPickerControl();
  144. this.label7 = new System.Windows.Forms.Label();
  145. this.label8 = new System.Windows.Forms.Label();
  146. this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
  147. this.trackBar1 = new System.Windows.Forms.TrackBar();
  148. this.label9 = new System.Windows.Forms.Label();
  149. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  150. ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
  151. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
  152. this.SuspendLayout();
  153. //
  154. // pictureBox1
  155. //
  156. this.pictureBox1.Location = new System.Drawing.Point(21, 8);
  157. this.pictureBox1.Name = "pictureBox1";
  158. this.pictureBox1.Size = new System.Drawing.Size(100, 100);
  159. this.pictureBox1.TabIndex = 1;
  160. this.pictureBox1.TabStop = false;
  161. //
  162. // label6
  163. //
  164. this.label6.AutoSize = true;
  165. this.label6.Location = new System.Drawing.Point(124, 95);
  166. this.label6.Name = "label6";
  167. this.label6.Size = new System.Drawing.Size(11, 12);
  168. this.label6.TabIndex = 14;
  169. this.label6.Text = "V";
  170. //
  171. // label5
  172. //
  173. this.label5.AutoSize = true;
  174. this.label5.Location = new System.Drawing.Point(124, 79);
  175. this.label5.Name = "label5";
  176. this.label5.Size = new System.Drawing.Size(11, 12);
  177. this.label5.TabIndex = 13;
  178. this.label5.Text = "S";
  179. //
  180. // label4
  181. //
  182. this.label4.AutoSize = true;
  183. this.label4.Location = new System.Drawing.Point(124, 63);
  184. this.label4.Name = "label4";
  185. this.label4.Size = new System.Drawing.Size(11, 12);
  186. this.label4.TabIndex = 12;
  187. this.label4.Text = "H";
  188. //
  189. // label3
  190. //
  191. this.label3.AutoSize = true;
  192. this.label3.Location = new System.Drawing.Point(124, 41);
  193. this.label3.Name = "label3";
  194. this.label3.Size = new System.Drawing.Size(11, 12);
  195. this.label3.TabIndex = 11;
  196. this.label3.Text = "B";
  197. //
  198. // label2
  199. //
  200. this.label2.AutoSize = true;
  201. this.label2.Location = new System.Drawing.Point(124, 25);
  202. this.label2.Name = "label2";
  203. this.label2.Size = new System.Drawing.Size(11, 12);
  204. this.label2.TabIndex = 10;
  205. this.label2.Text = "G";
  206. //
  207. // label1
  208. //
  209. this.label1.AutoSize = true;
  210. this.label1.Location = new System.Drawing.Point(124, 9);
  211. this.label1.Name = "label1";
  212. this.label1.Size = new System.Drawing.Size(35, 12);
  213. this.label1.TabIndex = 9;
  214. this.label1.Text = "R=255";
  215. //
  216. // panel1
  217. //
  218. this.panel1.Count = 1;
  219. this.panel1.CustomGradient = new System.Drawing.Color[] {
  220. System.Drawing.Color.White,
  221. System.Drawing.Color.Yellow,
  222. System.Drawing.Color.Red,
  223. System.Drawing.Color.Green,
  224. System.Drawing.Color.Blue,
  225. System.Drawing.Color.Black};
  226. this.panel1.DrawFarNub = true;
  227. this.panel1.DrawNearNub = true;
  228. this.panel1.Location = new System.Drawing.Point(3, 8);
  229. this.panel1.MaxColor = System.Drawing.Color.White;
  230. this.panel1.MinColor = System.Drawing.Color.Black;
  231. this.panel1.Name = "panel1";
  232. this.panel1.Orientation = System.Windows.Forms.Orientation.Vertical;
  233. this.panel1.Size = new System.Drawing.Size(14, 100);
  234. this.panel1.TabIndex = 17;
  235. this.panel1.TabStop = false;
  236. this.panel1.Value = 153;
  237. this.panel1.ValueChanged += new PaintDotNet.IndexEventHandler(this.Panel1_ValueChanged);
  238. //
  239. // label7
  240. //
  241. this.label7.AutoSize = true;
  242. this.label7.BackColor = System.Drawing.Color.Transparent;
  243. this.label7.Location = new System.Drawing.Point(24, 11);
  244. this.label7.Name = "label7";
  245. this.label7.Size = new System.Drawing.Size(17, 12);
  246. this.label7.TabIndex = 18;
  247. this.label7.Text = "X=";
  248. //
  249. // label8
  250. //
  251. this.label8.AutoSize = true;
  252. this.label8.BackColor = System.Drawing.Color.Transparent;
  253. this.label8.Location = new System.Drawing.Point(24, 27);
  254. this.label8.Name = "label8";
  255. this.label8.Size = new System.Drawing.Size(17, 12);
  256. this.label8.TabIndex = 19;
  257. this.label8.Text = "Y=";
  258. //
  259. // numericUpDown1
  260. //
  261. this.numericUpDown1.Location = new System.Drawing.Point(4, 114);
  262. this.numericUpDown1.Maximum = new decimal(new int[] {
  263. 10,
  264. 0,
  265. 0,
  266. 0});
  267. this.numericUpDown1.Minimum = new decimal(new int[] {
  268. 1,
  269. 0,
  270. 0,
  271. 0});
  272. this.numericUpDown1.Name = "numericUpDown1";
  273. this.numericUpDown1.Size = new System.Drawing.Size(37, 21);
  274. this.numericUpDown1.TabIndex = 20;
  275. this.numericUpDown1.Value = new decimal(new int[] {
  276. 1,
  277. 0,
  278. 0,
  279. 0});
  280. this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
  281. //
  282. // trackBar1
  283. //
  284. this.trackBar1.Location = new System.Drawing.Point(68, 114);
  285. this.trackBar1.Minimum = 1;
  286. this.trackBar1.Name = "trackBar1";
  287. this.trackBar1.Size = new System.Drawing.Size(91, 45);
  288. this.trackBar1.TabIndex = 21;
  289. this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None;
  290. this.trackBar1.Value = 1;
  291. this.trackBar1.ValueChanged += new System.EventHandler(this.trackBar1_ValueChanged);
  292. //
  293. // label9
  294. //
  295. this.label9.AutoSize = true;
  296. this.label9.Location = new System.Drawing.Point(45, 118);
  297. this.label9.Name = "label9";
  298. this.label9.Size = new System.Drawing.Size(0, 12);
  299. this.label9.TabIndex = 22;
  300. //
  301. // PixelTrackingDialog
  302. //
  303. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  304. this.ClientSize = new System.Drawing.Size(164, 141);
  305. this.Controls.Add(this.label9);
  306. this.Controls.Add(this.trackBar1);
  307. this.Controls.Add(this.numericUpDown1);
  308. this.Controls.Add(this.label8);
  309. this.Controls.Add(this.label7);
  310. this.Controls.Add(this.panel1);
  311. this.Controls.Add(this.label6);
  312. this.Controls.Add(this.label5);
  313. this.Controls.Add(this.label4);
  314. this.Controls.Add(this.label3);
  315. this.Controls.Add(this.label2);
  316. this.Controls.Add(this.label1);
  317. this.Controls.Add(this.pictureBox1);
  318. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  319. this.Name = "PixelTrackingDialog";
  320. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.PixelTrackingDialog_FormClosing);
  321. this.Controls.SetChildIndex(this.pictureBox1, 0);
  322. this.Controls.SetChildIndex(this.label1, 0);
  323. this.Controls.SetChildIndex(this.label2, 0);
  324. this.Controls.SetChildIndex(this.label3, 0);
  325. this.Controls.SetChildIndex(this.label4, 0);
  326. this.Controls.SetChildIndex(this.label5, 0);
  327. this.Controls.SetChildIndex(this.label6, 0);
  328. this.Controls.SetChildIndex(this.panel1, 0);
  329. this.Controls.SetChildIndex(this.label7, 0);
  330. this.Controls.SetChildIndex(this.label8, 0);
  331. this.Controls.SetChildIndex(this.numericUpDown1, 0);
  332. this.Controls.SetChildIndex(this.trackBar1, 0);
  333. this.Controls.SetChildIndex(this.label9, 0);
  334. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  335. ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
  336. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
  337. this.ResumeLayout(false);
  338. this.PerformLayout();
  339. }
  340. public bool IsPreview()
  341. {
  342. bool iss = CameraPreviewDialog.cameraPreviewDialog != null && CameraPreviewDialog.cameraPreviewDialog.IsLeave() && CameraPreviewDialog.cameraPreviewDialog.documentWorkspace.CompositionSurface != null;
  343. return iss;
  344. }
  345. /// <summary>
  346. /// 设置像素跟踪的数据
  347. /// </summary>
  348. public unsafe void SetImageAndData(System.Drawing.Point point, DocumentView doc)
  349. {
  350. mat = doc.CompositionSurface.CreatedAliasedMat();
  351. scale = int.Parse(this.numericUpDown1.Value.ToString());
  352. if (point.X >= 0 && point.Y >= 0 && point.X <= mat.Width && point.Y <= mat.Height)
  353. {
  354. /*if (point.X < 50 || point.X > mat.Width - 50 || point.Y < 50 || point.Y > mat.Height - 50)
  355. {
  356. int direction = CalcDirection(point, mat);
  357. Mat temp_1 = CalcCropMat(direction, point, mat);
  358. bmat = CalcCopyMakeBorder(direction, temp_1);
  359. }
  360. else*/
  361. {
  362. int x = point.X - 50;
  363. int y = point.Y - 50;
  364. if (x < 0) x = 0;
  365. if (y < 0) y = 0;
  366. int offsetX = (mat.Width - x) > 100 ? 100 : (mat.Width - x);
  367. int offsetY = (mat.Height - y) > 100 ? 100 : (mat.Height - y);
  368. if (x == 0) offsetX = 100 - 50 + point.X;
  369. if (y == 0) offsetY = 100 - 50 + point.Y;
  370. //if (x + offsetX > mat.Width) offsetX = x + offsetX - mat.Width;
  371. //if (y + offsetY > mat.Height) offsetY = y + offsetY - mat.Height;
  372. if (x + offsetX > mat.Width) offsetX = mat.Width - x;
  373. if (y + offsetY > mat.Height) offsetY = mat.Height - y;
  374. bmat = new Mat(mat, new Rect(x, y, offsetX, offsetY));
  375. //bmat = new Mat(mat, new Rect(point.X - 50, point.Y - 50, 100, 100));
  376. }
  377. //如果不足100*100,补足100*100
  378. if(bmat.Width<100 || bmat.Height<100)
  379. {
  380. bmat = CalcCopyMakeBorder(CalcDirection1(point, mat), bmat);
  381. //Cv2.CopyMakeBorder(bmat, bmat, 100 - bmat.Height, 0, 100 - bmat.Width, 0, BorderTypes.Constant, Scalar.All(255));
  382. }
  383. }
  384. this.label7.Text = "X=" + point.X.ToString();
  385. this.label8.Text = "Y=" + point.Y.ToString();
  386. if (point.X > 0 && point.Y > 0 && point.X < mat.Width && point.Y < mat.Height)
  387. {
  388. bgr = mat.At<Vec3b>(point.Y, point.X);
  389. this.label1.Text = "R=" + bgr[2];
  390. this.label2.Text = "G=" + bgr[1];
  391. this.label3.Text = "B=" + bgr[0];
  392. BaseTools.RgbToHsv(bgr, out h, out s, out v);
  393. this.label4.Text = "H=" + (int)(h / 2);
  394. this.label5.Text = "S=" + (int)(s * 255);
  395. this.label6.Text = "V=" + (int)(v * 255);
  396. //设置源图像ROI
  397. ImageROI = bmat;
  398. //按缩放比例截取小图像
  399. Rect roi1 = new Rect((100 - 100 / scale) / 2, (100 - 100 / scale) / 2, 100 / scale, 100 / scale);
  400. ImageROI = new Mat(ImageROI, roi1);
  401. //按固定宽高放大图像
  402. Cv2.Resize(ImageROI, ImageROI, new OpenCvSharp.Size(100, 100));
  403. this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(ImageROI);
  404. }
  405. GC.Collect();
  406. this.Refresh();
  407. }
  408. /// <summary>
  409. /// 计算方向 1上 2右上 3右 4右下 5下 6左下 7左 8左上
  410. /// </summary>
  411. /// <returns></returns>
  412. private int CalcDirection(System.Drawing.Point point, Mat mat)
  413. {
  414. int direction = 1;
  415. if (point.Y < 100) direction = 1;
  416. if (point.X > mat.Width - 100) direction = 3;
  417. if (point.Y > mat.Height - 100) direction = 5;
  418. if (point.X < 100) direction = 7;
  419. if (point.Y < 100 && point.X > mat.Width - 100) direction = 2;
  420. if (point.X > mat.Width - 100 && point.Y > mat.Height - 100) direction = 4;
  421. if (point.X < 100 && point.Y > mat.Height - 100) direction = 6;
  422. if (point.Y < 100 && point.X < 100) direction = 8;
  423. return direction;
  424. }
  425. private int CalcDirection1(System.Drawing.Point point, Mat mat)
  426. {
  427. int direction = 1;
  428. if (point.Y < 50) direction = 1;
  429. if (point.X > mat.Width - 50) direction = 3;
  430. if (point.Y > mat.Height - 50) direction = 5;
  431. if (point.X < 50) direction = 7;
  432. if (point.Y < 50 && point.X > mat.Width - 50) direction = 2;
  433. if (point.X > mat.Width - 50 && point.Y > mat.Height - 50) direction = 4;
  434. if (point.X < 50 && point.Y > mat.Height - 50) direction = 6;
  435. if (point.Y < 50 && point.X < 50) direction = 8;
  436. return direction;
  437. }
  438. /// <summary>
  439. /// 截取部分mat
  440. /// 1上 2右上 3右 4右下 5下 6左下 7左 8左上
  441. /// </summary>
  442. /// <param name="direction"></param>
  443. /// <param name="point"></param>
  444. /// <param name="mat"></param>
  445. private Mat CalcCropMat(int direction, System.Drawing.Point point, Mat mat)
  446. {
  447. Mat temp = new Mat(mat, new Rect(point.X - 50, point.Y, 100, point.Y < 50 ? 50 : point.Y));
  448. /*Mat temp = null;
  449. int x, y, x_off, y_off;
  450. switch (direction)
  451. {
  452. case 1: //上
  453. temp = new Mat(mat, new Rect(point.X - 50, point.Y, 100, point.Y < 50 ? 50 : point.Y));
  454. break;
  455. case 2: //右上
  456. x = point.X - 50;
  457. y = point.Y;
  458. x_off = (mat.Width - x) > 100 ? 100 : (mat.Width - x);
  459. y_off = 50;
  460. temp = new Mat(mat, new Rect(x, y, x_off, y_off));
  461. break;
  462. case 3: //右
  463. temp = new Mat(mat, new Rect(point.X - 50,
  464. point.Y - 50,
  465. point.X > mat.Width - 50 ? 50 : mat.Width - point.X,
  466. 100));
  467. break;
  468. case 4: //右下
  469. x = point.X - 50;
  470. y = point.Y - 50;
  471. if (y < 0) y = point.Y;
  472. x_off = (mat.Width - x) > 100 ? 100 : (mat.Width - x);
  473. y_off = (mat.Height - y) > 100 ? 100 : (mat.Height - y);
  474. temp = new Mat(mat, new Rect(x, y, x_off, y_off));
  475. break;
  476. case 5: //下
  477. y = point.Y - 50;
  478. if (y < 0) y = point.Y;
  479. temp = new Mat(mat, new Rect(point.X - 50, y, 100, (mat.Height - y) > 100 ? 100 : (mat.Height - y)));
  480. break;
  481. case 6: //左下
  482. x = point.X;
  483. y = (point.Y - 50) < 0 ? point.Y : (point.Y - 50);
  484. x_off = (mat.Width - x) > 50 ? 50 : (mat.Width - x);
  485. y_off = (mat.Height - y) > 100 ? 100 : (mat.Height - y);
  486. temp = new Mat(mat, new Rect(x, y, x_off, y_off));
  487. break;
  488. case 7: //左
  489. x = point.X;
  490. y = (point.Y - 50) < 0 ? point.Y : (point.Y - 50);
  491. temp = new Mat(mat, new Rect(x, y,
  492. (mat.Width - x) > 50 ? 50 : (mat.Width - x),
  493. (mat.Height - y) > 100 ? 100 : (mat.Height - y)));
  494. break;
  495. case 8: //左上
  496. temp = new Mat(mat, new Rect(point.X, point.Y, (mat.Width - point.X) > 50 ? 50 : (mat.Width - point.X), (mat.Height - point.Y) > 50 ? 50 : (mat.Height - point.Y)));
  497. break;
  498. }*/
  499. return temp;
  500. }
  501. /// <summary>
  502. /// 扩展边界
  503. /// 1上 2右上 3右 4右下 5下 6左下 7左 8左上
  504. /// </summary>
  505. /// <param name="direction"></param>
  506. /// <param name="temp_1"></param>
  507. /// <returns></returns>
  508. private Mat CalcCopyMakeBorder(int direction, Mat temp_1)
  509. {
  510. switch (direction)
  511. {
  512. case 1:
  513. Cv2.CopyMakeBorder(temp_1, temp_1, 100 - temp_1.Height, 0, 0, 0, BorderTypes.Constant, Scalar.All(255));
  514. break;
  515. case 2:
  516. Cv2.CopyMakeBorder(temp_1, temp_1, 100 - temp_1.Height, 0, 0, 100 - temp_1.Width, BorderTypes.Constant, Scalar.All(255));
  517. break;
  518. case 3:
  519. Cv2.CopyMakeBorder(temp_1, temp_1, 0, 0, 0, 100 - temp_1.Width, BorderTypes.Constant, Scalar.All(255));
  520. break;
  521. case 4:
  522. Cv2.CopyMakeBorder(temp_1, temp_1, 0, 100 - temp_1.Height, 0, 100 - temp_1.Width, BorderTypes.Constant, Scalar.All(255));
  523. break;
  524. case 5:
  525. Cv2.CopyMakeBorder(temp_1, temp_1, 0, 100 - temp_1.Height, 0, 0, BorderTypes.Constant, Scalar.All(255));
  526. break;
  527. case 6:
  528. Cv2.CopyMakeBorder(temp_1, temp_1, 0, 100 - temp_1.Height, 100 - temp_1.Width, 0, BorderTypes.Constant, Scalar.All(255));
  529. break;
  530. case 7:
  531. Cv2.CopyMakeBorder(temp_1, temp_1, 0, 0, 100 - temp_1.Width, 0, BorderTypes.Constant, Scalar.All(255));
  532. break;
  533. case 8:
  534. Cv2.CopyMakeBorder(temp_1, temp_1, 100 - temp_1.Height, 0, 100 - temp_1.Width, 0, BorderTypes.Constant, Scalar.All(255));
  535. break;
  536. }
  537. return temp_1;
  538. }
  539. }
  540. }