CameraRealTimeDepth.cs 16 KB

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