CameraRealTimeDepth.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using OpenCvSharp;
  8. using OpenCvSharp.Extensions;
  9. using PaintDotNet.Adjust.BaseImage;
  10. using PaintDotNet.Base.SettingModel;
  11. using PaintDotNet.Camera;
  12. namespace PaintDotNet.ImageCollect.CameraEDOF
  13. {
  14. internal class CameraRealTimeDepth : FloatingToolForm
  15. {
  16. #region
  17. /// <summary>
  18. /// Required designer variable.
  19. /// </summary>
  20. private System.ComponentModel.IContainer components = null;
  21. /// <summary>
  22. /// Clean up any resources being used.
  23. /// </summary>
  24. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  25. protected override void Dispose(bool disposing)
  26. {
  27. if (disposing && (components != null))
  28. {
  29. components.Dispose();
  30. }
  31. base.Dispose(disposing);
  32. }
  33. #region Windows Form Designer generated code
  34. private void InitializeLanguageText()
  35. {
  36. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  37. this.btnStart.Text = PdnResources.GetString("Menu.Started.text");
  38. this.btnCreatImgFile.Text = PdnResources.GetString("Menu.Createpicture.text");
  39. this.btnStop.Text = PdnResources.GetString("Menu.stop.text");
  40. this.groupBox2.Text = PdnResources.GetString("Menu.Preview.text");
  41. this.groupBox3.Text = PdnResources.GetString("Menu.ImageCollection.RealTimeDepth.Text");
  42. this.Text = PdnResources.GetString("Menu.ImageCollection.RealTimeDepth.Text");
  43. }
  44. /// <summary>
  45. /// Required method for Designer support - do not modify
  46. /// the contents of this method with the code editor.
  47. /// </summary>
  48. private void InitializeComponent()
  49. {
  50. this.groupBox1 = new System.Windows.Forms.GroupBox();
  51. this.btnStart = new System.Windows.Forms.Button();
  52. this.btnCreatImgFile = new System.Windows.Forms.Button();
  53. this.btnStop = new System.Windows.Forms.Button();
  54. this.groupBox2 = new System.Windows.Forms.GroupBox();
  55. this.groupBox3 = new System.Windows.Forms.GroupBox();
  56. this.picDepth = new System.Windows.Forms.PictureBox();
  57. this.picPreview = new System.Windows.Forms.PictureBox();
  58. this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
  59. this.groupBox1.SuspendLayout();
  60. this.groupBox2.SuspendLayout();
  61. this.groupBox3.SuspendLayout();
  62. ((System.ComponentModel.ISupportInitialize)(this.picDepth)).BeginInit();
  63. ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit();
  64. this.tableLayoutPanel1.SuspendLayout();
  65. this.SuspendLayout();
  66. //
  67. // groupBox1
  68. //
  69. this.groupBox1.Controls.Add(this.btnStart);
  70. this.groupBox1.Controls.Add(this.btnCreatImgFile);
  71. this.groupBox1.Controls.Add(this.btnStop);
  72. this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
  73. this.groupBox1.Location = new System.Drawing.Point(0, 0);
  74. this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
  75. this.groupBox1.Name = "groupBox1";
  76. this.groupBox1.Padding = new System.Windows.Forms.Padding(2);
  77. this.groupBox1.Size = new System.Drawing.Size(1243, 77);
  78. this.groupBox1.TabIndex = 0;
  79. this.groupBox1.TabStop = false;
  80. this.groupBox1.Text = "操作";
  81. //
  82. // btnStart
  83. //
  84. this.btnStart.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  85. this.btnStart.Location = new System.Drawing.Point(1082, 29);
  86. this.btnStart.Margin = new System.Windows.Forms.Padding(2);
  87. this.btnStart.Name = "btnStart";
  88. this.btnStart.Size = new System.Drawing.Size(65, 29);
  89. this.btnStart.TabIndex = 0;
  90. this.btnStart.Text = "开始";
  91. this.btnStart.UseVisualStyleBackColor = true;
  92. this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
  93. //
  94. // btnCreatImgFile
  95. //
  96. this.btnCreatImgFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  97. this.btnCreatImgFile.Location = new System.Drawing.Point(1160, 29);
  98. this.btnCreatImgFile.Margin = new System.Windows.Forms.Padding(2);
  99. this.btnCreatImgFile.Name = "btnCreatImgFile";
  100. this.btnCreatImgFile.Size = new System.Drawing.Size(65, 29);
  101. this.btnCreatImgFile.TabIndex = 2;
  102. this.btnCreatImgFile.Text = "创建图片";
  103. this.btnCreatImgFile.UseVisualStyleBackColor = true;
  104. this.btnCreatImgFile.Click += new System.EventHandler(this.btnCreatImgFile_Click);
  105. //
  106. // btnStop
  107. //
  108. this.btnStop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  109. this.btnStop.Location = new System.Drawing.Point(1082, 29);
  110. this.btnStop.Margin = new System.Windows.Forms.Padding(2);
  111. this.btnStop.Name = "btnStop";
  112. this.btnStop.Size = new System.Drawing.Size(65, 29);
  113. this.btnStop.TabIndex = 1;
  114. this.btnStop.UseVisualStyleBackColor = true;
  115. this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
  116. //
  117. // groupBox2
  118. //
  119. this.groupBox2.Controls.Add(this.picPreview);
  120. this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
  121. this.groupBox2.Location = new System.Drawing.Point(2, 2);
  122. this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
  123. this.groupBox2.Name = "groupBox2";
  124. this.groupBox2.Padding = new System.Windows.Forms.Padding(8);
  125. this.groupBox2.Size = new System.Drawing.Size(617, 554);
  126. this.groupBox2.TabIndex = 2;
  127. this.groupBox2.TabStop = false;
  128. this.groupBox2.Text = "预览";
  129. //
  130. // groupBox3
  131. //
  132. this.groupBox3.Controls.Add(this.picDepth);
  133. this.groupBox3.Dock = System.Windows.Forms.DockStyle.Fill;
  134. this.groupBox3.Location = new System.Drawing.Point(623, 2);
  135. this.groupBox3.Margin = new System.Windows.Forms.Padding(2);
  136. this.groupBox3.Name = "groupBox3";
  137. this.groupBox3.Padding = new System.Windows.Forms.Padding(8);
  138. this.groupBox3.Size = new System.Drawing.Size(618, 554);
  139. this.groupBox3.TabIndex = 3;
  140. this.groupBox3.TabStop = false;
  141. this.groupBox3.Text = "实时景深扩展";
  142. //
  143. // picDepth
  144. //
  145. this.picDepth.BackColor = System.Drawing.SystemColors.Control;
  146. this.picDepth.Dock = System.Windows.Forms.DockStyle.Fill;
  147. this.picDepth.Location = new System.Drawing.Point(8, 22);
  148. this.picDepth.Margin = new System.Windows.Forms.Padding(2);
  149. this.picDepth.Name = "picDepth";
  150. this.picDepth.Size = new System.Drawing.Size(602, 524);
  151. this.picDepth.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  152. this.picDepth.TabIndex = 0;
  153. this.picDepth.TabStop = false;
  154. //
  155. // picPreview
  156. //
  157. this.picPreview.BackColor = System.Drawing.SystemColors.Control;
  158. this.picPreview.Dock = System.Windows.Forms.DockStyle.Fill;
  159. this.picPreview.Location = new System.Drawing.Point(8, 22);
  160. this.picPreview.Margin = new System.Windows.Forms.Padding(2);
  161. this.picPreview.Name = "picPreview";
  162. this.picPreview.Size = new System.Drawing.Size(601, 524);
  163. this.picPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  164. this.picPreview.TabIndex = 0;
  165. this.picPreview.TabStop = false;
  166. //
  167. // tableLayoutPanel1
  168. //
  169. this.tableLayoutPanel1.ColumnCount = 2;
  170. this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
  171. this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
  172. this.tableLayoutPanel1.Controls.Add(this.groupBox3, 1, 0);
  173. this.tableLayoutPanel1.Controls.Add(this.groupBox2, 0, 0);
  174. this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
  175. this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 77);
  176. this.tableLayoutPanel1.Name = "tableLayoutPanel1";
  177. this.tableLayoutPanel1.RowCount = 1;
  178. this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
  179. this.tableLayoutPanel1.Size = new System.Drawing.Size(1243, 558);
  180. this.tableLayoutPanel1.TabIndex = 4;
  181. //
  182. // CameraRealTimeDepth
  183. //
  184. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  185. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  186. this.ClientSize = new System.Drawing.Size(1243, 635);
  187. this.Controls.Add(this.tableLayoutPanel1);
  188. this.Controls.Add(this.groupBox1);
  189. this.Margin = new System.Windows.Forms.Padding(2);
  190. this.Name = "CameraRealTimeDepth";
  191. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  192. this.Text = "实时景深扩展";
  193. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraRealTimeDepth_FormClosing);
  194. this.Load += new System.EventHandler(this.CameraRealTimeDepth_Load);
  195. this.Controls.SetChildIndex(this.groupBox1, 0);
  196. this.Controls.SetChildIndex(this.tableLayoutPanel1, 0);
  197. this.groupBox1.ResumeLayout(false);
  198. this.groupBox2.ResumeLayout(false);
  199. this.groupBox3.ResumeLayout(false);
  200. ((System.ComponentModel.ISupportInitialize)(this.picDepth)).EndInit();
  201. ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit();
  202. this.tableLayoutPanel1.ResumeLayout(false);
  203. this.ResumeLayout(false);
  204. }
  205. #endregion
  206. private System.Windows.Forms.GroupBox groupBox1;
  207. private System.Windows.Forms.Button btnCreatImgFile;
  208. private System.Windows.Forms.Button btnStop;
  209. private System.Windows.Forms.Button btnStart;
  210. private System.Windows.Forms.GroupBox groupBox2;
  211. private System.Windows.Forms.GroupBox groupBox3;
  212. private System.Windows.Forms.PictureBox picDepth;
  213. #endregion
  214. private AppWorkspace m_appWorkspace;
  215. private Mat m_mat;
  216. private Thread m_depthThread;
  217. private bool _isWorking;
  218. private Mat[] Coll_Ori = new Mat[2];
  219. private Mat m_merge;
  220. private PictureBox picPreview;
  221. private TableLayoutPanel tableLayoutPanel1;
  222. private string m_filePath = Application.StartupPath + "\\Picture\\";
  223. public CameraRealTimeDepth(AppWorkspace appWorkspace)
  224. {
  225. this.m_appWorkspace = appWorkspace;
  226. InitializeComponent();
  227. InitializeLanguageText();
  228. }
  229. private void CameraRealTimeDepth_Load(object sender, EventArgs e)
  230. {
  231. btnStop.Visible = false;
  232. CameraConfigs cameraConfig = CameraConfigs.GetInstance();
  233. cameraConfig.CameraParamInit();
  234. CameraManager.FrameCallback += CallbackDraw;
  235. }
  236. private byte[] m_pbuf;
  237. /// <summary>
  238. /// 绘制
  239. /// </summary>
  240. /// <param name="pBuf"></param>
  241. /// <param name="obj"></param>
  242. public void CallbackDraw(Bitmap bitmap)
  243. {
  244. if (bitmap == null)
  245. return;
  246. var mat = PaintDotNet.Camera.Tools.ToMat(bitmap);
  247. picPreview.Image = (Image)bitmap.Clone();
  248. m_mat = mat.Clone();
  249. picPreview.Invalidate();
  250. new Task(new Action(() => { WaitForDepthThreadEntry(mat); })).Start();
  251. }
  252. private void btnStart_Click(object sender, EventArgs e)
  253. {
  254. if (_isWorking)
  255. {
  256. return;
  257. }
  258. _isWorking = true;
  259. picDepth.Image = null;
  260. m_merge = null;
  261. updateBtn();
  262. t = 0;
  263. }
  264. private void updateBtn()
  265. {
  266. if (_isWorking)
  267. {
  268. btnStop.Visible = true;
  269. btnStart.Visible = false;
  270. btnCreatImgFile.Enabled = false;
  271. }
  272. else
  273. {
  274. btnStop.Visible = false;
  275. btnStart.Visible = true;
  276. }
  277. }
  278. int t = 0;
  279. /// <summary>
  280. /// 景深扩展
  281. /// </summary>
  282. private void WaitForDepthThreadEntry(Mat mat)
  283. {
  284. if (!_isWorking)
  285. return;
  286. if (t > 0) return;
  287. try
  288. {
  289. t++;
  290. if (m_merge == null)
  291. {
  292. m_merge = mat;
  293. return;
  294. }
  295. //原始图
  296. Coll_Ori[1] = mat;
  297. Coll_Ori[0] = m_merge;
  298. m_merge = Merge.GetMergeMatForCamera(Coll_Ori);
  299. this.Invoke(new Action(() =>
  300. {
  301. picDepth.Image = BitmapConverter.ToBitmap(m_merge);
  302. }));
  303. }
  304. catch { }
  305. finally { t--; }
  306. }
  307. private void CameraRealTimeDepth_FormClosing(object sender, FormClosingEventArgs e)
  308. {
  309. if (m_depthThread != null)
  310. {
  311. _isWorking = false;
  312. updateBtn();
  313. m_depthThread.Abort();
  314. }
  315. CameraManager.FrameCallback -= CallbackDraw;
  316. if (m_mat != null && !m_mat.IsDisposed)
  317. {
  318. m_mat.Dispose();
  319. }
  320. if (m_merge != null && !m_merge.IsDisposed)
  321. {
  322. m_merge.Dispose();
  323. }
  324. GC.Collect();
  325. }
  326. private void btnStop_Click(object sender, EventArgs e)
  327. {
  328. _isWorking = false;
  329. updateBtn();
  330. btnCreatImgFile.Enabled = true;
  331. }
  332. private void btnCreatImgFile_Click(object sender, EventArgs e)
  333. {
  334. if (m_merge == null)
  335. {
  336. MessageBox.Show(PdnResources.GetString("Menu.ofimageshavenotyetbeengene.Text"));
  337. return;
  338. }
  339. DocumentWorkspace dw = m_appWorkspace.AddNewDocumentWorkspace();
  340. //dw.rules = Startup.instance.rules;
  341. dw.xmlSaveModel = Startup.instance.ruleDB;
  342. dw.InitRulerInfo();
  343. Document document = Document.FromImageMat(m_merge.Clone());
  344. dw.Document = document;
  345. dw.fileText = string.Format("{0}-{1:D3}", "实时景深", Startup.instance.step_length++);
  346. m_appWorkspace.ActiveDocumentWorkspace = dw;
  347. }
  348. }
  349. }