CameraRealTimeStitchingDialog.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using OpenCvSharp;
  13. using OpenCvSharp.Extensions;
  14. using PaintDotNet.Adjust;
  15. using PaintDotNet.Adjust.BaseImage;
  16. using PaintDotNet.Base.SettingModel;
  17. using TUCAMAPI;
  18. using TUCamera;
  19. namespace PaintDotNet.ImageCollect
  20. {
  21. internal class CameraRealTimeStitchingDialog : FloatingToolForm
  22. {
  23. #region
  24. /// <summary>
  25. /// Required designer variable.
  26. /// </summary>
  27. private System.ComponentModel.IContainer components = null;
  28. /// <summary>
  29. /// Clean up any resources being used.
  30. /// </summary>
  31. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  32. protected override void Dispose(bool disposing)
  33. {
  34. if (disposing && (components != null))
  35. {
  36. components.Dispose();
  37. }
  38. base.Dispose(disposing);
  39. }
  40. #region Windows Form Designer generated code
  41. private void InitializeLanguageText()
  42. {
  43. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  44. this.btnCreatImgFile.Text = PdnResources.GetString("Menu.Createpicture.text");
  45. this.btnWorking.Text = PdnResources.GetString("Menu.Started.text");
  46. this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text");
  47. this.groupBox4.Text = PdnResources.GetString("Menu.Puzzleeffect.text");
  48. this.Text = PdnResources.GetString("Menu.imagecapture.Reatimldexpansion.Reaestitching.text");
  49. }
  50. /// <summary>
  51. /// Required method for Designer support - do not modify
  52. /// the contents of this method with the code editor.
  53. /// </summary>
  54. private void InitializeComponent()
  55. {
  56. this.groupBox1 = new System.Windows.Forms.GroupBox();
  57. this.btnCreatImgFile = new System.Windows.Forms.Button();
  58. this.btnWorking = new System.Windows.Forms.Button();
  59. this.groupBox3 = new System.Windows.Forms.GroupBox();
  60. this.picPreview = new System.Windows.Forms.PictureBox();
  61. this.groupBox4 = new System.Windows.Forms.GroupBox();
  62. this.picMatch = new System.Windows.Forms.PictureBox();
  63. this.groupBox1.SuspendLayout();
  64. this.groupBox3.SuspendLayout();
  65. ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit();
  66. this.groupBox4.SuspendLayout();
  67. ((System.ComponentModel.ISupportInitialize)(this.picMatch)).BeginInit();
  68. this.SuspendLayout();
  69. //
  70. // groupBox1
  71. //
  72. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  73. | System.Windows.Forms.AnchorStyles.Right)));
  74. this.groupBox1.Controls.Add(this.btnCreatImgFile);
  75. this.groupBox1.Controls.Add(this.btnWorking);
  76. this.groupBox1.Location = new System.Drawing.Point(12, 12);
  77. this.groupBox1.Name = "groupBox1";
  78. this.groupBox1.Size = new System.Drawing.Size(1217, 87);
  79. this.groupBox1.TabIndex = 1;
  80. this.groupBox1.TabStop = false;
  81. this.groupBox1.Text = "操作";
  82. //
  83. // btnCreatImgFile
  84. //
  85. this.btnCreatImgFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  86. this.btnCreatImgFile.Location = new System.Drawing.Point(1106, 28);
  87. this.btnCreatImgFile.Name = "btnCreatImgFile";
  88. this.btnCreatImgFile.Size = new System.Drawing.Size(92, 34);
  89. this.btnCreatImgFile.TabIndex = 1;
  90. this.btnCreatImgFile.Text = "创建图片";
  91. this.btnCreatImgFile.UseVisualStyleBackColor = true;
  92. this.btnCreatImgFile.Click += new System.EventHandler(this.btnCreatImgFile_Click);
  93. //
  94. // btnWorking
  95. //
  96. this.btnWorking.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  97. this.btnWorking.Location = new System.Drawing.Point(993, 28);
  98. this.btnWorking.Name = "btnWorking";
  99. this.btnWorking.Size = new System.Drawing.Size(89, 34);
  100. this.btnWorking.TabIndex = 0;
  101. this.btnWorking.Text = "开始";
  102. this.btnWorking.UseVisualStyleBackColor = true;
  103. this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click);
  104. //
  105. // groupBox3
  106. //
  107. this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  108. | System.Windows.Forms.AnchorStyles.Left)));
  109. this.groupBox3.Controls.Add(this.picPreview);
  110. this.groupBox3.Location = new System.Drawing.Point(12, 105);
  111. this.groupBox3.Name = "groupBox3";
  112. this.groupBox3.Size = new System.Drawing.Size(466, 517);
  113. this.groupBox3.TabIndex = 3;
  114. this.groupBox3.TabStop = false;
  115. this.groupBox3.Text = "预览";
  116. //
  117. // picPreview
  118. //
  119. this.picPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  120. | System.Windows.Forms.AnchorStyles.Left)
  121. | System.Windows.Forms.AnchorStyles.Right)));
  122. this.picPreview.Location = new System.Drawing.Point(20, 74);
  123. this.picPreview.Name = "picPreview";
  124. this.picPreview.Size = new System.Drawing.Size(429, 364);
  125. this.picPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  126. this.picPreview.TabIndex = 0;
  127. this.picPreview.TabStop = false;
  128. //
  129. // groupBox4
  130. //
  131. this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  132. | System.Windows.Forms.AnchorStyles.Left)
  133. | System.Windows.Forms.AnchorStyles.Right)));
  134. this.groupBox4.Controls.Add(this.picMatch);
  135. this.groupBox4.Location = new System.Drawing.Point(484, 105);
  136. this.groupBox4.Name = "groupBox4";
  137. this.groupBox4.Size = new System.Drawing.Size(745, 517);
  138. this.groupBox4.TabIndex = 4;
  139. this.groupBox4.TabStop = false;
  140. this.groupBox4.Text = "拼图效果";
  141. //
  142. // picMatch
  143. //
  144. this.picMatch.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  145. | System.Windows.Forms.AnchorStyles.Left)
  146. | System.Windows.Forms.AnchorStyles.Right)));
  147. this.picMatch.Location = new System.Drawing.Point(17, 74);
  148. this.picMatch.Name = "picMatch";
  149. this.picMatch.Size = new System.Drawing.Size(709, 416);
  150. this.picMatch.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  151. this.picMatch.TabIndex = 1;
  152. this.picMatch.TabStop = false;
  153. //
  154. // CameraRealTimeStitchingDialog
  155. //
  156. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  157. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  158. this.ClientSize = new System.Drawing.Size(1241, 634);
  159. this.Controls.Add(this.groupBox4);
  160. this.Controls.Add(this.groupBox3);
  161. this.Controls.Add(this.groupBox1);
  162. this.Name = "CameraRealTimeStitchingDialog";
  163. this.Text = "图像实时拼接";
  164. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraRealTimeStitchingDialog_FormClosing);
  165. this.Load += new System.EventHandler(this.CameraRealTimeStitchingDialog_Load);
  166. this.Controls.SetChildIndex(this.groupBox1, 0);
  167. this.Controls.SetChildIndex(this.groupBox3, 0);
  168. this.Controls.SetChildIndex(this.groupBox4, 0);
  169. this.groupBox1.ResumeLayout(false);
  170. this.groupBox3.ResumeLayout(false);
  171. ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit();
  172. this.groupBox4.ResumeLayout(false);
  173. ((System.ComponentModel.ISupportInitialize)(this.picMatch)).EndInit();
  174. this.ResumeLayout(false);
  175. }
  176. #endregion
  177. private System.Windows.Forms.GroupBox groupBox1;
  178. private System.Windows.Forms.Button btnCreatImgFile;
  179. private System.Windows.Forms.Button btnWorking;
  180. private System.Windows.Forms.GroupBox groupBox3;
  181. private System.Windows.Forms.PictureBox picPreview;
  182. private System.Windows.Forms.GroupBox groupBox4;
  183. private System.Windows.Forms.PictureBox picMatch;
  184. #endregion
  185. private AppWorkspace m_appWorkspace;
  186. private TUCamera.TUCamera m_camera;
  187. private CameraParamModel m_cameraParamModel;
  188. private Mat m_mat;
  189. private Thread m_stitchingThread;
  190. private bool is_depth;
  191. private Mat[] Coll_Ori = new Mat[2];
  192. private Mat m_merge;
  193. private string m_filePath = Application.StartupPath + "\\Picture\\";
  194. private bool m_isWorking;
  195. public CameraRealTimeStitchingDialog(AppWorkspace appWorkspace)
  196. {
  197. m_appWorkspace = appWorkspace;
  198. InitializeComponent();
  199. InitializeLanguageText();
  200. }
  201. private void CameraRealTimeStitchingDialog_Load(object sender, EventArgs e)
  202. {
  203. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  204. m_cameraParamModel = Startup.instance.cameraParamModel;
  205. CameraConfigs cameraConfig = CameraConfigs.GetInstance();
  206. cameraConfig.CameraParamInit();
  207. m_camera.m_bufHandler += CallbackDraw;
  208. m_camera.StartWaitForFrame();
  209. }
  210. private byte[] m_pbuf;
  211. /// <summary>
  212. /// 绘制
  213. /// </summary>
  214. /// <param name="pBuf"></param>
  215. /// <param name="obj"></param>
  216. public void CallbackDraw(Bitmap bitmap)
  217. {
  218. m_mat = BitmapConverter.ToMat(bitmap);
  219. picPreview.Image = (Image)bitmap.Clone();
  220. }
  221. private void btnWorking_Click(object sender, EventArgs e)
  222. {
  223. if (m_isWorking)
  224. {
  225. m_isWorking = false;
  226. btnWorking.Text = PdnResources.GetString("Menu.Started.text");
  227. return;
  228. }
  229. m_isWorking = true;
  230. btnWorking.Text = PdnResources.GetString("Menu.stop.text");
  231. m_merge = null;
  232. m_stitchingThread = new Thread(new ThreadStart(WaitForStitchingThreadEntry));
  233. m_stitchingThread.Start();
  234. }
  235. /// <summary>
  236. /// 图像拼接
  237. /// </summary>
  238. private void WaitForStitchingThreadEntry()
  239. {
  240. while (m_isWorking)
  241. {
  242. if (m_mat == null)
  243. {
  244. Thread.Sleep(100);
  245. continue;
  246. }
  247. Mat mat = m_mat;//.Clone();
  248. if (m_merge == null)
  249. {
  250. AdjustIntent.InitMatchPicOneByOne();
  251. }
  252. Rect rect;
  253. var result = AdjustIntent.MatchPicRealtime(mat, m_merge, out bool error, out rect);
  254. if (!error)
  255. {
  256. m_merge = result;
  257. }
  258. else
  259. {
  260. Console.WriteLine("拼图失败");
  261. }
  262. var tmp = m_merge.Clone();
  263. Cv2.Rectangle(tmp, rect, Scalar.Lime, 5, LineTypes.Link4, 4);
  264. picMatch.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(tmp);
  265. picMatch.Invalidate();
  266. GC.Collect();
  267. }
  268. }
  269. private void btnCreatImgFile_Click(object sender, EventArgs e)
  270. {
  271. if (m_merge == null)
  272. {
  273. MessageBox.Show(PdnResources.GetString("Menu.mageMosaicimagehanotbeengeneratedyet.Text"));
  274. return;
  275. }
  276. Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_merge);
  277. DocumentWorkspace dw = m_appWorkspace.AddNewDocumentWorkspace();
  278. Document document = Document.FromImage(bitmap);
  279. dw.Document = document;
  280. dw.xmlSaveModel = Startup.instance.ruleDB;
  281. dw.InitRulerInfo();
  282. dw.fileText = "1";
  283. m_appWorkspace.ActiveDocumentWorkspace = dw;
  284. }
  285. private void CameraRealTimeStitchingDialog_FormClosing(object sender, FormClosingEventArgs e)
  286. {
  287. if (m_stitchingThread != null)
  288. {
  289. m_isWorking = false;
  290. m_stitchingThread.Join();
  291. m_stitchingThread.Abort();
  292. m_stitchingThread.DisableComObjectEagerCleanup();
  293. }
  294. m_camera.m_bufHandler -= CallbackDraw;
  295. m_camera.StopWaitForFrame();
  296. if (m_mat != null && !m_mat.IsDisposed)
  297. {
  298. m_mat.Dispose();
  299. }
  300. if (m_merge != null && !m_merge.IsDisposed)
  301. {
  302. m_merge.Dispose();
  303. }
  304. GC.Collect();
  305. }
  306. }
  307. }